Skip to content

Commit

Permalink
Move URLSession to Octokit instead of having it in every method
Browse files Browse the repository at this point in the history
  • Loading branch information
pietbrauer committed May 21, 2023
1 parent 0c4076a commit 1a54461
Show file tree
Hide file tree
Showing 28 changed files with 267 additions and 414 deletions.
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

0 comments on commit 1a54461

Please sign in to comment.