@@ -24,9 +24,19 @@ import Glibc
24
24
import CRT
25
25
#endif
26
26
27
+ public protocol HTTPClientProtocol {
28
+ func execute( _ request: HTTPClientRequest , callback: @escaping ( Result < HTTPClientResponse , Error > ) -> Void )
29
+ }
30
+
31
+ public enum HTTPClientError : Error , Equatable {
32
+ case invalidResponse
33
+ case badResponseStatusCode( Int )
34
+ case circuitBreakerTriggered
35
+ }
36
+
27
37
// MARK: - HTTPClient
28
38
29
- public struct HTTPClient {
39
+ public struct HTTPClient : HTTPClientProtocol {
30
40
public typealias Configuration = HTTPClientConfiguration
31
41
public typealias Request = HTTPClientRequest
32
42
public typealias Response = HTTPClientResponse
@@ -168,33 +178,29 @@ public struct HTTPClient {
168
178
}
169
179
}
170
180
171
- extension HTTPClient {
172
- public func head( _ url: URL , headers: HTTPClientHeaders = . init( ) , options: Request . Options = . init( ) , callback: @escaping ( Result < Response , Error > ) -> Void ) {
181
+ public extension HTTPClient {
182
+ func head( _ url: URL , headers: HTTPClientHeaders = . init( ) , options: Request . Options = . init( ) , callback: @escaping ( Result < Response , Error > ) -> Void ) {
173
183
self . execute ( Request ( method: . head, url: url, headers: headers, body: nil , options: options) , callback: callback)
174
184
}
175
185
176
- public func get( _ url: URL , headers: HTTPClientHeaders = . init( ) , options: Request . Options = . init( ) , callback: @escaping ( Result < Response , Error > ) -> Void ) {
186
+ func get( _ url: URL , headers: HTTPClientHeaders = . init( ) , options: Request . Options = . init( ) , callback: @escaping ( Result < Response , Error > ) -> Void ) {
177
187
self . execute ( Request ( method: . get, url: url, headers: headers, body: nil , options: options) , callback: callback)
178
188
}
179
189
180
- public func put( _ url: URL , body: Data ? , headers: HTTPClientHeaders = . init( ) , options: Request . Options = . init( ) , callback: @escaping ( Result < Response , Error > ) -> Void ) {
190
+ func put( _ url: URL , body: Data ? , headers: HTTPClientHeaders = . init( ) , options: Request . Options = . init( ) , callback: @escaping ( Result < Response , Error > ) -> Void ) {
181
191
self . execute ( Request ( method: . put, url: url, headers: headers, body: body, options: options) , callback: callback)
182
192
}
183
193
184
- public func post( _ url: URL , body: Data ? , headers: HTTPClientHeaders = . init( ) , options: Request . Options = . init( ) , callback: @escaping ( Result < Response , Error > ) -> Void ) {
194
+ func post( _ url: URL , body: Data ? , headers: HTTPClientHeaders = . init( ) , options: Request . Options = . init( ) , callback: @escaping ( Result < Response , Error > ) -> Void ) {
185
195
self . execute ( Request ( method: . post, url: url, headers: headers, body: body, options: options) , callback: callback)
186
196
}
187
197
188
- public func delete( _ url: URL , headers: HTTPClientHeaders = . init( ) , options: Request . Options = . init( ) , callback: @escaping ( Result < Response , Error > ) -> Void ) {
198
+ func delete( _ url: URL , headers: HTTPClientHeaders = . init( ) , options: Request . Options = . init( ) , callback: @escaping ( Result < Response , Error > ) -> Void ) {
189
199
self . execute ( Request ( method: . delete, url: url, headers: headers, body: nil , options: options) , callback: callback)
190
200
}
191
201
}
192
202
193
- extension HTTPClientResponse {
194
- public func decodeBody< T: Decodable > ( _ type: T . Type , using decoder: JSONDecoder = . init( ) ) throws -> T ? {
195
- try self . body. flatMap { try decoder. decode ( type, from: $0) }
196
- }
197
- }
203
+ // MARK: - HTTPClientConfiguration
198
204
199
205
public struct HTTPClientConfiguration {
200
206
public var requestHeaders : HTTPClientHeaders ?
@@ -220,6 +226,8 @@ public enum HTTPClientCircuitBreakerStrategy {
220
226
case hostErrors( maxErrors: Int , age: DispatchTimeInterval )
221
227
}
222
228
229
+ // MARK: - HTTPClientRequest
230
+
223
231
public struct HTTPClientRequest {
224
232
public let method : Method
225
233
public let url : URL
@@ -231,7 +239,8 @@ public struct HTTPClientRequest {
231
239
url: URL ,
232
240
headers: HTTPClientHeaders = . init( ) ,
233
241
body: Data ? = nil ,
234
- options: Options = . init( ) ) {
242
+ options: Options = . init( ) )
243
+ {
235
244
self . method = method
236
245
self . url = url
237
246
self . headers = headers
@@ -266,6 +275,8 @@ public struct HTTPClientRequest {
266
275
}
267
276
}
268
277
278
+ // MARK: - HTTPClientResponse
279
+
269
280
public struct HTTPClientResponse {
270
281
public let statusCode : Int
271
282
public let statusText : String ?
@@ -275,15 +286,22 @@ public struct HTTPClientResponse {
275
286
public init ( statusCode: Int ,
276
287
statusText: String ? = nil ,
277
288
headers: HTTPClientHeaders = . init( ) ,
278
- body: Data ? = nil ) {
289
+ body: Data ? = nil )
290
+ {
279
291
self . statusCode = statusCode
280
292
self . statusText = statusText
281
293
self . headers = headers
282
294
self . body = body
283
295
}
296
+
297
+ public func decodeBody< T: Decodable > ( _ type: T . Type , using decoder: JSONDecoder = . init( ) ) throws -> T ? {
298
+ try self . body. flatMap { try decoder. decode ( type, from: $0) }
299
+ }
284
300
}
285
301
286
- public struct HTTPClientHeaders : Sequence , Equatable {
302
+ // MARK: - HTTPClientHeaders
303
+
304
+ public struct HTTPClientHeaders {
287
305
private var items : [ Item ]
288
306
private var headers : [ String : [ String ] ]
289
307
@@ -336,10 +354,6 @@ public struct HTTPClientHeaders: Sequence, Equatable {
336
354
self . headers [ name. lowercased ( ) ] ?? [ ]
337
355
}
338
356
339
- public func makeIterator( ) -> IndexingIterator < [ Item ] > {
340
- return self . items. makeIterator ( )
341
- }
342
-
343
357
public struct Item : Equatable {
344
358
let name : String
345
359
let value : String
@@ -349,14 +363,22 @@ public struct HTTPClientHeaders: Sequence, Equatable {
349
363
self . value = value
350
364
}
351
365
}
366
+ }
352
367
368
+ extension HTTPClientHeaders : Sequence {
369
+ public func makeIterator( ) -> IndexingIterator < [ Item ] > {
370
+ return self . items. makeIterator ( )
371
+ }
372
+ }
373
+
374
+ extension HTTPClientHeaders : Equatable {
353
375
public static func == ( lhs: HTTPClientHeaders , rhs: HTTPClientHeaders ) -> Bool {
354
376
return lhs. headers == rhs. headers
355
377
}
356
378
}
357
379
358
- public enum HTTPClientError : Error , Equatable {
359
- case invalidResponse
360
- case badResponseStatusCode ( Int )
361
- case circuitBreakerTriggered
380
+ extension HTTPClientHeaders : ExpressibleByDictionaryLiteral {
381
+ public init ( dictionaryLiteral elements : ( String , String ) ... ) {
382
+ self . init ( elements . map ( Item . init ) )
383
+ }
362
384
}
0 commit comments