Skip to content

Commit 6403d4f

Browse files
author
Mihai Arosoaie
committed
Extend User api
- allow fetching user by its unique id
1 parent cd108b3 commit 6403d4f

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

OctoKit/User.swift

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,45 @@ open class User: Codable {
169169
// MARK: request
170170

171171
public extension Octokit {
172+
/**
173+
Fetches a user or organization
174+
- parameter id: The id of the user or organization.
175+
- parameter completion: Callback for the outcome of the fetch.
176+
*/
177+
@discardableResult
178+
func user(id: Int, completion: @escaping (_ response: Result<User, Error>) -> Void) -> URLSessionDataTaskProtocol? {
179+
let router = UserRouter.readUserById(id, configuration)
180+
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: User.self) { user, error in
181+
if let error = error {
182+
completion(.failure(error))
183+
} else {
184+
if let user = user {
185+
completion(.success(user))
186+
}
187+
}
188+
}
189+
}
190+
191+
#if compiler(>=5.5.2) && canImport(_Concurrency)
192+
/**
193+
Fetches a user or organization
194+
- parameter id: The identifier of the user or organization.
195+
*/
196+
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
197+
func user(id: Int) async throws -> User {
198+
let router = UserRouter.readUserById(id, configuration)
199+
return try await router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: User.self)
200+
}
201+
#endif
202+
172203
/**
173204
Fetches a user or organization
174205
- parameter name: The name of the user or organization.
175206
- parameter completion: Callback for the outcome of the fetch.
176207
*/
177208
@discardableResult
178209
func user(name: String, completion: @escaping (_ response: Result<User, Error>) -> Void) -> URLSessionDataTaskProtocol? {
179-
let router = UserRouter.readUser(name, configuration)
210+
let router = UserRouter.readUserByName(name, configuration)
180211
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: User.self) { user, error in
181212
if let error = error {
182213
completion(.failure(error))
@@ -195,7 +226,7 @@ public extension Octokit {
195226
*/
196227
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
197228
func user(name: String) async throws -> User {
198-
let router = UserRouter.readUser(name, configuration)
229+
let router = UserRouter.readUserByName(name, configuration)
199230
return try await router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: User.self)
200231
}
201232
#endif
@@ -234,12 +265,14 @@ public extension Octokit {
234265

235266
enum UserRouter: Router {
236267
case readAuthenticatedUser(Configuration)
237-
case readUser(String, Configuration)
268+
case readUserById(Int, Configuration)
269+
case readUserByName(String, Configuration)
238270

239271
var configuration: Configuration {
240272
switch self {
241273
case let .readAuthenticatedUser(config): return config
242-
case let .readUser(_, config): return config
274+
case let .readUserById(_, config): return config
275+
case let .readUserByName(_, config): return config
243276
}
244277
}
245278

@@ -255,7 +288,9 @@ enum UserRouter: Router {
255288
switch self {
256289
case .readAuthenticatedUser:
257290
return "user"
258-
case let .readUser(username, _):
291+
case let .readUserById(id, _):
292+
return "user/\(id)"
293+
case let .readUserByName(username, _):
259294
return "users/\(username)"
260295
}
261296
}

0 commit comments

Comments
 (0)