@@ -115,9 +115,7 @@ public struct AsyncHTTPClientTransport: ClientTransport {
115115
116116 // MARK: LocalizedError
117117
118- var errorDescription : String ? {
119- description
120- }
118+ var errorDescription : String ? { description }
121119 }
122120
123121 /// A set of configuration values used by the transport.
@@ -130,21 +128,15 @@ public struct AsyncHTTPClientTransport: ClientTransport {
130128 /// - Parameters:
131129 /// - configuration: A set of configuration values used by the transport.
132130 /// - requestSender: The underlying request sender.
133- internal init (
134- configuration: Configuration ,
135- requestSender: any HTTPRequestSending
136- ) {
131+ internal init ( configuration: Configuration , requestSender: any HTTPRequestSending ) {
137132 self . configuration = configuration
138133 self . requestSender = requestSender
139134 }
140135
141136 /// Creates a new transport.
142137 /// - Parameter configuration: A set of configuration values used by the transport.
143138 public init ( configuration: Configuration ) {
144- self . init (
145- configuration: configuration,
146- requestSender: AsyncHTTPRequestSender ( )
147- )
139+ self . init ( configuration: configuration, requestSender: AsyncHTTPRequestSender ( ) )
148140 }
149141
150142 // MARK: ClientTransport
@@ -159,40 +151,27 @@ public struct AsyncHTTPClientTransport: ClientTransport {
159151 ///
160152 /// - Returns: A tuple containing the HTTP response and an optional HTTP body in the response.
161153 /// - 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+ ) {
168157 let httpRequest = try Self . convertRequest ( request, body: body, baseURL: baseURL)
169158 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)
174160 return response
175161 }
176162
177163 // MARK: Internal
178164
179165 /// 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) ,
187170 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) }
191172 baseUrlComponents. percentEncodedPath += requestUrlComponents. percentEncodedPath
192173 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) }
196175 var clientRequest = HTTPClientRequest ( url: url. absoluteString)
197176 clientRequest. method = request. method. asHTTPMethod
198177 for header in request. headerFields {
@@ -201,114 +180,73 @@ public struct AsyncHTTPClientTransport: ClientTransport {
201180 if let body {
202181 let length : HTTPClientRequest . Body . Length
203182 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)
208185 }
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)
213187 }
214188 return clientRequest
215189 }
216190
217191 /// 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+ ) {
222195
223196 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 }
227198
228199 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) {
232201 length = . known( lengthHeader)
233202 } else {
234203 length = . unknown
235204 }
236205
237206 let body : HTTPBody ?
238207 switch method {
239- case . head, . connect, . trace:
240- body = nil
208+ case . head, . connect, . trace: body = nil
241209 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)
247211 }
248212
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)
253214 return ( response, body)
254215 }
255216
256217 // MARK: Private
257218
258219 /// Makes the underlying HTTP call.
259220 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)
265222 }
266223}
267224
268225extension HTTPTypes . HTTPRequest . Method {
269226 var asHTTPMethod : NIOHTTP1 . HTTPMethod {
270227 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)
289237 }
290238 }
291239}
292240
293241/// A type that performs HTTP operations using the HTTP client.
294242internal 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
300245}
301246
302247/// Performs HTTP calls using AsyncHTTPClient
303248internal 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) }
314252}
0 commit comments