@@ -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, method: requestType. rawValue, 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, method: requestType. rawValue, 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, method: requestType. rawValue, 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, method: requestType. rawValue, 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
@@ -328,7 +324,7 @@ extension Networking {
328324 case . cancelled:
329325 return . failure( . cancelled)
330326 case . redirection, . clientError, . serverError, . unknown:
331- let error = HTTPError ( statusCode: statusCode, metadata: makeResponseMetadata ( httpResponse, body: responseData) )
327+ let error = HTTPError ( statusCode: statusCode, metadata: ResponseMetadata ( response : httpResponse, body: responseData) )
332328 return . failure( . http( error) )
333329 }
334330 }
@@ -346,7 +342,7 @@ extension Networking {
346342 let networkingJSON = JSONResponse ( statusCode: httpResponse. statusCode, headers: headers, body: body)
347343 return . success( networkingJSON as! T )
348344 } catch let error as DecodingError {
349- return . failure( . decoding( error, makeResponseMetadata ( httpResponse, body: responseData) ) )
345+ return . failure( . decoding( error, ResponseMetadata ( response : httpResponse, body: responseData) ) )
350346 } catch {
351347 return . failure( . invalidResponse) // JSONDecoder only throws DecodingError; unreachable.
352348 }
@@ -356,17 +352,13 @@ extension Networking {
356352 do {
357353 return . success( try decoder. decode ( T . self, from: responseData) )
358354 } catch let error as DecodingError {
359- return . failure( . decoding( error, makeResponseMetadata ( httpResponse, body: responseData) ) )
355+ return . failure( . decoding( error, ResponseMetadata ( response : httpResponse, body: responseData) ) )
360356 } catch {
361357 return . failure( . invalidResponse) // JSONDecoder only throws DecodingError; unreachable.
362358 }
363359 }
364360 }
365361
366- private func makeResponseMetadata( _ httpResponse: HTTPURLResponse , body: Data ) -> ResponseMetadata {
367- ResponseMetadata ( response: httpResponse, body: body)
368- }
369-
370362 private func bodySnippet( from data: Data , limit: Int = 512 ) -> String ? {
371363 ResponseMetadata . bodySnippet ( from: data, limit: limit)
372364 }
0 commit comments