@@ -115,9 +115,7 @@ public struct AsyncHTTPClientTransport: ClientTransport {
115
115
116
116
// MARK: LocalizedError
117
117
118
- var errorDescription : String ? {
119
- description
120
- }
118
+ var errorDescription : String ? { description }
121
119
}
122
120
123
121
/// A set of configuration values used by the transport.
@@ -130,21 +128,15 @@ public struct AsyncHTTPClientTransport: ClientTransport {
130
128
/// - Parameters:
131
129
/// - configuration: A set of configuration values used by the transport.
132
130
/// - requestSender: The underlying request sender.
133
- internal init (
134
- configuration: Configuration ,
135
- requestSender: any HTTPRequestSending
136
- ) {
131
+ internal init ( configuration: Configuration , requestSender: any HTTPRequestSending ) {
137
132
self . configuration = configuration
138
133
self . requestSender = requestSender
139
134
}
140
135
141
136
/// Creates a new transport.
142
137
/// - Parameter configuration: A set of configuration values used by the transport.
143
138
public init ( configuration: Configuration ) {
144
- self . init (
145
- configuration: configuration,
146
- requestSender: AsyncHTTPRequestSender ( )
147
- )
139
+ self . init ( configuration: configuration, requestSender: AsyncHTTPRequestSender ( ) )
148
140
}
149
141
150
142
// MARK: ClientTransport
@@ -159,40 +151,27 @@ public struct AsyncHTTPClientTransport: ClientTransport {
159
151
///
160
152
/// - Returns: A tuple containing the HTTP response and an optional HTTP body in the response.
161
153
/// - Throws: An error if the request or response handling encounters any issues.
162
- public func send(
163
- _ request: HTTPRequest ,
164
- body: HTTPBody ? ,
165
- baseURL: URL ,
166
- operationID: String
167
- ) async throws -> ( HTTPResponse , HTTPBody ? ) {
154
+ public func send( _ request: HTTPRequest , body: HTTPBody ? , baseURL: URL , operationID: String ) async throws -> (
155
+ HTTPResponse , HTTPBody ?
156
+ ) {
168
157
let httpRequest = try Self . convertRequest ( request, body: body, baseURL: baseURL)
169
158
let httpResponse = try await invokeSession ( with: httpRequest)
170
- let response = try await Self . convertResponse (
171
- method: request. method,
172
- httpResponse: httpResponse
173
- )
159
+ let response = try await Self . convertResponse ( method: request. method, httpResponse: httpResponse)
174
160
return response
175
161
}
176
162
177
163
// MARK: Internal
178
164
179
165
/// Converts the shared Request type into URLRequest.
180
- internal static func convertRequest(
181
- _ request: HTTPRequest ,
182
- body: HTTPBody ? ,
183
- baseURL: URL
184
- ) throws -> HTTPClientRequest {
185
- guard
186
- var baseUrlComponents = URLComponents ( string: baseURL. absoluteString) ,
166
+ internal static func convertRequest( _ request: HTTPRequest , body: HTTPBody ? , baseURL: URL ) throws
167
+ -> HTTPClientRequest
168
+ {
169
+ guard var baseUrlComponents = URLComponents ( string: baseURL. absoluteString) ,
187
170
let requestUrlComponents = URLComponents ( string: request. path ?? " " )
188
- else {
189
- throw Error . invalidRequestURL ( request: request, baseURL: baseURL)
190
- }
171
+ else { throw Error . invalidRequestURL ( request: request, baseURL: baseURL) }
191
172
baseUrlComponents. percentEncodedPath += requestUrlComponents. percentEncodedPath
192
173
baseUrlComponents. percentEncodedQuery = requestUrlComponents. percentEncodedQuery
193
- guard let url = baseUrlComponents. url else {
194
- throw Error . invalidRequestURL ( request: request, baseURL: baseURL)
195
- }
174
+ guard let url = baseUrlComponents. url else { throw Error . invalidRequestURL ( request: request, baseURL: baseURL) }
196
175
var clientRequest = HTTPClientRequest ( url: url. absoluteString)
197
176
clientRequest. method = request. method. asHTTPMethod
198
177
for header in request. headerFields {
@@ -201,114 +180,73 @@ public struct AsyncHTTPClientTransport: ClientTransport {
201
180
if let body {
202
181
let length : HTTPClientRequest . Body . Length
203
182
switch body. length {
204
- case . unknown:
205
- length = . unknown
206
- case . known( let count) :
207
- length = . known( count)
183
+ case . unknown: length = . unknown
184
+ case . known( let count) : length = . known( count)
208
185
}
209
- clientRequest. body = . stream(
210
- body. map { . init( bytes: $0) } ,
211
- length: length
212
- )
186
+ clientRequest. body = . stream( body. map { . init( bytes: $0) } , length: length)
213
187
}
214
188
return clientRequest
215
189
}
216
190
217
191
/// Converts the received URLResponse into the shared Response.
218
- internal static func convertResponse(
219
- method: HTTPRequest . Method ,
220
- httpResponse: HTTPClientResponse
221
- ) async throws -> ( HTTPResponse , HTTPBody ? ) {
192
+ internal static func convertResponse( method: HTTPRequest . Method , httpResponse: HTTPClientResponse ) async throws -> (
193
+ HTTPResponse , HTTPBody ?
194
+ ) {
222
195
223
196
var headerFields : HTTPFields = [ : ]
224
- for header in httpResponse. headers {
225
- headerFields [ . init( header. name) !] = header. value
226
- }
197
+ for header in httpResponse. headers { headerFields [ . init( header. name) !] = header. value }
227
198
228
199
let length : HTTPBody . Length
229
- if let lengthHeaderString = headerFields [ . contentLength] ,
230
- let lengthHeader = Int ( lengthHeaderString)
231
- {
200
+ if let lengthHeaderString = headerFields [ . contentLength] , let lengthHeader = Int ( lengthHeaderString) {
232
201
length = . known( lengthHeader)
233
202
} else {
234
203
length = . unknown
235
204
}
236
205
237
206
let body : HTTPBody ?
238
207
switch method {
239
- case . head, . connect, . trace:
240
- body = nil
208
+ case . head, . connect, . trace: body = nil
241
209
default :
242
- body = HTTPBody (
243
- httpResponse. body. map { $0. readableBytesView } ,
244
- length: length,
245
- iterationBehavior: . single
246
- )
210
+ body = HTTPBody ( httpResponse. body. map { $0. readableBytesView } , length: length, iterationBehavior: . single)
247
211
}
248
212
249
- let response = HTTPResponse (
250
- status: . init( code: Int ( httpResponse. status. code) ) ,
251
- headerFields: headerFields
252
- )
213
+ let response = HTTPResponse ( status: . init( code: Int ( httpResponse. status. code) ) , headerFields: headerFields)
253
214
return ( response, body)
254
215
}
255
216
256
217
// MARK: Private
257
218
258
219
/// Makes the underlying HTTP call.
259
220
private func invokeSession( with request: Request ) async throws -> Response {
260
- try await requestSender. send (
261
- request: request,
262
- with: configuration. client,
263
- timeout: configuration. timeout
264
- )
221
+ try await requestSender. send ( request: request, with: configuration. client, timeout: configuration. timeout)
265
222
}
266
223
}
267
224
268
225
extension HTTPTypes . HTTPRequest . Method {
269
226
var asHTTPMethod : NIOHTTP1 . HTTPMethod {
270
227
switch self {
271
- case . get:
272
- return . GET
273
- case . put:
274
- return . PUT
275
- case . post:
276
- return . POST
277
- case . delete:
278
- return . DELETE
279
- case . options:
280
- return . OPTIONS
281
- case . head:
282
- return . HEAD
283
- case . patch:
284
- return . PATCH
285
- case . trace:
286
- return . TRACE
287
- default :
288
- return . RAW( value: rawValue)
228
+ case . get: return . GET
229
+ case . put: return . PUT
230
+ case . post: return . POST
231
+ case . delete: return . DELETE
232
+ case . options: return . OPTIONS
233
+ case . head: return . HEAD
234
+ case . patch: return . PATCH
235
+ case . trace: return . TRACE
236
+ default : return . RAW( value: rawValue)
289
237
}
290
238
}
291
239
}
292
240
293
241
/// A type that performs HTTP operations using the HTTP client.
294
242
internal protocol HTTPRequestSending : Sendable {
295
- func send(
296
- request: AsyncHTTPClientTransport . Request ,
297
- with client: HTTPClient ,
298
- timeout: TimeAmount
299
- ) async throws -> AsyncHTTPClientTransport . Response
243
+ func send( request: AsyncHTTPClientTransport . Request , with client: HTTPClient , timeout: TimeAmount ) async throws
244
+ -> AsyncHTTPClientTransport . Response
300
245
}
301
246
302
247
/// Performs HTTP calls using AsyncHTTPClient
303
248
internal struct AsyncHTTPRequestSender : HTTPRequestSending {
304
- func send(
305
- request: AsyncHTTPClientTransport . Request ,
306
- with client: AsyncHTTPClient . HTTPClient ,
307
- timeout: TimeAmount
308
- ) async throws -> AsyncHTTPClientTransport . Response {
309
- try await client. execute (
310
- request,
311
- timeout: timeout
312
- )
313
- }
249
+ func send( request: AsyncHTTPClientTransport . Request , with client: AsyncHTTPClient . HTTPClient , timeout: TimeAmount )
250
+ async throws -> AsyncHTTPClientTransport . Response
251
+ { try await client. execute ( request, timeout: timeout) }
314
252
}
0 commit comments