@@ -16,6 +16,7 @@ import AsyncHTTPClient
1616import NIOCore
1717import NIOHTTP1
1818import NIOFoundationCompat
19+ import HTTPTypes
1920#if canImport(Darwin)
2021import Foundation
2122#else
@@ -100,15 +101,15 @@ public struct AsyncHTTPClientTransport: ClientTransport {
100101 internal enum Error : Swift . Error , CustomStringConvertible , LocalizedError {
101102
102103 /// Invalid URL composed from base URL and received request.
103- case invalidRequestURL( request: OpenAPIRuntime . Request , baseURL: URL )
104+ case invalidRequestURL( request: HTTPRequest , baseURL: URL )
104105
105106 // MARK: CustomStringConvertible
106107
107108 var description : String {
108109 switch self {
109110 case let . invalidRequestURL( request: request, baseURL: baseURL) :
110111 return
111- " Invalid request URL from request path: \( request. path) , query: \( request . query ?? " <nil> " ) relative to base URL: \( baseURL. absoluteString) "
112+ " Invalid request URL from request path: \( request. path ?? " <nil> " ) relative to base URL: \( baseURL. absoluteString) "
112113 }
113114 }
114115
@@ -150,57 +151,97 @@ public struct AsyncHTTPClientTransport: ClientTransport {
150151 // MARK: ClientTransport
151152
152153 public func send(
153- _ request: OpenAPIRuntime . Request ,
154+ _ request: HTTPRequest ,
155+ body: HTTPBody ? ,
154156 baseURL: URL ,
155157 operationID: String
156- ) async throws -> OpenAPIRuntime . Response {
157- let httpRequest = try Self . convertRequest ( request, baseURL: baseURL)
158+ ) async throws -> ( HTTPResponse , HTTPBody ? ) {
159+ let httpRequest = try Self . convertRequest ( request, body : body , baseURL: baseURL)
158160 let httpResponse = try await invokeSession ( with: httpRequest)
159- let response = try await Self . convertResponse ( httpResponse)
161+ let response = try await Self . convertResponse (
162+ method: request. method,
163+ httpResponse: httpResponse
164+ )
160165 return response
161166 }
162167
163168 // MARK: Internal
164169
165170 /// Converts the shared Request type into URLRequest.
166171 internal static func convertRequest(
167- _ request: OpenAPIRuntime . Request ,
172+ _ request: HTTPRequest ,
173+ body: HTTPBody ? ,
168174 baseURL: URL
169175 ) throws -> HTTPClientRequest {
170- guard var baseUrlComponents = URLComponents ( string: baseURL. absoluteString) else {
176+ guard
177+ var baseUrlComponents = URLComponents ( string: baseURL. absoluteString) ,
178+ let requestUrlComponents = URLComponents ( string: request. path ?? " " )
179+ else {
171180 throw Error . invalidRequestURL ( request: request, baseURL: baseURL)
172181 }
173- baseUrlComponents. percentEncodedPath += request . path
174- baseUrlComponents. percentEncodedQuery = request . query
182+ baseUrlComponents. percentEncodedPath += requestUrlComponents . percentEncodedPath
183+ baseUrlComponents. percentEncodedQuery = requestUrlComponents . percentEncodedQuery
175184 guard let url = baseUrlComponents. url else {
176185 throw Error . invalidRequestURL ( request: request, baseURL: baseURL)
177186 }
178187 var clientRequest = HTTPClientRequest ( url: url. absoluteString)
179188 clientRequest. method = request. method. asHTTPMethod
180189 for header in request. headerFields {
181- clientRequest. headers. add ( name: header. name. lowercased ( ) , value: header. value)
190+ clientRequest. headers. add ( name: header. name. canonicalName , value: header. value)
182191 }
183- if let body = request. body {
184- clientRequest. body = . bytes( body)
192+ if let body {
193+ let length : HTTPClientRequest . Body . Length
194+ switch body. length {
195+ case . unknown:
196+ length = . unknown
197+ case . known( let count) :
198+ length = . known( count)
199+ }
200+ clientRequest. body = . stream(
201+ body. map { . init( bytes: $0) } ,
202+ length: length
203+ )
185204 }
186205 return clientRequest
187206 }
188207
189208 /// Converts the received URLResponse into the shared Response.
190209 internal static func convertResponse(
191- _ httpResponse: HTTPClientResponse
192- ) async throws -> OpenAPIRuntime . Response {
193- let headerFields : [ OpenAPIRuntime . HeaderField ] = httpResponse
194- . headers
195- . map { . init( name: $0, value: $1) }
196- let body = try await httpResponse. body. collect ( upTo: . max)
197- let bodyData = Data ( buffer: body, byteTransferStrategy: . noCopy)
198- let response = OpenAPIRuntime . Response (
199- statusCode: Int ( httpResponse. status. code) ,
200- headerFields: headerFields,
201- body: bodyData
210+ method: HTTPRequest . Method ,
211+ httpResponse: HTTPClientResponse
212+ ) async throws -> ( HTTPResponse , HTTPBody ? ) {
213+
214+ var headerFields : HTTPFields = [ : ]
215+ for header in httpResponse. headers {
216+ headerFields [ . init( header. name) !] = header. value
217+ }
218+
219+ let length : HTTPBody . Length
220+ if let lengthHeaderString = headerFields [ . contentLength] ,
221+ let lengthHeader = Int ( lengthHeaderString)
222+ {
223+ length = . known( lengthHeader)
224+ } else {
225+ length = . unknown
226+ }
227+
228+ let body : HTTPBody ?
229+ switch method {
230+ case . head, . connect, . trace:
231+ body = nil
232+ default :
233+ body = HTTPBody (
234+ httpResponse. body. map { $0. readableBytesView } ,
235+ length: length,
236+ iterationBehavior: . single
237+ )
238+ }
239+
240+ let response = HTTPResponse (
241+ status: . init( code: Int ( httpResponse. status. code) ) ,
242+ headerFields: headerFields
202243 )
203- return response
244+ return ( response, body )
204245 }
205246
206247 // MARK: Private
@@ -215,7 +256,7 @@ public struct AsyncHTTPClientTransport: ClientTransport {
215256 }
216257}
217258
218- extension OpenAPIRuntime . HTTPMethod {
259+ extension HTTPTypes . HTTPRequest . Method {
219260 var asHTTPMethod : NIOHTTP1 . HTTPMethod {
220261 switch self {
221262 case . get:
0 commit comments