From 15f4e271b04cc9554f1d769a0fe3296464e554f6 Mon Sep 17 00:00:00 2001 From: Lucas <13612932+LucasCoderT@users.noreply.github.com> Date: Sun, 25 Aug 2024 15:11:31 -0600 Subject: [PATCH 1/2] Update Configuration.swift Added handleOpenURL and Authorize support async await --- OctoKit/Configuration.swift | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/OctoKit/Configuration.swift b/OctoKit/Configuration.swift index 730f380..ffbe9a5 100644 --- a/OctoKit/Configuration.swift +++ b/OctoKit/Configuration.swift @@ -102,6 +102,30 @@ public struct OAuthConfiguration: Configuration { task.resume() } } + + #if compiler(>=5.5.2) && canImport(_Concurrency) + @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) + public func authorize(code: String) async throws -> TokenConfiguration? { + let request = OAuthRouter.accessToken(self, code).URLRequest + if let request = request { + let (data, response) = try await session.data(for: request, delegate: nil) + if let response = response as? HTTPURLResponse { + if response.statusCode != 200 { + return nil + } else { + if let string = String(data: data, encoding: .utf8) { + let accessToken = self.accessTokenFromResponse(string) + if let accessToken = accessToken { + return TokenConfiguration(accessToken, url: self.apiEndpoint) + } + } + } + } + } + return nil + } + #endif + public func handleOpenURL(url: URL, completion: @escaping (_ config: TokenConfiguration) -> Void) { if let code = url.URLParameters["code"] { @@ -110,6 +134,16 @@ public struct OAuthConfiguration: Configuration { } } } + + #if compiler(>=5.5.2) && canImport(_Concurrency) + @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) + public func handleOpenURL(url: URL) async throws -> TokenConfiguration? { + if let code = url.URLParameters["code"] { + return try await authorize(code: code) + } + return nil + } + #endif public func accessTokenFromResponse(_ response: String) -> String? { let accessTokenParam = response.components(separatedBy: "&").first From 0f921105fce7665aa2e8ae4be39d1ec92767bd58 Mon Sep 17 00:00:00 2001 From: Lucas <13612932+LucasCoderT@users.noreply.github.com> Date: Sun, 25 Aug 2024 15:35:27 -0600 Subject: [PATCH 2/2] Update Configuration.swift lint --- OctoKit/Configuration.swift | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/OctoKit/Configuration.swift b/OctoKit/Configuration.swift index ffbe9a5..6b8af5c 100644 --- a/OctoKit/Configuration.swift +++ b/OctoKit/Configuration.swift @@ -102,10 +102,10 @@ public struct OAuthConfiguration: Configuration { task.resume() } } - + #if compiler(>=5.5.2) && canImport(_Concurrency) @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) - public func authorize(code: String) async throws -> TokenConfiguration? { + public func authorize(code: String) async throws -> TokenConfiguration? { let request = OAuthRouter.accessToken(self, code).URLRequest if let request = request { let (data, response) = try await session.data(for: request, delegate: nil) @@ -114,9 +114,9 @@ public struct OAuthConfiguration: Configuration { return nil } else { if let string = String(data: data, encoding: .utf8) { - let accessToken = self.accessTokenFromResponse(string) + let accessToken = accessTokenFromResponse(string) if let accessToken = accessToken { - return TokenConfiguration(accessToken, url: self.apiEndpoint) + return TokenConfiguration(accessToken, url: apiEndpoint) } } } @@ -125,7 +125,6 @@ public struct OAuthConfiguration: Configuration { return nil } #endif - public func handleOpenURL(url: URL, completion: @escaping (_ config: TokenConfiguration) -> Void) { if let code = url.URLParameters["code"] { @@ -134,7 +133,7 @@ public struct OAuthConfiguration: Configuration { } } } - + #if compiler(>=5.5.2) && canImport(_Concurrency) @available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) public func handleOpenURL(url: URL) async throws -> TokenConfiguration? {