@@ -169,14 +169,45 @@ open class User: Codable {
169
169
// MARK: request
170
170
171
171
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
+
172
203
/**
173
204
Fetches a user or organization
174
205
- parameter name: The name of the user or organization.
175
206
- parameter completion: Callback for the outcome of the fetch.
176
207
*/
177
208
@discardableResult
178
209
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)
180
211
return router. load ( session, dateDecodingStrategy: . formatted( Time . rfc3339DateFormatter) , expectedResultType: User . self) { user, error in
181
212
if let error = error {
182
213
completion ( . failure( error) )
@@ -195,7 +226,7 @@ public extension Octokit {
195
226
*/
196
227
@available ( macOS 12 . 0 , iOS 15 . 0 , tvOS 15 . 0 , watchOS 8 . 0 , * )
197
228
func user( name: String ) async throws -> User {
198
- let router = UserRouter . readUser ( name, configuration)
229
+ let router = UserRouter . readUserByName ( name, configuration)
199
230
return try await router. load ( session, dateDecodingStrategy: . formatted( Time . rfc3339DateFormatter) , expectedResultType: User . self)
200
231
}
201
232
#endif
@@ -234,12 +265,14 @@ public extension Octokit {
234
265
235
266
enum UserRouter : Router {
236
267
case readAuthenticatedUser( Configuration )
237
- case readUser( String , Configuration )
268
+ case readUserById( Int , Configuration )
269
+ case readUserByName( String , Configuration )
238
270
239
271
var configuration : Configuration {
240
272
switch self {
241
273
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
243
276
}
244
277
}
245
278
@@ -255,7 +288,9 @@ enum UserRouter: Router {
255
288
switch self {
256
289
case . readAuthenticatedUser:
257
290
return " user "
258
- case let . readUser( username, _) :
291
+ case let . readUserById( id, _) :
292
+ return " user/ \( id) "
293
+ case let . readUserByName( username, _) :
259
294
return " users/ \( username) "
260
295
}
261
296
}
0 commit comments