|
| 1 | +// |
| 2 | +// BearerDecodableRequestBuilder.swift |
| 3 | +// SwaggerClient |
| 4 | +// |
| 5 | +// Created by Bruno Coelho on 31/12/2020. |
| 6 | +// Copyright © 2020 Swagger. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import PetstoreClient |
| 11 | + |
| 12 | +class BearerRequestBuilderFactory: RequestBuilderFactory { |
| 13 | + func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type { |
| 14 | + BearerRequestBuilder<T>.self |
| 15 | + } |
| 16 | + |
| 17 | + func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type { |
| 18 | + BearerDecodableRequestBuilder<T>.self |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +class BearerRequestBuilder<T>: URLSessionRequestBuilder<T> { |
| 23 | + override func execute(_ apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue, _ completion: @escaping (Result<Response<T>, Error>) -> Void) { |
| 24 | + |
| 25 | + // Before making the request, we can validate if we have a bearer token to be able to make a request |
| 26 | + BearerTokenHandler.refreshTokenIfDoesntExist { |
| 27 | + |
| 28 | + // Here we make the request |
| 29 | + super.execute(apiResponseQueue) { result in |
| 30 | + |
| 31 | + switch result { |
| 32 | + case .success: |
| 33 | + // If we got a successful response, we send the response to the completion block |
| 34 | + completion(result) |
| 35 | + |
| 36 | + case let .failure(error): |
| 37 | + |
| 38 | + // If we got a failure response, we will analyse the error to see what we should do with it |
| 39 | + if case let ErrorResponse.error(_, data, response, error) = error { |
| 40 | + |
| 41 | + // If the error is an ErrorResponse.error() we will analyse it to see if it's a 401, and if it's a 401, we will refresh the token and retry the request |
| 42 | + BearerTokenHandler.refreshTokenIfUnauthorizedRequestResponse( |
| 43 | + data: data, |
| 44 | + response: response, |
| 45 | + error: error |
| 46 | + ) { wasTokenRefreshed in |
| 47 | + |
| 48 | + if wasTokenRefreshed { |
| 49 | + // If the token was refreshed, it's because it was a 401 error, so we refreshed the token, and we are going to retry the request by calling self.execute() |
| 50 | + self.execute(apiResponseQueue, completion) |
| 51 | + } else { |
| 52 | + // If the token was not refreshed, it's because it was not a 401 error, so we send the response to the completion block |
| 53 | + completion(result) |
| 54 | + } |
| 55 | + } |
| 56 | + } else { |
| 57 | + // If it's an unknown error, we send the response to the completion block |
| 58 | + completion(result) |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +class BearerDecodableRequestBuilder<T: Decodable>: URLSessionDecodableRequestBuilder<T> { |
| 68 | + override func execute(_ apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue, _ completion: @escaping (Result<Response<T>, Error>) -> Void) { |
| 69 | + |
| 70 | + // Before making the request, we can validate if we have a bearer token to be able to make a request |
| 71 | + BearerTokenHandler.refreshTokenIfDoesntExist { |
| 72 | + |
| 73 | + // Here we make the request |
| 74 | + super.execute(apiResponseQueue) { result in |
| 75 | + |
| 76 | + switch result { |
| 77 | + case .success: |
| 78 | + // If we got a successful response, we send the response to the completion block |
| 79 | + completion(result) |
| 80 | + |
| 81 | + case let .failure(error): |
| 82 | + |
| 83 | + // If we got a failure response, we will analyse the error to see what we should do with it |
| 84 | + if case let ErrorResponse.error(_, data, response, error) = error { |
| 85 | + |
| 86 | + // If the error is an ErrorResponse.error() we will analyse it to see if it's a 401, and if it's a 401, we will refresh the token and retry the request |
| 87 | + BearerTokenHandler.refreshTokenIfUnauthorizedRequestResponse( |
| 88 | + data: data, |
| 89 | + response: response, |
| 90 | + error: error |
| 91 | + ) { wasTokenRefreshed in |
| 92 | + |
| 93 | + if wasTokenRefreshed { |
| 94 | + // If the token was refreshed, it's because it was a 401 error, so we refreshed the token, and we are going to retry the request by calling self.execute() |
| 95 | + self.execute(apiResponseQueue, completion) |
| 96 | + } else { |
| 97 | + // If the token was not refreshed, it's because it was not a 401 error, so we send the response to the completion block |
| 98 | + completion(result) |
| 99 | + } |
| 100 | + } |
| 101 | + } else { |
| 102 | + // If it's an unknown error, we send the response to the completion block |
| 103 | + completion(result) |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +class BearerTokenHandler { |
| 113 | + private static var bearerToken: String? = nil |
| 114 | + |
| 115 | + static func refreshTokenIfDoesntExist(completionHandler: @escaping () -> Void) { |
| 116 | + if bearerToken != nil { |
| 117 | + completionHandler() |
| 118 | + } else { |
| 119 | + startRefreshingToken { |
| 120 | + completionHandler() |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + static func refreshTokenIfUnauthorizedRequestResponse(data: Data?, response: URLResponse?, error: Error?, completionHandler: @escaping (Bool) -> Void) { |
| 126 | + if let response = response as? HTTPURLResponse, response.statusCode == 401 { |
| 127 | + startRefreshingToken { |
| 128 | + completionHandler(true) |
| 129 | + } |
| 130 | + } else { |
| 131 | + completionHandler(false) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static func startRefreshingToken(completionHandler: @escaping () -> Void) { |
| 136 | + // Get a bearer token |
| 137 | + let dummyBearerToken = "..." |
| 138 | + |
| 139 | + bearerToken = dummyBearerToken |
| 140 | + PetstoreClientAPI.customHeaders["Authorization"] = "Bearer \(dummyBearerToken)" |
| 141 | + |
| 142 | + completionHandler() |
| 143 | + } |
| 144 | +} |
0 commit comments