@@ -74,19 +74,15 @@ public struct URLSessionTransport: ClientTransport {
7474 /// Creates a new configuration with the provided session.
7575 /// - Parameter session: The URLSession used for performing HTTP operations.
7676 /// If none is provided, the system uses the shared URLSession.
77- public init ( session: URLSession = . shared) {
78- self . session = session
79- }
77+ public init ( session: URLSession = . shared) { self . session = session }
8078 }
8179
8280 /// A set of configuration values used by the transport.
8381 public var configuration : Configuration
8482
8583 /// Creates a new URLSession-based transport.
8684 /// - Parameter configuration: A set of configuration values used by the transport.
87- public init ( configuration: Configuration = . init( ) ) {
88- self . configuration = configuration
89- }
85+ public init ( configuration: Configuration = . init( ) ) { self . configuration = configuration }
9086
9187 /// Asynchronously sends an HTTP request and returns the response and body.
9288 ///
@@ -97,20 +93,13 @@ public struct URLSessionTransport: ClientTransport {
9793 /// - operationID: An optional identifier for the operation or request.
9894 /// - Returns: A tuple containing the HTTP response and an optional HTTP response body.
9995 /// - Throws: An error if there is a problem sending the request or processing the response.
100- public func send(
101- _ request: HTTPRequest ,
102- body: HTTPBody ? ,
103- baseURL: URL ,
104- operationID: String
105- ) async throws -> ( HTTPResponse , HTTPBody ? ) {
96+ public func send( _ request: HTTPRequest , body: HTTPBody ? , baseURL: URL , operationID: String ) async throws -> (
97+ HTTPResponse , HTTPBody ?
98+ ) {
10699 // TODO: https://github.com/apple/swift-openapi-generator/issues/301
107100 let urlRequest = try await URLRequest ( request, body: body, baseURL: baseURL)
108101 let ( responseBody, urlResponse) = try await invokeSession ( urlRequest)
109- return try HTTPResponse . response (
110- method: request. method,
111- urlResponse: urlResponse,
112- data: responseBody
113- )
102+ return try HTTPResponse . response ( method: request. method, urlResponse: urlResponse, data: responseBody)
114103 }
115104
116105 private func invokeSession( _ urlRequest: URLRequest ) async throws -> ( Data , URLResponse ) {
@@ -124,15 +113,11 @@ public struct URLSessionTransport: ClientTransport {
124113 }
125114
126115 guard let response else {
127- continuation. resume (
128- with: . failure( URLSessionTransportError . noResponse ( url: urlRequest. url) )
129- )
116+ continuation. resume ( with: . failure( URLSessionTransportError . noResponse ( url: urlRequest. url) ) )
130117 return
131118 }
132119
133- continuation. resume (
134- with: . success( ( data ?? Data ( ) , response) )
135- )
120+ continuation. resume ( with: . success( ( data ?? Data ( ) , response) ) )
136121 }
137122 . resume ( )
138123 }
@@ -153,46 +138,31 @@ internal enum URLSessionTransportError: Error {
153138}
154139
155140extension HTTPResponse {
156- static func response(
157- method: HTTPRequest . Method ,
158- urlResponse: URLResponse ,
159- data: Data
160- ) throws -> ( HTTPResponse , HTTPBody ? ) {
141+ static func response( method: HTTPRequest . Method , urlResponse: URLResponse , data: Data ) throws -> (
142+ HTTPResponse , HTTPBody ?
143+ ) {
161144 guard let httpResponse = urlResponse as? HTTPURLResponse else {
162145 throw URLSessionTransportError . notHTTPResponse ( urlResponse)
163146 }
164147 var headerFields = HTTPFields ( )
165148 for (headerName, headerValue) in httpResponse. allHeaderFields {
166- guard
167- let rawName = headerName as? String ,
168- let name = HTTPField . Name ( rawName) ,
149+ guard let rawName = headerName as? String , let name = HTTPField . Name ( rawName) ,
169150 let value = headerValue as? String
170- else {
171- continue
172- }
151+ else { continue }
173152 headerFields [ name] = value
174153 }
175154 let body : HTTPBody ?
176155 switch method {
177- case . head, . connect, . trace:
178- body = nil
179- default :
180- body = . init( data)
156+ case . head, . connect, . trace: body = nil
157+ default : body = . init( data)
181158 }
182- return (
183- HTTPResponse (
184- status: . init( code: httpResponse. statusCode) ,
185- headerFields: headerFields
186- ) ,
187- body
188- )
159+ return ( HTTPResponse ( status: . init( code: httpResponse. statusCode) , headerFields: headerFields) , body)
189160 }
190161}
191162
192163extension URLRequest {
193164 init ( _ request: HTTPRequest , body: HTTPBody ? , baseURL: URL ) async throws {
194- guard
195- var baseUrlComponents = URLComponents ( string: baseURL. absoluteString) ,
165+ guard var baseUrlComponents = URLComponents ( string: baseURL. absoluteString) ,
196166 let requestUrlComponents = URLComponents ( string: request. path ?? " " )
197167 else {
198168 throw URLSessionTransportError . invalidRequestURL (
@@ -206,11 +176,7 @@ extension URLRequest {
206176 baseUrlComponents. percentEncodedPath += path
207177 baseUrlComponents. percentEncodedQuery = requestUrlComponents. percentEncodedQuery
208178 guard let url = baseUrlComponents. url else {
209- throw URLSessionTransportError . invalidRequestURL (
210- path: path,
211- method: request. method,
212- baseURL: baseURL
213- )
179+ throw URLSessionTransportError . invalidRequestURL ( path: path, method: request. method, baseURL: baseURL)
214180 }
215181 self . init ( url: url)
216182 self . httpMethod = request. method. rawValue
@@ -238,8 +204,7 @@ extension URLSessionTransportError: CustomStringConvertible {
238204 " Invalid request URL from request path: \( path) , method: \( method) , relative to base URL: \( baseURL. absoluteString) "
239205 case . notHTTPResponse( let response) :
240206 return " Received a non-HTTP response, of type: \( String ( describing: type ( of: response) ) ) "
241- case . noResponse( let url) :
242- return " Received a nil response for \( url? . absoluteString ?? " <nil URL> " ) "
207+ case . noResponse( let url) : return " Received a nil response for \( url? . absoluteString ?? " <nil URL> " ) "
243208 }
244209 }
245210}
0 commit comments