-
Notifications
You must be signed in to change notification settings - Fork 734
/
HTTPNetworkTransport.swift
300 lines (254 loc) · 12.9 KB
/
HTTPNetworkTransport.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import Foundation
/// Empty base protocol to allow multiple sub-protocols to just use a single parameter.
public protocol HTTPNetworkTransportDelegate: class {}
/// Methods which will be called prior to a request being sent to the server.
public protocol HTTPNetworkTransportPreflightDelegate: HTTPNetworkTransportDelegate {
/// Called when a request is about to send, to validate that it should be sent.
/// Good for early-exiting if your user is not logged in, for example.
///
/// - Parameters:
/// - networkTransport: The network transport which wants to send a request
/// - request: The request, BEFORE it has been modified by `willSend`
/// - Returns: True if the request should proceed, false if not.
func networkTransport(_ networkTransport: HTTPNetworkTransport, shouldSend request: URLRequest) -> Bool
/// Called when a request is about to send. Allows last minute modification of any properties on the request,
///
///
/// - Parameters:
/// - networkTransport: The network transport which is about to send a request
/// - request: The request, as an `inout` variable for modification
func networkTransport(_ networkTransport: HTTPNetworkTransport, willSend request: inout URLRequest)
}
// MARK: -
/// Methods which will be called after some kind of response has been received to a `URLSessionTask`.
public protocol HTTPNetworkTransportTaskCompletedDelegate: HTTPNetworkTransportDelegate {
/// A callback to allow hooking in URL session responses for things like logging and examining headers.
/// NOTE: This will call back on whatever thread the URL session calls back on, which is never the main thread. Call `DispatchQueue.main.async` before touching your UI!
///
/// - Parameters:
/// - networkTransport: The network transport that completed a task
/// - request: The request which was completed by the task
/// - data: [optional] Any data received. Passed through from `URLSession`.
/// - response: [optional] Any response received. Passed through from `URLSession`.
/// - error: [optional] Any error received. Passed through from `URLSession`.
func networkTransport(_ networkTransport: HTTPNetworkTransport,
didCompleteRawTaskForRequest request: URLRequest,
withData data: Data?,
response: URLResponse?,
error: Error?)
}
// MARK: -
public protocol HTTPNetworkTransportRetryDelegate: HTTPNetworkTransportDelegate {
/// Called when an error has been received after a request has been sent to the server to see if an operation should be retried or not.
/// NOTE: Don't just call the `retryHandler` with `true` all the time, or you can potentially wind up in an infinite loop of errors
///
/// - Parameters:
/// - networkTransport: The network transport which received the error
/// - error: The received error
/// - request: The URLRequest which generated the error
/// - response: [Optional] Any response received when the error was generated
/// - retryHandler: A closure indicating whether the operation should be retried. Asyncrhonous to allow for re-authentication or other async operations to complete.
func networkTransport(_ networkTransport: HTTPNetworkTransport,
receivedError error: Error,
for request: URLRequest,
response: URLResponse?,
retryHandler: @escaping (_ shouldRetry: Bool) -> Void)
}
// MARK: -
/// A network transport that uses HTTP POST requests to send GraphQL operations to a server, and that uses `URLSession` as the networking implementation.
public class HTTPNetworkTransport {
let url: URL
let session: URLSession
let serializationFormat = JSONSerializationFormat.self
let useGETForQueries: Bool
let delegate: HTTPNetworkTransportDelegate?
private let sendOperationIdentifiers: Bool
/// Creates a network transport with the specified server URL and session configuration.
///
/// - Parameters:
/// - url: The URL of a GraphQL server to connect to.
/// - session: The URLSession to use. Defaults to `URLSession.shared`,
/// - sendOperationIdentifiers: Whether to send operation identifiers rather than full operation text, for use with servers that support query persistence. Defaults to false.
/// - useGETForQueries: If query operation should be sent using GET instead of POST. Defaults to false.
/// - delegate: [Optional] A delegate which can conform to any or all of `HTTPNetworkTransportPreflightDelegate`, `HTTPNetworkTransportTaskCompletedDelegate`, and `HTTPNetworkTransportRetryDelegate`. Defaults to nil.
public init(url: URL,
session: URLSession = .shared,
sendOperationIdentifiers: Bool = false,
useGETForQueries: Bool = false,
delegate: HTTPNetworkTransportDelegate? = nil) {
self.url = url
self.session = session
self.sendOperationIdentifiers = sendOperationIdentifiers
self.useGETForQueries = useGETForQueries
self.delegate = delegate
}
private func send<Operation>(operation: Operation, files: [GraphQLFile]?, completionHandler: @escaping (_ results: Result<GraphQLResponse<Operation>, Error>) -> Void) -> Cancellable {
let request: URLRequest
do {
request = try self.createRequest(for: operation, files: files)
} catch {
completionHandler(.failure(error))
return EmptyCancellable()
}
let task = session.dataTask(with: request) { [weak self] data, response, error in
self?.rawTaskCompleted(request: request,
data: data,
response: response,
error: error)
if let receivedError = error {
self?.handleErrorOrRetry(operation: operation,
error: receivedError,
for: request,
response: response,
completionHandler: completionHandler)
return
}
guard let httpResponse = response as? HTTPURLResponse else {
fatalError("Response should be an HTTPURLResponse")
}
guard httpResponse.isSuccessful else {
let unsuccessfulError = GraphQLHTTPResponseError(body: data,
response: httpResponse,
kind: .errorResponse)
self?.handleErrorOrRetry(operation: operation,
error: unsuccessfulError,
for: request,
response: response,
completionHandler: completionHandler)
return
}
guard let data = data else {
let error = GraphQLHTTPResponseError(body: nil,
response: httpResponse,
kind: .invalidResponse)
self?.handleErrorOrRetry(operation: operation,
error: error,
for: request,
response: response,
completionHandler: completionHandler)
return
}
do {
guard let body = try self?.serializationFormat.deserialize(data: data) as? JSONObject else {
throw GraphQLHTTPResponseError(body: data, response: httpResponse, kind: .invalidResponse)
}
let response = GraphQLResponse(operation: operation, body: body)
completionHandler(.success(response))
} catch let parsingError {
self?.handleErrorOrRetry(operation: operation,
error: parsingError,
for: request,
response: response,
completionHandler: completionHandler)
}
}
task.resume()
return task
}
private func handleErrorOrRetry<Operation>(operation: Operation,
error: Error,
for request: URLRequest,
response: URLResponse?,
completionHandler: @escaping (_ result: Result<GraphQLResponse<Operation>, Error>) -> Void) {
guard
let delegate = self.delegate,
let retrier = delegate as? HTTPNetworkTransportRetryDelegate else {
completionHandler(.failure(error))
return
}
retrier.networkTransport(
self,
receivedError: error,
for: request,
response: response,
retryHandler: { [weak self] shouldRetry in
guard shouldRetry else {
completionHandler(.failure(error))
return
}
_ = self?.send(operation: operation, completionHandler: completionHandler)
})
}
private func rawTaskCompleted(request: URLRequest,
data: Data?,
response: URLResponse?,
error: Error?) {
guard
let delegate = self.delegate,
let taskDelegate = delegate as? HTTPNetworkTransportTaskCompletedDelegate else {
return
}
taskDelegate.networkTransport(self,
didCompleteRawTaskForRequest: request,
withData: data,
response: response,
error: error)
}
private func createRequest<Operation: GraphQLOperation>(for operation: Operation, files: [GraphQLFile]?) throws -> URLRequest {
let body = RequestCreator.requestBody(for: operation, sendOperationIdentifiers: self.sendOperationIdentifiers)
var request = URLRequest(url: self.url)
// We default to json, but this can be changed below if needed.
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
if self.useGETForQueries && operation.operationType == .query {
let transformer = GraphQLGETTransformer(body: body, url: self.url)
if let urlForGet = transformer.createGetURL() {
request = URLRequest(url: urlForGet)
request.httpMethod = GraphQLHTTPMethod.GET.rawValue
} else {
throw GraphQLHTTPRequestError.serializedQueryParamsMessageError
}
} else {
do {
if let files = files, !files.isEmpty {
let formData = try RequestCreator.requestMultipartFormData(
for: operation,
files: files,
sendOperationIdentifiers: self.sendOperationIdentifiers,
serializationFormat: self.serializationFormat)
request.setValue("multipart/form-data; boundary=\(formData.boundary)", forHTTPHeaderField: "Content-Type")
request.httpBody = try formData.encode()
} else {
request.httpBody = try serializationFormat.serialize(value: body)
}
request.httpMethod = GraphQLHTTPMethod.POST.rawValue
} catch {
throw GraphQLHTTPRequestError.serializedBodyMessageError
}
}
request.setValue(operation.operationName, forHTTPHeaderField: "X-APOLLO-OPERATION-NAME")
if let operationID = operation.operationIdentifier {
request.setValue(operationID, forHTTPHeaderField: "X-APOLLO-OPERATION-ID")
}
// If there's a delegate, do a pre-flight check and allow modifications to the request.
if
let delegate = self.delegate,
let preflightDelegate = delegate as? HTTPNetworkTransportPreflightDelegate {
guard preflightDelegate.networkTransport(self, shouldSend: request) else {
throw GraphQLHTTPRequestError.cancelledByDelegate
}
preflightDelegate.networkTransport(self, willSend: &request)
}
return request
}
}
// MARK: - NetworkTransport conformance
extension HTTPNetworkTransport: NetworkTransport {
public func send<Operation>(operation: Operation, completionHandler: @escaping (_ result: Result<GraphQLResponse<Operation>, Error>) -> Void) -> Cancellable {
return send(operation: operation, files: nil, completionHandler: completionHandler)
}
}
// MARK: - UploadingNetworkTransport conformance
extension HTTPNetworkTransport: UploadingNetworkTransport {
public func upload<Operation>(operation: Operation, files: [GraphQLFile], completionHandler: @escaping (_ result: Result<GraphQLResponse<Operation>, Error>) -> Void) -> Cancellable {
return send(operation: operation, files: files, completionHandler: completionHandler)
}
}
// MARK: - Equatable conformance
extension HTTPNetworkTransport: Equatable {
public static func ==(lhs: HTTPNetworkTransport, rhs: HTTPNetworkTransport) -> Bool {
return lhs.url == rhs.url
&& lhs.session == rhs.session
&& lhs.sendOperationIdentifiers == rhs.sendOperationIdentifiers
&& lhs.useGETForQueries == rhs.useGETForQueries
}
}