@@ -74,19 +74,15 @@ public struct URLSessionTransport: ClientTransport {
74
74
/// Creates a new configuration with the provided session.
75
75
/// - Parameter session: The URLSession used for performing HTTP operations.
76
76
/// 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 }
80
78
}
81
79
82
80
/// A set of configuration values used by the transport.
83
81
public var configuration : Configuration
84
82
85
83
/// Creates a new URLSession-based transport.
86
84
/// - 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 }
90
86
91
87
/// Asynchronously sends an HTTP request and returns the response and body.
92
88
///
@@ -97,20 +93,13 @@ public struct URLSessionTransport: ClientTransport {
97
93
/// - operationID: An optional identifier for the operation or request.
98
94
/// - Returns: A tuple containing the HTTP response and an optional HTTP response body.
99
95
/// - 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
+ ) {
106
99
// TODO: https://github.com/apple/swift-openapi-generator/issues/301
107
100
let urlRequest = try await URLRequest ( request, body: body, baseURL: baseURL)
108
101
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)
114
103
}
115
104
116
105
private func invokeSession( _ urlRequest: URLRequest ) async throws -> ( Data , URLResponse ) {
@@ -124,15 +113,11 @@ public struct URLSessionTransport: ClientTransport {
124
113
}
125
114
126
115
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) ) )
130
117
return
131
118
}
132
119
133
- continuation. resume (
134
- with: . success( ( data ?? Data ( ) , response) )
135
- )
120
+ continuation. resume ( with: . success( ( data ?? Data ( ) , response) ) )
136
121
}
137
122
. resume ( )
138
123
}
@@ -153,46 +138,31 @@ internal enum URLSessionTransportError: Error {
153
138
}
154
139
155
140
extension 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
+ ) {
161
144
guard let httpResponse = urlResponse as? HTTPURLResponse else {
162
145
throw URLSessionTransportError . notHTTPResponse ( urlResponse)
163
146
}
164
147
var headerFields = HTTPFields ( )
165
148
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) ,
169
150
let value = headerValue as? String
170
- else {
171
- continue
172
- }
151
+ else { continue }
173
152
headerFields [ name] = value
174
153
}
175
154
let body : HTTPBody ?
176
155
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)
181
158
}
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)
189
160
}
190
161
}
191
162
192
163
extension URLRequest {
193
164
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) ,
196
166
let requestUrlComponents = URLComponents ( string: request. path ?? " " )
197
167
else {
198
168
throw URLSessionTransportError . invalidRequestURL (
@@ -206,11 +176,7 @@ extension URLRequest {
206
176
baseUrlComponents. percentEncodedPath += path
207
177
baseUrlComponents. percentEncodedQuery = requestUrlComponents. percentEncodedQuery
208
178
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)
214
180
}
215
181
self . init ( url: url)
216
182
self . httpMethod = request. method. rawValue
@@ -238,8 +204,7 @@ extension URLSessionTransportError: CustomStringConvertible {
238
204
" Invalid request URL from request path: \( path) , method: \( method) , relative to base URL: \( baseURL. absoluteString) "
239
205
case . notHTTPResponse( let response) :
240
206
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> " ) "
243
208
}
244
209
}
245
210
}
0 commit comments