-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTPPNetworkExecutor.swift
447 lines (382 loc) · 16.5 KB
/
TPPNetworkExecutor.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//
// TPPNetworkExecutor.swift
// The Palace Project
//
// Created by Ettore Pasquini on 3/19/20.
// Copyright © 2020 NYPL Labs. All rights reserved.
//
import Foundation
/// Use this enum to express either-or semantics in a result.
enum NYPLResult<SuccessInfo> {
case success(SuccessInfo, URLResponse?)
case failure(TPPUserFriendlyError, URLResponse?)
}
/// A class that is capable of executing network requests in a thread-safe way.
/// This class implements caching according to server response caching headers,
/// but can also be configured to have a fallback mechanism to cache responses
/// that lack a sufficient set of caching headers. This fallback cache attempts
/// to use the value found in the `max-age` directive of the `Cache-Control`
/// header if present, otherwise defaults to 3 hours.
///
/// The cache lives on both memory and disk.
@objc class TPPNetworkExecutor: NSObject {
private let urlSession: URLSession
private let refreshQueue = DispatchQueue(label: "com.palace.token-refresh-queue", qos: .userInitiated)
private var isRefreshing = false
private var retryQueue: [URLSessionTask] = []
/// The delegate of the URLSession.
private let responder: TPPNetworkResponder
/// Designated initializer.
/// - Parameter credentialsProvider: The object responsible with providing cretdentials
/// - Parameter cachingStrategy: The strategy to cache responses with.
/// - Parameter delegateQueue: The queue where callbacks will be called.
@objc init(credentialsProvider: NYPLBasicAuthCredentialsProvider? = nil,
cachingStrategy: NYPLCachingStrategy,
delegateQueue: OperationQueue? = nil) {
self.responder = TPPNetworkResponder(credentialsProvider: credentialsProvider,
useFallbackCaching: cachingStrategy == .fallback)
let config = TPPCaching.makeURLSessionConfiguration(
caching: cachingStrategy,
requestTimeout: TPPNetworkExecutor.defaultRequestTimeout)
self.urlSession = URLSession(configuration: config,
delegate: self.responder,
delegateQueue: delegateQueue)
super.init()
}
deinit {
urlSession.finishTasksAndInvalidate()
}
/// A shared generic executor with enabled fallback caching.
@objc static let shared = TPPNetworkExecutor(cachingStrategy: .fallback)
/// Performs a GET request using the specified URL
/// - Parameters:
/// - reqURL: URL of the resource to GET.
/// - completion: Always called when the resource is either fetched from
/// the network or from the cache.
func GET(_ reqURL: URL,
useTokenIfAvailable: Bool = true,
completion: @escaping (_ result: NYPLResult<Data>) -> Void) {
let req = request(for: reqURL, useTokenIfAvailable: useTokenIfAvailable)
executeRequest(req, completion: completion)
}
}
extension TPPNetworkExecutor: TPPRequestExecuting {
/// Executes a given request.
/// - Parameters:
/// - req: The request to perform.
/// - completion: Always called when the resource is either fetched from
/// the network or from the cache.
/// - Returns: The task issueing the given request.
@discardableResult
func executeRequest(_ req: URLRequest, completion: @escaping (_: NYPLResult<Data>) -> Void) -> URLSessionDataTask {
let userAccount = TPPUserAccount.sharedAccount()
if let authDefinition = userAccount.authDefinition, authDefinition.isSaml {
return performDataTask(with: req, completion: completion)
}
if userAccount.isTokenRefreshRequired() {
handleTokenRefresh(for: req, completion: completion)
return URLSessionDataTask()
}
if req.hasRetried {
let error = createErrorForRetryFailure()
completion(NYPLResult.failure(error, nil))
return URLSessionDataTask()
}
return performDataTask(with: req, completion: completion)
}
private func createErrorForRetryFailure() -> NSError {
return NSError(
domain: TPPErrorLogger.clientDomain,
code: TPPErrorCode.invalidCredentials.rawValue,
userInfo: [NSLocalizedDescriptionKey: "Unauthorized HTTP after token refresh attempt"]
)
}
private func handleTokenRefresh(for req: URLRequest, completion: @escaping (_: NYPLResult<Data>) -> Void) {
refreshTokenAndResume(task: nil) { [weak self] newToken in
guard let strongSelf = self else { return }
if let token = newToken {
var updatedRequest = req
updatedRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
updatedRequest.hasRetried = true
strongSelf.executeRequest(updatedRequest, completion: completion)
} else {
let error = NSError(domain: TPPErrorLogger.clientDomain, code: TPPErrorCode.invalidCredentials.rawValue, userInfo: [NSLocalizedDescriptionKey: "Unauthorized HTTP"])
completion(NYPLResult.failure(error, nil))
}
}
}
private func performDataTask(with request: URLRequest,
completion: @escaping (_: NYPLResult<Data>) -> Void) -> URLSessionDataTask {
let task = urlSession.dataTask(with: request)
responder.addCompletion(completion, taskID: task.taskIdentifier)
Log.info(#file, "Task \(task.taskIdentifier): starting request \(request.loggableString)")
task.resume()
return task
}
}
extension TPPNetworkExecutor {
@objc func request(for url: URL, useTokenIfAvailable: Bool = true) -> URLRequest {
var urlRequest = URLRequest(url: url,
cachePolicy: urlSession.configuration.requestCachePolicy)
if let authToken = TPPUserAccount.sharedAccount().authToken, useTokenIfAvailable {
let headers = [
"Authorization" : "Bearer \(authToken)",
"Content-Type" : "application/json"
]
headers.forEach { urlRequest.setValue($0.value, forHTTPHeaderField: $0.key) }
}
urlRequest.setValue("", forHTTPHeaderField: "Accept-Language")
return urlRequest
}
@objc func clearCache() {
urlSession.configuration.urlCache?.removeAllCachedResponses()
}
}
// Objective-C compatibility
extension TPPNetworkExecutor {
@objc class func bearerAuthorized(request: URLRequest) -> URLRequest {
let headers: [String: String]
if let authToken = TPPUserAccount.sharedAccount().authToken, !authToken.isEmpty {
headers = [
"Authorization" : "Bearer \(authToken)",
"Content-Type" : "application/json"]
} else {
headers = [
"Authorization" : "",
"Content-Type" : "application/json"]
}
var request = request
for (headerKey, headerValue) in headers {
request.setValue(headerValue, forHTTPHeaderField: headerKey)
}
return request
}
/// Performs a GET request using the specified URL
/// - Parameters:
/// - reqURL: URL of the resource to GET.
/// - completion: Always called when the resource is either fetched from
/// the network or from the cache.
@objc func download(_ reqURL: URL,
completion: @escaping (_ result: Data?, _ response: URLResponse?, _ error: Error?) -> Void) -> URLSessionDownloadTask {
let req = request(for: reqURL)
let completionWrapper: (_ result: NYPLResult<Data>) -> Void = { result in
switch result {
case let .success(data, response): completion(data, response, nil)
case let .failure(error, response): completion(nil, response, error)
}
}
let task = urlSession.downloadTask(with: req)
responder.addCompletion(completionWrapper, taskID: task.taskIdentifier)
task.resume()
return task
}
/// Performs a GET request using the specified URL, if oauth token is available, it is added to the request
/// - Parameters:
/// - reqURL: URL of the resource to GET.
/// - completion: Always called when the resource is either fetched from
/// the network or from the cache.
@objc func addBearerAndExecute(_ request: URLRequest,
completion: @escaping (_ result: Data?, _ response: URLResponse?, _ error: Error?) -> Void) -> URLSessionDataTask? {
let req = TPPNetworkExecutor.bearerAuthorized(request: request)
let completionWrapper: (_ result: NYPLResult<Data>) -> Void = { result in
switch result {
case let .success(data, response): completion(data, response, nil)
case let .failure(error, response): completion(nil, response, error)
}
}
return executeRequest(req, completion: completionWrapper)
}
// Performs a GET request using the specified URL
/// - Parameters:
/// - reqURL: URL of the resource to GET.
/// - completion: Always called when the resource is either fetched from
/// the network or from the cache.
@objc func GET(_ reqURL: URL,
cachePolicy: NSURLRequest.CachePolicy = .useProtocolCachePolicy,
completion: @escaping (_ result: Data?, _ response: URLResponse?, _ error: Error?) -> Void) -> URLSessionDataTask? {
GET(request: request(for: reqURL), cachePolicy: cachePolicy, completion: completion)
}
/// Performs a GET request using the specified URLRequest
/// - Parameters:
/// - request: URLRequest of the resource to GET.
/// - completion: Always called when the resource is either fetched from
/// the network or from the cache.
@objc func GET(request: URLRequest,
cachePolicy: NSURLRequest.CachePolicy = .useProtocolCachePolicy,
completion: @escaping (_ result: Data?, _ response: URLResponse?, _ error: Error?) -> Void) -> URLSessionDataTask? {
if (request.httpMethod != "GET") {
var newRequest = request
newRequest.httpMethod = "GET"
return GET(request: newRequest, cachePolicy: cachePolicy, completion: completion)
}
var updatedReq = request
updatedReq.cachePolicy = cachePolicy
let completionWrapper: (_ result: NYPLResult<Data>) -> Void = { result in
switch result {
case let .success(data, response): completion(data, response, nil)
case let .failure(error, response): completion(nil, response, error)
}
}
return executeRequest(updatedReq, completion: completionWrapper)
}
/// Performs a PUT request using the specified URL
/// - Parameters:
/// - reqURL: URL of the resource to PUT.
/// - completion: Always called when the resource is either fetched from
/// the network or from the cache.
@objc func PUT(_ reqURL: URL,
completion: @escaping (_ result: Data?, _ response: URLResponse?, _ error: Error?) -> Void) -> URLSessionDataTask? {
PUT(request: request(for: reqURL), completion: completion)
}
/// Performs a PUT request using the specified URLRequest
/// - Parameters:
/// - request: URLRequest of the resource to PUT.
/// - completion: Always called when the resource is either fetched from
/// the network or from the cache.
@objc func PUT(request: URLRequest,
completion: @escaping (_ result: Data?, _ response: URLResponse?, _ error: Error?) -> Void) -> URLSessionDataTask? {
if (request.httpMethod != "PUT") {
var newRequest = request
newRequest.httpMethod = "PUT"
return PUT(request: newRequest, completion: completion)
}
let completionWrapper: (_ result: NYPLResult<Data>) -> Void = { result in
switch result {
case let .success(data, response): completion(data, response, nil)
case let .failure(error, response): completion(nil, response, error)
}
}
return executeRequest(request, completion: completionWrapper)
}
/// Performs a POST request using the specified request
/// - Parameters:
/// - request: Request to be posted..
/// - completion: Always called when the api call either returns or times out
@discardableResult
@objc
func POST(_ request: URLRequest,
completion: ((_ result: Data?, _ response: URLResponse?, _ error: Error?) -> Void)?) -> URLSessionDataTask? {
if (request.httpMethod != "POST") {
var newRequest = request
newRequest.httpMethod = "POST"
return POST(newRequest, completion: completion)
}
let completionWrapper: (_ result: NYPLResult<Data>) -> Void = { result in
switch result {
case let .success(data, response): completion?(data, response, nil)
case let .failure(error, response): completion?(nil, response, error)
}
}
return executeRequest(request, completion: completionWrapper)
}
/// Performs a DELETE request using the specified request
/// - Parameters:
/// - request: Request to be deleted..
/// - completion: Always called when the api call either returns or times out
@discardableResult
@objc
func DELETE(_ request: URLRequest,
completion: ((_ result: Data?, _ response: URLResponse?, _ error: Error?) -> Void)?) -> URLSessionDataTask? {
if (request.httpMethod != "DELETE") {
var newRequest = request
newRequest.httpMethod = "DELETE"
return DELETE(newRequest, completion: completion)
}
let completionWrapper: (_ result: NYPLResult<Data>) -> Void = { result in
switch result {
case let .success(data, response): completion?(data, response, nil)
case let .failure(error, response): completion?(nil, response, error)
}
}
return executeRequest(request, completion: completionWrapper)
}
func refreshTokenAndResume(task: URLSessionTask?, completion: ((String?) -> Void)? = nil) {
refreshQueue.async { [weak self] in
guard let self = self else { return }
guard !self.isRefreshing else {
completion?(nil)
return
}
self.isRefreshing = true
guard let username = TPPUserAccount.sharedAccount().username,
let password = TPPUserAccount.sharedAccount().pin else {
Log.info(#file, "Failed to refresh token due to missing credentials!")
self.isRefreshing = false
completion?(nil)
return
}
if let task = task {
self.retryQueue.append(task)
}
self.executeTokenRefresh(username: username, password: password) { result in
defer { self.isRefreshing = false }
switch result {
case .success:
var newTasks = [URLSessionTask]()
self.retryQueue.forEach { oldTask in
guard let originalRequest = oldTask.originalRequest,
let url = originalRequest.url else {
return
}
let mutableRequest = self.request(for: url)
let newTask = self.urlSession.dataTask(with: mutableRequest)
self.responder.updateCompletionId(oldTask.taskIdentifier, newId: newTask.taskIdentifier)
newTasks.append(newTask)
oldTask.cancel()
}
newTasks.forEach { $0.resume() }
self.retryQueue.removeAll()
completion?(nil)
case .failure(let error):
Log.info(#file, "Failed to refresh token with error: \(error)")
completion?(nil)
}
}
}
}
private func retryFailedRequests() {
while !retryQueue.isEmpty {
let task = retryQueue.removeFirst()
guard let request = task.originalRequest else { continue }
self.executeRequest(request) { _ in
Log.info(#file, "Task Successfully resumed after token refresh")
}
}
}
func executeTokenRefresh(username: String, password: String, completion: @escaping (Result<TokenResponse, Error>) -> Void) {
guard let tokenURL = TPPUserAccount.sharedAccount().authDefinition?.tokenURL else {
Log.error(#file, "Unable to refresh token, missing credentials")
completion(.failure(NSError(domain: "Unable to refresh token, missing credentials", code: 401)))
return
}
Task {
let tokenRequest = TokenRequest(url: tokenURL, username: username, password: password)
let result = await tokenRequest.execute()
switch result {
case .success(let tokenResponse):
TPPUserAccount.sharedAccount().setAuthToken(
tokenResponse.accessToken,
barcode: username,
pin: password,
expirationDate: tokenResponse.expirationDate
)
completion(.success(tokenResponse))
case .failure(let error):
completion(.failure(error))
}
}
}
}
private extension URLRequest {
struct AssociatedKeys {
static var hasRetriedKey = "hasRetriedKey"
}
var hasRetried: Bool {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.hasRetriedKey) as? Bool ?? false
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.hasRetriedKey, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
}