@@ -46,6 +46,8 @@ public protocol HTTPNetworkTransportDelegate: class {
4646 /// Opportunity for the delegate to modify the URLRequest before it is sent. `completionHandler` must be called with the
4747 /// desired URLRequest to send.
4848 func networkTransport( _ networkTransport: HTTPNetworkTransport , prepareRequest request: URLRequest , completionHandler: @escaping ( URLRequest ) -> Void )
49+ /// Opportunity for the delegate to inspect and modify a resposne before it is processed by Apollo.
50+ func networkTransport( _ networkTransport: HTTPNetworkTransport , request: URLRequest , preprocessData data: Data ? , response: URLResponse ? , error: Error ? , completionHandler: @escaping ( Data ? , URLResponse ? , Error ? ) -> Void )
4951}
5052
5153/// A network transport that uses HTTP POST requests to send GraphQL operations to a server, and that uses `URLSession` as the networking implementation.
@@ -87,33 +89,12 @@ public class HTTPNetworkTransport: NetworkTransport {
8789 request. httpBody = try ! serializationFormat. serialize ( value: body)
8890
8991 let requestOperation = URLRequestOperation ( session: session, request: request) { ( data: Data ? , response: URLResponse ? , error: Error ? ) in
90- if error != nil {
91- completionHandler ( nil , error)
92- return
93- }
94-
95- guard let httpResponse = response as? HTTPURLResponse else {
96- fatalError ( " Response should be an HTTPURLResponse " )
97- }
98-
99- if ( !httpResponse. isSuccessful) {
100- completionHandler ( nil , GraphQLHTTPResponseError ( body: data, response: httpResponse, kind: . errorResponse) )
101- return
102- }
103-
104- guard let data = data else {
105- completionHandler ( nil , GraphQLHTTPResponseError ( body: nil , response: httpResponse, kind: . invalidResponse) )
106- return
107- }
108-
109- do {
110- guard let body = try self . serializationFormat. deserialize ( data: data) as? JSONObject else {
111- throw GraphQLHTTPResponseError ( body: data, response: httpResponse, kind: . invalidResponse)
92+ if let delegate = self . delegate {
93+ delegate. networkTransport ( self , request: request, preprocessData: data, response: response, error: error) {
94+ self . handleResponse ( for: operation, data: $0, response: $1, error: $2, completionHandler: completionHandler)
11295 }
113- let response = GraphQLResponse ( operation: operation, body: body)
114- completionHandler ( response, nil )
115- } catch {
116- completionHandler ( nil , error)
96+ } else {
97+ self . handleResponse ( for: operation, data: data, response: response, error: error, completionHandler: completionHandler)
11798 }
11899 }
119100
@@ -128,6 +109,37 @@ public class HTTPNetworkTransport: NetworkTransport {
128109
129110 return requestOperation
130111 }
112+
113+ private func handleResponse< Operation> ( for operation: Operation , data: Data ? , response: URLResponse ? , error: Error ? , completionHandler: @escaping ( _ response: GraphQLResponse < Operation > ? , _ error: Error ? ) -> Void ) {
114+ if error != nil {
115+ completionHandler ( nil , error)
116+ return
117+ }
118+
119+ guard let httpResponse = response as? HTTPURLResponse else {
120+ fatalError ( " Response should be an HTTPURLResponse " )
121+ }
122+
123+ if ( !httpResponse. isSuccessful) {
124+ completionHandler ( nil , GraphQLHTTPResponseError ( body: data, response: httpResponse, kind: . errorResponse) )
125+ return
126+ }
127+
128+ guard let data = data else {
129+ completionHandler ( nil , GraphQLHTTPResponseError ( body: nil , response: httpResponse, kind: . invalidResponse) )
130+ return
131+ }
132+
133+ do {
134+ guard let body = try self . serializationFormat. deserialize ( data: data) as? JSONObject else {
135+ throw GraphQLHTTPResponseError ( body: data, response: httpResponse, kind: . invalidResponse)
136+ }
137+ let response = GraphQLResponse ( operation: operation, body: body)
138+ completionHandler ( response, nil )
139+ } catch {
140+ completionHandler ( nil , error)
141+ }
142+ }
131143
132144 private let sendOperationIdentifiers : Bool
133145
0 commit comments