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

Move URLSession to Octokit instead of having it in every method #160

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions OctoKit/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public struct OAuthConfiguration: Configuration {
/// Used for preview support of new APIs, for instance Reaction API.
/// see: https://developer.github.com/changes/2016-05-12-reactions-api-preview/
private var previewCustomHeaders: [HTTPHeader]?
private let session: RequestKitURLSession

public var customHeaders: [HTTPHeader]? {
// More (non-preview) headers can be appended if needed in the future
Expand All @@ -65,20 +66,22 @@ public struct OAuthConfiguration: Configuration {
token: String,
secret: String,
scopes: [String],
previewHeaders: [PreviewHeader] = []) {
previewHeaders: [PreviewHeader] = [],
session: RequestKitURLSession = URLSession.shared) {
apiEndpoint = url
webEndpoint = webURL
self.token = token
self.secret = secret
self.scopes = scopes
self.session = session
previewCustomHeaders = previewHeaders.map { $0.header }
}

public func authenticate() -> URL? {
return OAuthRouter.authorize(self).URLRequest?.url
}

public func authorize(_ session: RequestKitURLSession = URLSession.shared, code: String, completion: @escaping (_ config: TokenConfiguration) -> Void) {
public func authorize(code: String, completion: @escaping (_ config: TokenConfiguration) -> Void) {
let request = OAuthRouter.accessToken(self, code).URLRequest
if let request = request {
let task = session.dataTask(with: request) { data, response, _ in
Expand All @@ -100,9 +103,9 @@ public struct OAuthConfiguration: Configuration {
}
}

public func handleOpenURL(_ session: RequestKitURLSession = URLSession.shared, url: URL, completion: @escaping (_ config: TokenConfiguration) -> Void) {
public func handleOpenURL(url: URL, completion: @escaping (_ config: TokenConfiguration) -> Void) {
if let code = url.URLParameters["code"] {
authorize(session, code: code) { config in
authorize(code: code) { config in
completion(config)
}
}
Expand Down
24 changes: 8 additions & 16 deletions OctoKit/Follow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import FoundationNetworking
public extension Octokit {
/**
Fetches the followers of the authenticated user
- parameter session: RequestKitURLSession, defaults to URLSession.shared
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func myFollowers(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Result<[User], Error>) -> Void) -> URLSessionDataTaskProtocol? {
func myFollowers(completion: @escaping (_ response: Result<[User], Error>) -> Void) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readAuthenticatedFollowers(configuration)
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self) { users, error in
if let error = error {
Expand All @@ -27,23 +26,21 @@ public extension Octokit {
#if compiler(>=5.5.2) && canImport(_Concurrency)
/**
Fetches the followers of the authenticated user
- parameter session: RequestKitURLSession, defaults to URLSession.shared
*/
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func myFollowers(_ session: RequestKitURLSession = URLSession.shared) async throws -> [User] {
func myFollowers() async throws -> [User] {
let router = FollowRouter.readAuthenticatedFollowers(configuration)
return try await router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self)
}
#endif

/**
Fetches the followers of a user
- parameter session: RequestKitURLSession, defaults to URLSession.shared
- parameter name: Name of the user
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func followers(_ session: RequestKitURLSession = URLSession.shared, name: String, completion: @escaping (_ response: Result<[User], Error>) -> Void) -> URLSessionDataTaskProtocol? {
func followers(name: String, completion: @escaping (_ response: Result<[User], Error>) -> Void) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readFollowers(name, configuration)
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self) { users, error in
if let error = error {
Expand All @@ -59,23 +56,21 @@ public extension Octokit {
#if compiler(>=5.5.2) && canImport(_Concurrency)
/**
Fetches the followers of a user
- parameter session: RequestKitURLSession, defaults to URLSession.shared
- parameter name: Name of the user
*/
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func followers(_ session: RequestKitURLSession = URLSession.shared, name: String) async throws -> [User] {
func followers(name: String) async throws -> [User] {
let router = FollowRouter.readFollowers(name, configuration)
return try await router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self)
}
#endif

/**
Fetches the users following the authenticated user
- parameter session: RequestKitURLSession, defaults to URLSession.shared
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func myFollowing(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Result<[User], Error>) -> Void) -> URLSessionDataTaskProtocol? {
func myFollowing(completion: @escaping (_ response: Result<[User], Error>) -> Void) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readAuthenticatedFollowing(configuration)
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self) { users, error in
if let error = error {
Expand All @@ -91,23 +86,21 @@ public extension Octokit {
#if compiler(>=5.5.2) && canImport(_Concurrency)
/**
Fetches the users following the authenticated user
- parameter session: RequestKitURLSession, defaults to URLSession.shared
*/
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func myFollowing(_ session: RequestKitURLSession = URLSession.shared) async throws -> [User] {
func myFollowing() async throws -> [User] {
let router = FollowRouter.readAuthenticatedFollowing(configuration)
return try await router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self)
}
#endif

/**
Fetches the users following a user
- parameter session: RequestKitURLSession, defaults to URLSession.shared
- parameter name: The name of the user
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func following(_ session: RequestKitURLSession = URLSession.shared, name: String, completion: @escaping (_ response: Result<[User], Error>) -> Void) -> URLSessionDataTaskProtocol? {
func following(name: String, completion: @escaping (_ response: Result<[User], Error>) -> Void) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readFollowing(name, configuration)
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self) { users, error in
if let error = error {
Expand All @@ -123,11 +116,10 @@ public extension Octokit {
#if compiler(>=5.5.2) && canImport(_Concurrency)
/**
Fetches the users following a user
- parameter session: RequestKitURLSession, defaults to URLSession.shared
- parameter name: The name of the user
*/
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func following(_ session: RequestKitURLSession = URLSession.shared, name: String) async throws -> [User] {
func following(name: String) async throws -> [User] {
let router = FollowRouter.readFollowing(name, configuration)
return try await router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self)
}
Expand Down
34 changes: 10 additions & 24 deletions OctoKit/Gist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ open class Gist: Codable {
public extension Octokit {
/**
Fetches the gists of the authenticated user
- parameter session: RequestKitURLSession, defaults to URLSession.sharedSession()
- parameter page: Current page for gist pagination. `1` by default.
- parameter perPage: Number of gists per page. `100` by default.
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func myGists(_ session: RequestKitURLSession = URLSession.shared,
page: String = "1",
func myGists(page: String = "1",
perPage: String = "100",
completion: @escaping (_ response: Result<[Gist], Error>) -> Void) -> URLSessionDataTaskProtocol? {
let router = GistRouter.readAuthenticatedGists(configuration, page, perPage)
Expand All @@ -74,28 +72,25 @@ public extension Octokit {
#if compiler(>=5.5.2) && canImport(_Concurrency)
/**
Fetches the gists of the authenticated user
- parameter session: RequestKitURLSession, defaults to URLSession.sharedSession()
- parameter page: Current page for gist pagination. `1` by default.
- parameter perPage: Number of gists per page. `100` by default.
*/
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func myGists(_ session: RequestKitURLSession = URLSession.shared, page: String = "1", perPage: String = "100") async throws -> [Gist] {
func myGists(page: String = "1", perPage: String = "100") async throws -> [Gist] {
let router = GistRouter.readAuthenticatedGists(configuration, page, perPage)
return try await router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [Gist].self)
}
#endif

/**
Fetches the gists of the specified user
- parameter session: RequestKitURLSession, defaults to URLSession.sharedSession()
- parameter owner: The username who owns the gists.
- parameter page: Current page for gist pagination. `1` by default.
- parameter perPage: Number of gists per page. `100` by default.
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func gists(_ session: RequestKitURLSession = URLSession.shared,
owner: String,
func gists(owner: String,
page: String = "1",
perPage: String = "100",
completion: @escaping (_ response: Result<[Gist], Error>) -> Void) -> URLSessionDataTaskProtocol? {
Expand All @@ -114,26 +109,24 @@ public extension Octokit {
#if compiler(>=5.5.2) && canImport(_Concurrency)
/**
Fetches the gists of the specified user
- parameter session: RequestKitURLSession, defaults to URLSession.sharedSession()
- parameter owner: The username who owns the gists.
- parameter page: Current page for gist pagination. `1` by default.
- parameter perPage: Number of gists per page. `100` by default.
*/
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func gists(_ session: RequestKitURLSession = URLSession.shared, owner: String, page: String = "1", perPage: String = "100") async throws -> [Gist] {
func gists(owner: String, page: String = "1", perPage: String = "100") async throws -> [Gist] {
let router = GistRouter.readGists(configuration, owner, page, perPage)
return try await router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [Gist].self)
}
#endif

/**
Fetches an gist
- parameter session: RequestKitURLSession, defaults to URLSession.sharedSession()
- parameter id: The id of the gist.
- parameter completion: Callback for the outcome of the fetch.
*/
@discardableResult
func gist(_ session: RequestKitURLSession = URLSession.shared, id: String, completion: @escaping (_ response: Result<Gist, Error>) -> Void) -> URLSessionDataTaskProtocol? {
func gist(id: String, completion: @escaping (_ response: Result<Gist, Error>) -> Void) -> URLSessionDataTaskProtocol? {
let router = GistRouter.readGist(configuration, id)
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: Gist.self) { gist, error in
if let error = error {
Expand All @@ -149,28 +142,25 @@ public extension Octokit {
#if compiler(>=5.5.2) && canImport(_Concurrency)
/**
Fetches an gist
- parameter session: RequestKitURLSession, defaults to URLSession.sharedSession()
- parameter id: The id of the gist.
*/
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func gist(_ session: RequestKitURLSession = URLSession.shared, id: String) async throws -> Gist {
func gist(id: String) async throws -> Gist {
let router = GistRouter.readGist(configuration, id)
return try await router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: Gist.self)
}
#endif

/**
Creates an gist with a single file.
- parameter session: RequestKitURLSession, defaults to URLSession.sharedSession()
- parameter description: The description of the gist.
- parameter filename: The name of the file in the gist.
- parameter fileContent: The content of the file in the gist.
- parameter publicAccess: The public/private visability of the gist.
- parameter completion: Callback for the gist that is created.
*/
@discardableResult
func postGistFile(_ session: RequestKitURLSession = URLSession.shared,
description: String,
func postGistFile(description: String,
filename: String,
fileContent: String,
publicAccess: Bool,
Expand All @@ -192,14 +182,13 @@ public extension Octokit {
#if compiler(>=5.5.2) && canImport(_Concurrency)
/**
Creates an gist with a single file.
- parameter session: RequestKitURLSession, defaults to URLSession.sharedSession()
- parameter description: The description of the gist.
- parameter filename: The name of the file in the gist.
- parameter fileContent: The content of the file in the gist.
- parameter publicAccess: The public/private visability of the gist.
*/
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func postGistFile(_ session: RequestKitURLSession = URLSession.shared, description: String, filename: String, fileContent: String, publicAccess: Bool) async throws -> Gist {
func postGistFile(description: String, filename: String, fileContent: String, publicAccess: Bool) async throws -> Gist {
let router = GistRouter.postGistFile(configuration, description, filename, fileContent, publicAccess)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(Time.rfc3339DateFormatter)
Expand All @@ -209,16 +198,14 @@ public extension Octokit {

/**
Edits an gist with a single file.
- parameter session: RequestKitURLSession, defaults to URLSession.sharedSession()
- parameter id: The of the gist to update.
- parameter description: The description of the gist.
- parameter filename: The name of the file in the gist.
- parameter fileContent: The content of the file in the gist.
- parameter completion: Callback for the gist that is created.
*/
@discardableResult
func patchGistFile(_ session: RequestKitURLSession = URLSession.shared,
id: String,
func patchGistFile(id: String,
description: String,
filename: String,
fileContent: String,
Expand All @@ -240,14 +227,13 @@ public extension Octokit {
#if compiler(>=5.5.2) && canImport(_Concurrency)
/**
Edits an gist with a single file.
- parameter session: RequestKitURLSession, defaults to URLSession.sharedSession()
- parameter id: The of the gist to update.
- parameter description: The description of the gist.
- parameter filename: The name of the file in the gist.
- parameter fileContent: The content of the file in the gist.
*/
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func patchGistFile(_ session: RequestKitURLSession = URLSession.shared, id: String, description: String, filename: String, fileContent: String) async throws -> Gist {
func patchGistFile(id: String, description: String, filename: String, fileContent: String) async throws -> Gist {
let router = GistRouter.patchGistFile(configuration, id, description, filename, fileContent)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(Time.rfc3339DateFormatter)
Expand Down
4 changes: 1 addition & 3 deletions OctoKit/Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ import FoundationNetworking
public extension Octokit {
/// Deletes a reference.
/// - Parameters:
/// - session: RequestKitURLSession, defaults to URLSession.shared()
/// - owner: The user or organization that owns the repositories.
/// - repo: The repository on which the reference needs to be deleted.
/// - ref: The reference to delete.
/// - completion: Callback for the outcome of the deletion.
@discardableResult
func deleteReference(_ session: RequestKitURLSession = URLSession.shared,
owner: String,
func deleteReference(owner: String,
repository: String,
ref: String,
completion: @escaping (_ response: Error?) -> Void) -> URLSessionDataTaskProtocol? {
Expand Down
Loading