... | @@ -332,8 +332,9 @@ Please use `swift5` generator because Swift 4.x is deprecated. |
... | @@ -332,8 +332,9 @@ Please use `swift5` generator because Swift 4.x is deprecated. |
|
<details>
|
|
<details>
|
|
<summary>Then you subclass URLSessionRequestBuilder and URLSessionDecodableRequestBuilder </summary>
|
|
<summary>Then you subclass URLSessionRequestBuilder and URLSessionDecodableRequestBuilder </summary>
|
|
|
|
|
|
class BearerRequestBuilder<T>: URLSessionRequestBuilder<T> {
|
|
class BearerRequestBuilder<T>: URLSessionRequestBuilder<T> {
|
|
override func execute(_ apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue, _ completion: @escaping (Result<Response<T>, Error>) -> Void) {
|
|
@discardableResult
|
|
|
|
override func execute(_ apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue, _ completion: @escaping (Result<Response<T>, ErrorResponse>) -> Void) -> OpenApiRequestCancellable {
|
|
|
|
|
|
// Before making the request, we can validate if we have a bearer token to be able to make a request
|
|
// Before making the request, we can validate if we have a bearer token to be able to make a request
|
|
BearerTokenHandler.refreshTokenIfDoesntExist {
|
|
BearerTokenHandler.refreshTokenIfDoesntExist {
|
... | @@ -374,12 +375,14 @@ Please use `swift5` generator because Swift 4.x is deprecated. |
... | @@ -374,12 +375,14 @@ Please use `swift5` generator because Swift 4.x is deprecated. |
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BearerDecodableRequestBuilder<T: Decodable>: URLSessionDecodableRequestBuilder<T> {
|
|
return requestCancellable
|
|
override func execute(_ apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue, _ completion: @escaping (Result<Response<T>, Error>) -> Void) {
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BearerDecodableRequestBuilder<T: Decodable>: URLSessionDecodableRequestBuilder<T> {
|
|
|
|
@discardableResult
|
|
|
|
override func execute(_ apiResponseQueue: DispatchQueue = PetstoreClientAPI.apiResponseQueue, _ completion: @escaping (Result<Response<T>, ErrorResponse>) -> Void) -> OpenApiRequestCancellable {
|
|
// Before making the request, we can validate if we have a bearer token to be able to make a request
|
|
// Before making the request, we can validate if we have a bearer token to be able to make a request
|
|
BearerTokenHandler.refreshTokenIfDoesntExist {
|
|
BearerTokenHandler.refreshTokenIfDoesntExist {
|
|
|
|
|
... | @@ -419,10 +422,12 @@ Please use `swift5` generator because Swift 4.x is deprecated. |
... | @@ -419,10 +422,12 @@ Please use `swift5` generator because Swift 4.x is deprecated. |
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return requestCancellable
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
class BearerTokenHandler {
|
|
class BearerTokenHandler {
|
|
private static var bearerToken: String? = nil
|
|
private static var bearerToken: String? = nil
|
|
|
|
|
|
static func refreshTokenIfDoesntExist(completionHandler: @escaping () -> Void) {
|
|
static func refreshTokenIfDoesntExist(completionHandler: @escaping () -> Void) {
|
... | @@ -454,7 +459,8 @@ Please use `swift5` generator because Swift 4.x is deprecated. |
... | @@ -454,7 +459,8 @@ Please use `swift5` generator because Swift 4.x is deprecated. |
|
|
|
|
|
completionHandler()
|
|
completionHandler()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
</details>
|
|
</details>
|
|
|
|
|
|
Then you assign the `BearerRequestBuilderFactory` to the property requestBuilderFactory in the APIs.swift file.
|
|
Then you assign the `BearerRequestBuilderFactory` to the property requestBuilderFactory in the APIs.swift file.
|
... | | ... | |