Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Configuration.swift handleOpenURL and authorize to support async await #192

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions OctoKit/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@ 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 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 = accessTokenFromResponse(string)
if let accessToken = accessToken {
return TokenConfiguration(accessToken, url: apiEndpoint)
}
}
}
}
}
return nil
}
#endif

public func handleOpenURL(url: URL, completion: @escaping (_ config: TokenConfiguration) -> Void) {
if let code = url.URLParameters["code"] {
authorize(code: code) { config in
Expand All @@ -111,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
if let accessTokenParam = accessTokenParam {
Expand Down
Loading