@@ -35,7 +35,7 @@ extension Networking {
3535
3636 do {
3737 if let fakeRequest = try FakeRequest . find ( ofType: requestType, forPath: path, in: fakeRequests) {
38- let fakeContext = makeContext ( id: requestID, method : requestType. rawValue , url: try ? composedURL ( with: path) , headers: headerFields ?? [ : ] )
38+ let fakeContext = RequestContext ( id: requestID, requestType : requestType, url: try ? composedURL ( with: path) , headers: headerFields)
3939 context = fakeContext
4040 emit ( . started( fakeContext) )
4141 let ( fakeResult, fakeStatus, fakeBytes) : ( Result < T , NetworkingError > , Int , Int ) = try await handleFakeRequest ( fakeRequest, path: path, requestType: requestType)
@@ -44,7 +44,7 @@ extension Networking {
4444 byteCount = fakeBytes
4545 } else {
4646 let request = try createRequest ( path: path, requestType: requestType, body: body, query: query)
47- let requestContext = makeContext ( id: requestID, method : requestType. rawValue , url: request. url, headers: request. allHTTPHeaderFields ?? [ : ] )
47+ let requestContext = RequestContext ( id: requestID, requestType : requestType, url: request. url, headers: request. allHTTPHeaderFields)
4848 context = requestContext
4949 emit ( . started( requestContext) )
5050
@@ -61,7 +61,7 @@ extension Networking {
6161 result = handleResponse ( responseData: exchange. data, response: exchange. response, path: path)
6262 statusCode = exchange. response. statusCode
6363 byteCount = exchange. data. count
64- responseMetadata = makeResponseMetadata ( exchange. response, body: exchange. data)
64+ responseMetadata = ResponseMetadata ( response : exchange. response, body: exchange. data)
6565 } else {
6666 let collector = MetricsCollector ( )
6767 let exchange = try await perform ( request, collector: collector)
@@ -82,15 +82,15 @@ extension Networking {
8282 byteCount = responseData. count
8383 metrics = collector. metrics. flatMap ( TransactionMetrics . init)
8484 if let httpResponse = response as? HTTPURLResponse {
85- responseMetadata = makeResponseMetadata ( httpResponse, body: responseData)
85+ responseMetadata = ResponseMetadata ( response : httpResponse, body: responseData)
8686 }
8787 }
8888 }
8989 } catch {
9090 // A failure before the request context was built (e.g. URL building) still emits a paired
9191 // .started/.completed so observers see every request.
9292 if context == nil {
93- let errorContext = makeContext ( id: requestID, method : requestType. rawValue , url: nil , headers: headerFields ?? [ : ] )
93+ let errorContext = RequestContext ( id: requestID, requestType : requestType, url: nil , headers: headerFields)
9494 context = errorContext
9595 emit ( . started( errorContext) )
9696 }
@@ -199,15 +199,11 @@ extension Networking {
199199 // body encoding), so observers don't miss it.
200200 func emitPreflightFailure< T: Decodable > ( _ requestType: RequestType , path: String , error: NetworkingError ) -> Result < T , NetworkingError > {
201201 let requestID = UUID ( )
202- let context = makeContext ( id: requestID, method : requestType. rawValue , url: try ? composedURL ( with: path) , headers: headerFields ?? [ : ] )
202+ let context = RequestContext ( id: requestID, requestType : requestType, url: try ? composedURL ( with: path) , headers: headerFields)
203203 emit ( . started( context) )
204204 return complete ( . failure( error) , context: context, statusCode: nil , byteCount: 0 , metrics: nil , duration: . zero)
205205 }
206206
207- func makeContext( id: UUID , method: String , url: URL ? , headers: [ String : String ] ) -> RequestContext {
208- RequestContext ( id: id, method: method, url: url, headers: headers)
209- }
210-
211207 // Persists status code and headers alongside the body so a cache hit reproduces real metadata.
212208 private struct CachedResponse : Codable {
213209 let statusCode : Int
@@ -274,7 +270,6 @@ extension Networking {
274270 var request = URLRequest (
275271 url: url,
276272 requestType: requestType,
277- path: path,
278273 contentType: body. contentType ( boundary: boundary) ,
279274 responseType: . json,
280275 authorizationHeaderValue: authorizationHeaderValue,
@@ -329,7 +324,7 @@ extension Networking {
329324 case . cancelled:
330325 return . failure( . cancelled)
331326 case . redirection, . clientError, . serverError, . unknown:
332- let error = HTTPError ( statusCode: statusCode, metadata: makeResponseMetadata ( httpResponse, body: responseData) )
327+ let error = HTTPError ( statusCode: statusCode, metadata: ResponseMetadata ( response : httpResponse, body: responseData) )
333328 return . failure( . http( error) )
334329 }
335330 }
@@ -347,7 +342,7 @@ extension Networking {
347342 let networkingJSON = JSONResponse ( statusCode: httpResponse. statusCode, headers: headers, body: body)
348343 return . success( networkingJSON as! T )
349344 } catch let error as DecodingError {
350- return . failure( . decoding( error, makeResponseMetadata ( httpResponse, body: responseData) ) )
345+ return . failure( . decoding( error, ResponseMetadata ( response : httpResponse, body: responseData) ) )
351346 } catch {
352347 return . failure( . invalidResponse) // JSONDecoder only throws DecodingError; unreachable.
353348 }
@@ -357,17 +352,13 @@ extension Networking {
357352 do {
358353 return . success( try decoder. decode ( T . self, from: responseData) )
359354 } catch let error as DecodingError {
360- return . failure( . decoding( error, makeResponseMetadata ( httpResponse, body: responseData) ) )
355+ return . failure( . decoding( error, ResponseMetadata ( response : httpResponse, body: responseData) ) )
361356 } catch {
362357 return . failure( . invalidResponse) // JSONDecoder only throws DecodingError; unreachable.
363358 }
364359 }
365360 }
366361
367- private func makeResponseMetadata( _ httpResponse: HTTPURLResponse , body: Data ) -> ResponseMetadata {
368- ResponseMetadata ( response: httpResponse, body: body)
369- }
370-
371362 private func bodySnippet( from data: Data , limit: Int = 512 ) -> String ? {
372363 ResponseMetadata . bodySnippet ( from: data, limit: limit)
373364 }
0 commit comments