forked from loafle/openapi-generator-original
[swift][client] upgrade alamofire to version 5.4.3 (#10292)
* [swift][client] initial swift upgrade * [swift][client] upgrade alamofire
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
{{#useAlamofire}}github "Alamofire/Alamofire" ~> 4.9.1{{/useAlamofire}}{{#usePromiseKit}}
|
||||
{{#useAlamofire}}github "Alamofire/Alamofire" ~> 5.4.3{{/useAlamofire}}{{#usePromiseKit}}
|
||||
github "mxcl/PromiseKit" ~> 6.15.3{{/usePromiseKit}}{{#useRxSwift}}
|
||||
github "ReactiveX/RxSwift" ~> 6.2.0{{/useRxSwift}}
|
||||
github "Flight-School/AnyCodable" ~> 0.6.1
|
||||
|
||||
@@ -9,9 +9,16 @@ let package = Package(
|
||||
.macOS(.v10_15),
|
||||
{{/useVapor}}
|
||||
{{^useVapor}}
|
||||
{{#useAlamofire}}
|
||||
.iOS(.v10),
|
||||
.macOS(.v10_12),
|
||||
.tvOS(.v10),
|
||||
{{/useAlamofire}}
|
||||
{{^useAlamofire}}
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
{{/useAlamofire}}
|
||||
.watchOS(.v3),
|
||||
{{/useVapor}}
|
||||
],
|
||||
@@ -26,7 +33,7 @@ let package = Package(
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
.package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.1"),
|
||||
{{#useAlamofire}}
|
||||
.package(url: "https://github.com/Alamofire/Alamofire", from: "4.9.1"),
|
||||
.package(url: "https://github.com/Alamofire/Alamofire", from: "5.4.3"),
|
||||
{{/useAlamofire}}
|
||||
{{#useVapor}}
|
||||
.package(url: "https://github.com/vapor/vapor", from: "4.0.0")
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = '{{projectName}}'{{#projectDescription}}
|
||||
s.summary = '{{.}}'{{/projectDescription}}
|
||||
{{#useAlamofire}}
|
||||
s.ios.deployment_target = '10.0'
|
||||
s.osx.deployment_target = '10.12'
|
||||
s.tvos.deployment_target = '10.0'
|
||||
{{/useAlamofire}}
|
||||
{{^useAlamofire}}
|
||||
s.ios.deployment_target = '9.0'
|
||||
s.osx.deployment_target = '10.11'
|
||||
s.tvos.deployment_target = '9.0'
|
||||
{{/useAlamofire}}
|
||||
s.watchos.deployment_target = '3.0'
|
||||
s.version = '{{podVersion}}{{^podVersion}}{{#apiInfo}}{{version}}{{/apiInfo}}{{^apiInfo}}}0.0.1{{/apiInfo}}{{/podVersion}}'
|
||||
s.source = {{#podSource}}{{& podSource}}{{/podSource}}{{^podSource}}{ :git => 'git@github.com:OpenAPITools/openapi-generator.git', :tag => 'v{{#apiInfo}}{{version}}{{/apiInfo}}{{^apiInfo}}}0.0.1{{/apiInfo}}' }{{/podSource}}
|
||||
@@ -33,7 +40,7 @@ Pod::Spec.new do |s|
|
||||
s.dependency 'RxSwift', '~> 6.2.0'
|
||||
{{/useRxSwift}}
|
||||
{{#useAlamofire}}
|
||||
s.dependency 'Alamofire', '~> 4.9.1'
|
||||
s.dependency 'Alamofire', '~> 5.4.3'
|
||||
{{/useAlamofire}}
|
||||
s.dependency 'AnyCodable-FlightSchool', '~> 0.6.1'
|
||||
end
|
||||
|
||||
@@ -18,7 +18,7 @@ class AlamofireRequestBuilderFactory: RequestBuilderFactory {
|
||||
}
|
||||
|
||||
// Store manager to retain its reference
|
||||
private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManager>()
|
||||
private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
required {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:]) {
|
||||
@@ -29,19 +29,18 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
May be overridden by a subclass if you want to control the session
|
||||
configuration.
|
||||
*/
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func createSessionManager() -> Alamofire.SessionManager {
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func createAlamofireSession(interceptor: RequestInterceptor? = nil) -> Alamofire.Session {
|
||||
let configuration = URLSessionConfiguration.default
|
||||
configuration.httpAdditionalHeaders = buildHeaders()
|
||||
return Alamofire.SessionManager(configuration: configuration)
|
||||
return Alamofire.Session(configuration: configuration,
|
||||
interceptor: interceptor)
|
||||
}
|
||||
|
||||
/**
|
||||
May be overridden by a subclass if you want to custom request constructor.
|
||||
*/
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func createURLRequest() -> URLRequest? {
|
||||
guard let xMethod = Alamofire.HTTPMethod(rawValue: method) else {
|
||||
fatalError("Unsupported Http method - \(method)")
|
||||
}
|
||||
let xMethod = Alamofire.HTTPMethod(rawValue: method)
|
||||
|
||||
let encoding: ParameterEncoding
|
||||
|
||||
@@ -51,9 +50,12 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
case .options, .post, .put, .patch, .delete, .trace, .connect:
|
||||
encoding = JSONDataEncoding()
|
||||
|
||||
default:
|
||||
fatalError("Unsupported HTTPMethod - \(xMethod.rawValue)")
|
||||
}
|
||||
|
||||
guard let originalRequest = try? URLRequest(url: URLString, method: HTTPMethod(rawValue: method)!, headers: buildHeaders()) else { return nil }
|
||||
guard let originalRequest = try? URLRequest(url: URLString, method: xMethod, headers: HTTPHeaders(buildHeaders())) else { return nil }
|
||||
return try? encoding.encode(originalRequest, with: parameters)
|
||||
}
|
||||
|
||||
@@ -72,19 +74,17 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
May be overridden by a subclass if you want to control the request
|
||||
configuration (e.g. to override the cache policy).
|
||||
*/
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func makeRequest(manager: SessionManager, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) -> DataRequest {
|
||||
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: headers)
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func makeRequest(manager: Session, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) -> DataRequest {
|
||||
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: HTTPHeaders(headers))
|
||||
}
|
||||
|
||||
override {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func execute(_ apiResponseQueue: DispatchQueue = {{projectName}}.apiResponseQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) {
|
||||
let managerId = UUID().uuidString
|
||||
// Create a new manager for each request to customize its request header
|
||||
let manager = createSessionManager()
|
||||
let manager = createAlamofireSession()
|
||||
managerStore[managerId] = manager
|
||||
|
||||
guard let xMethod = Alamofire.HTTPMethod(rawValue: method) else {
|
||||
fatalError("Unsupported Http method - \(method)")
|
||||
}
|
||||
let xMethod = Alamofire.HTTPMethod(rawValue: method)
|
||||
|
||||
let encoding: ParameterEncoding?
|
||||
|
||||
@@ -100,7 +100,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
} else if contentType == "multipart/form-data" {
|
||||
encoding = nil
|
||||
|
||||
manager.upload(multipartFormData: { mpForm in
|
||||
let upload = manager.upload(multipartFormData: { mpForm in
|
||||
for (k, v) in self.parameters! {
|
||||
switch v {
|
||||
case let fileURL as URL:
|
||||
@@ -117,30 +117,28 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
fatalError("Unprocessable value \(v) with key \(k)")
|
||||
}
|
||||
}
|
||||
}, to: URLString, method: xMethod, headers: nil, encodingCompletion: { encodingResult in
|
||||
switch encodingResult {
|
||||
case let .success(upload, _, _):
|
||||
if let onProgressReady = self.onProgressReady {
|
||||
onProgressReady(upload.uploadProgress)
|
||||
}
|
||||
self.processRequest(request: upload, managerId, apiResponseQueue, completion)
|
||||
case let .failure(encodingError):
|
||||
apiResponseQueue.async {
|
||||
completion(.failure(ErrorResponse.error(415, nil, nil, encodingError)))
|
||||
}
|
||||
}, to: URLString, method: xMethod, headers: nil)
|
||||
.uploadProgress { progress in
|
||||
if let onProgressReady = self.onProgressReady {
|
||||
onProgressReady(progress)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
self.processRequest(request: upload, managerId, apiResponseQueue, completion)
|
||||
} else if contentType == "application/x-www-form-urlencoded" {
|
||||
encoding = URLEncoding(destination: .httpBody)
|
||||
} else {
|
||||
fatalError("Unsupported Media Type - \(contentType)")
|
||||
}
|
||||
|
||||
default:
|
||||
fatalError("Unsupported HTTPMethod - \(xMethod.rawValue)")
|
||||
}
|
||||
|
||||
if let encoding = encoding {
|
||||
let request = makeRequest(manager: manager, method: xMethod, encoding: encoding, headers: headers)
|
||||
if let onProgressReady = self.onProgressReady {
|
||||
onProgressReady(request.progress)
|
||||
onProgressReady(request.uploadProgress)
|
||||
}
|
||||
processRequest(request: request, managerId, apiResponseQueue, completion)
|
||||
}
|
||||
@@ -148,7 +146,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
fileprivate func processRequest(request: DataRequest, _ managerId: String, _ apiResponseQueue: DispatchQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) {
|
||||
if let credential = self.credential {
|
||||
request.authenticate(usingCredential: credential)
|
||||
request.authenticate(with: credential)
|
||||
}
|
||||
|
||||
let cleanupRequest = {
|
||||
@@ -176,7 +174,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
do {
|
||||
|
||||
guard !dataResponse.result.isFailure else {
|
||||
guard case .success = dataResponse.result else {
|
||||
throw DownloadException.responseFailed
|
||||
}
|
||||
|
||||
@@ -256,7 +254,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}open{{/nonPublicApi}} func buildHeaders() -> [String: String] {
|
||||
var httpHeaders = SessionManager.defaultHTTPHeaders
|
||||
var httpHeaders = Alamofire.HTTPHeaders.default.dictionary
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
@@ -320,7 +318,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
override fileprivate func processRequest(request: DataRequest, _ managerId: String, _ apiResponseQueue: DispatchQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) {
|
||||
if let credential = self.credential {
|
||||
request.authenticate(usingCredential: credential)
|
||||
request.authenticate(with: credential)
|
||||
}
|
||||
|
||||
let cleanupRequest = {
|
||||
@@ -348,7 +346,7 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
do {
|
||||
|
||||
guard !dataResponse.result.isFailure else {
|
||||
guard case .success = dataResponse.result else {
|
||||
throw DownloadException.responseFailed
|
||||
}
|
||||
|
||||
@@ -413,11 +411,11 @@ private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManag
|
||||
|
||||
})
|
||||
default:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (dataResponse: DataResponse<Data>) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
|
||||
cleanupRequest()
|
||||
|
||||
guard dataResponse.result.isSuccess else {
|
||||
completion(.failure(ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.response, dataResponse.result.error!)))
|
||||
if case let .failure(error) = dataResponse.result {
|
||||
completion(.failure(ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.response, error)))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
github "Alamofire/Alamofire" ~> 4.9.1
|
||||
github "Alamofire/Alamofire" ~> 5.4.3
|
||||
github "Flight-School/AnyCodable" ~> 0.6.1
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
"repositoryURL": "https://github.com/Alamofire/Alamofire",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "747c8db8d57b68d5e35275f10c92d55f982adbd4",
|
||||
"version": "4.9.1"
|
||||
"revision": "f96b619bcb2383b43d898402283924b80e2c4bae",
|
||||
"version": "5.4.3"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@@ -5,9 +5,9 @@ import PackageDescription
|
||||
let package = Package(
|
||||
name: "PetstoreClient",
|
||||
platforms: [
|
||||
.iOS(.v9),
|
||||
.macOS(.v10_11),
|
||||
.tvOS(.v9),
|
||||
.iOS(.v10),
|
||||
.macOS(.v10_12),
|
||||
.tvOS(.v10),
|
||||
.watchOS(.v3),
|
||||
],
|
||||
products: [
|
||||
@@ -20,7 +20,7 @@ let package = Package(
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
.package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.1"),
|
||||
.package(url: "https://github.com/Alamofire/Alamofire", from: "4.9.1"),
|
||||
.package(url: "https://github.com/Alamofire/Alamofire", from: "5.4.3"),
|
||||
],
|
||||
targets: [
|
||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'PetstoreClient'
|
||||
s.ios.deployment_target = '9.0'
|
||||
s.osx.deployment_target = '10.11'
|
||||
s.tvos.deployment_target = '9.0'
|
||||
s.ios.deployment_target = '10.0'
|
||||
s.osx.deployment_target = '10.12'
|
||||
s.tvos.deployment_target = '10.0'
|
||||
s.watchos.deployment_target = '3.0'
|
||||
s.version = '1.0.0'
|
||||
s.source = { :git => 'git@github.com:OpenAPITools/openapi-generator.git', :tag => 'v1.0.0' }
|
||||
@@ -11,6 +11,6 @@ Pod::Spec.new do |s|
|
||||
s.homepage = 'https://github.com/openapitools/openapi-generator'
|
||||
s.summary = 'PetstoreClient'
|
||||
s.source_files = 'PetstoreClient/Classes/**/*.swift'
|
||||
s.dependency 'Alamofire', '~> 4.9.1'
|
||||
s.dependency 'Alamofire', '~> 5.4.3'
|
||||
s.dependency 'AnyCodable-FlightSchool', '~> 0.6.1'
|
||||
end
|
||||
|
||||
@@ -18,7 +18,7 @@ class AlamofireRequestBuilderFactory: RequestBuilderFactory {
|
||||
}
|
||||
|
||||
// Store manager to retain its reference
|
||||
private var managerStore = SynchronizedDictionary<String, Alamofire.SessionManager>()
|
||||
private var managerStore = SynchronizedDictionary<String, Alamofire.Session>()
|
||||
|
||||
open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:]) {
|
||||
@@ -29,19 +29,18 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
May be overridden by a subclass if you want to control the session
|
||||
configuration.
|
||||
*/
|
||||
open func createSessionManager() -> Alamofire.SessionManager {
|
||||
open func createAlamofireSession(interceptor: RequestInterceptor? = nil) -> Alamofire.Session {
|
||||
let configuration = URLSessionConfiguration.default
|
||||
configuration.httpAdditionalHeaders = buildHeaders()
|
||||
return Alamofire.SessionManager(configuration: configuration)
|
||||
return Alamofire.Session(configuration: configuration,
|
||||
interceptor: interceptor)
|
||||
}
|
||||
|
||||
/**
|
||||
May be overridden by a subclass if you want to custom request constructor.
|
||||
*/
|
||||
open func createURLRequest() -> URLRequest? {
|
||||
guard let xMethod = Alamofire.HTTPMethod(rawValue: method) else {
|
||||
fatalError("Unsupported Http method - \(method)")
|
||||
}
|
||||
let xMethod = Alamofire.HTTPMethod(rawValue: method)
|
||||
|
||||
let encoding: ParameterEncoding
|
||||
|
||||
@@ -51,9 +50,12 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
case .options, .post, .put, .patch, .delete, .trace, .connect:
|
||||
encoding = JSONDataEncoding()
|
||||
|
||||
default:
|
||||
fatalError("Unsupported HTTPMethod - \(xMethod.rawValue)")
|
||||
}
|
||||
|
||||
guard let originalRequest = try? URLRequest(url: URLString, method: HTTPMethod(rawValue: method)!, headers: buildHeaders()) else { return nil }
|
||||
guard let originalRequest = try? URLRequest(url: URLString, method: xMethod, headers: HTTPHeaders(buildHeaders())) else { return nil }
|
||||
return try? encoding.encode(originalRequest, with: parameters)
|
||||
}
|
||||
|
||||
@@ -72,19 +74,17 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
May be overridden by a subclass if you want to control the request
|
||||
configuration (e.g. to override the cache policy).
|
||||
*/
|
||||
open func makeRequest(manager: SessionManager, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) -> DataRequest {
|
||||
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: headers)
|
||||
open func makeRequest(manager: Session, method: HTTPMethod, encoding: ParameterEncoding, headers: [String: String]) -> DataRequest {
|
||||
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding, headers: HTTPHeaders(headers))
|
||||
}
|
||||
|
||||
override open func execute(_ apiResponseQueue: DispatchQueue = PetstoreClient.apiResponseQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) {
|
||||
let managerId = UUID().uuidString
|
||||
// Create a new manager for each request to customize its request header
|
||||
let manager = createSessionManager()
|
||||
let manager = createAlamofireSession()
|
||||
managerStore[managerId] = manager
|
||||
|
||||
guard let xMethod = Alamofire.HTTPMethod(rawValue: method) else {
|
||||
fatalError("Unsupported Http method - \(method)")
|
||||
}
|
||||
let xMethod = Alamofire.HTTPMethod(rawValue: method)
|
||||
|
||||
let encoding: ParameterEncoding?
|
||||
|
||||
@@ -100,7 +100,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
} else if contentType == "multipart/form-data" {
|
||||
encoding = nil
|
||||
|
||||
manager.upload(multipartFormData: { mpForm in
|
||||
let upload = manager.upload(multipartFormData: { mpForm in
|
||||
for (k, v) in self.parameters! {
|
||||
switch v {
|
||||
case let fileURL as URL:
|
||||
@@ -117,30 +117,28 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
fatalError("Unprocessable value \(v) with key \(k)")
|
||||
}
|
||||
}
|
||||
}, to: URLString, method: xMethod, headers: nil, encodingCompletion: { encodingResult in
|
||||
switch encodingResult {
|
||||
case let .success(upload, _, _):
|
||||
if let onProgressReady = self.onProgressReady {
|
||||
onProgressReady(upload.uploadProgress)
|
||||
}
|
||||
self.processRequest(request: upload, managerId, apiResponseQueue, completion)
|
||||
case let .failure(encodingError):
|
||||
apiResponseQueue.async {
|
||||
completion(.failure(ErrorResponse.error(415, nil, nil, encodingError)))
|
||||
}
|
||||
}, to: URLString, method: xMethod, headers: nil)
|
||||
.uploadProgress { progress in
|
||||
if let onProgressReady = self.onProgressReady {
|
||||
onProgressReady(progress)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
self.processRequest(request: upload, managerId, apiResponseQueue, completion)
|
||||
} else if contentType == "application/x-www-form-urlencoded" {
|
||||
encoding = URLEncoding(destination: .httpBody)
|
||||
} else {
|
||||
fatalError("Unsupported Media Type - \(contentType)")
|
||||
}
|
||||
|
||||
default:
|
||||
fatalError("Unsupported HTTPMethod - \(xMethod.rawValue)")
|
||||
}
|
||||
|
||||
if let encoding = encoding {
|
||||
let request = makeRequest(manager: manager, method: xMethod, encoding: encoding, headers: headers)
|
||||
if let onProgressReady = self.onProgressReady {
|
||||
onProgressReady(request.progress)
|
||||
onProgressReady(request.uploadProgress)
|
||||
}
|
||||
processRequest(request: request, managerId, apiResponseQueue, completion)
|
||||
}
|
||||
@@ -148,7 +146,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
fileprivate func processRequest(request: DataRequest, _ managerId: String, _ apiResponseQueue: DispatchQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) {
|
||||
if let credential = self.credential {
|
||||
request.authenticate(usingCredential: credential)
|
||||
request.authenticate(with: credential)
|
||||
}
|
||||
|
||||
let cleanupRequest = {
|
||||
@@ -176,7 +174,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
|
||||
do {
|
||||
|
||||
guard !dataResponse.result.isFailure else {
|
||||
guard case .success = dataResponse.result else {
|
||||
throw DownloadException.responseFailed
|
||||
}
|
||||
|
||||
@@ -256,7 +254,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||
}
|
||||
|
||||
open func buildHeaders() -> [String: String] {
|
||||
var httpHeaders = SessionManager.defaultHTTPHeaders
|
||||
var httpHeaders = Alamofire.HTTPHeaders.default.dictionary
|
||||
for (key, value) in headers {
|
||||
httpHeaders[key] = value
|
||||
}
|
||||
@@ -320,7 +318,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
override fileprivate func processRequest(request: DataRequest, _ managerId: String, _ apiResponseQueue: DispatchQueue, _ completion: @escaping (_ result: Swift.Result<Response<T>, Error>) -> Void) {
|
||||
if let credential = self.credential {
|
||||
request.authenticate(usingCredential: credential)
|
||||
request.authenticate(with: credential)
|
||||
}
|
||||
|
||||
let cleanupRequest = {
|
||||
@@ -348,7 +346,7 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
do {
|
||||
|
||||
guard !dataResponse.result.isFailure else {
|
||||
guard case .success = dataResponse.result else {
|
||||
throw DownloadException.responseFailed
|
||||
}
|
||||
|
||||
@@ -413,11 +411,11 @@ open class AlamofireDecodableRequestBuilder<T: Decodable>: AlamofireRequestBuild
|
||||
|
||||
})
|
||||
default:
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { (dataResponse: DataResponse<Data>) in
|
||||
validatedRequest.responseData(queue: apiResponseQueue, completionHandler: { dataResponse in
|
||||
cleanupRequest()
|
||||
|
||||
guard dataResponse.result.isSuccess else {
|
||||
completion(.failure(ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.response, dataResponse.result.error!)))
|
||||
if case let .failure(error) = dataResponse.result {
|
||||
completion(.failure(ErrorResponse.error(dataResponse.response?.statusCode ?? 500, dataResponse.data, dataResponse.response, error)))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -326,7 +326,7 @@
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.2;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10;
|
||||
MTL_ENABLE_DEBUG_INFO = YES;
|
||||
ONLY_ACTIVE_ARCH = YES;
|
||||
SDKROOT = iphoneos;
|
||||
@@ -377,7 +377,7 @@
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 9.2;
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 10;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
SDKROOT = iphoneos;
|
||||
SWIFT_COMPILATION_MODE = wholemodule;
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
"pins": [
|
||||
{
|
||||
"package": "Alamofire",
|
||||
"repositoryURL": "https://github.com/Alamofire/Alamofire.git",
|
||||
"repositoryURL": "https://github.com/Alamofire/Alamofire",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "747c8db8d57b68d5e35275f10c92d55f982adbd4",
|
||||
"version": "4.9.1"
|
||||
"revision": "f96b619bcb2383b43d898402283924b80e2c4bae",
|
||||
"version": "5.4.3"
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -15,8 +15,8 @@
|
||||
"repositoryURL": "https://github.com/Flight-School/AnyCodable",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "876d162385e9862ae8b3c8d65dc301312b040005",
|
||||
"version": "0.6.0"
|
||||
"revision": "69261f239f0fffaf51495dadc4f8483fbfe97025",
|
||||
"version": "0.6.1"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@@ -21,49 +21,47 @@ class BearerRequestBuilderFactory: RequestBuilderFactory {
|
||||
}
|
||||
|
||||
class BearerRequestBuilder<T>: AlamofireRequestBuilder<T> {
|
||||
override func createSessionManager() -> SessionManager {
|
||||
let sessionManager = super.createSessionManager()
|
||||
|
||||
override func createAlamofireSession(interceptor: RequestInterceptor? = nil) -> Session {
|
||||
let bearerTokenHandler = BearerTokenHandler()
|
||||
sessionManager.adapter = bearerTokenHandler
|
||||
sessionManager.retrier = bearerTokenHandler
|
||||
|
||||
let alamofireSession = super.createAlamofireSession(interceptor: bearerTokenHandler)
|
||||
|
||||
return sessionManager
|
||||
return alamofireSession
|
||||
}
|
||||
}
|
||||
|
||||
class BearerDecodableRequestBuilder<T: Decodable>: AlamofireDecodableRequestBuilder<T> {
|
||||
override func createSessionManager() -> SessionManager {
|
||||
let sessionManager = super.createSessionManager()
|
||||
|
||||
override func createAlamofireSession(interceptor: RequestInterceptor? = nil) -> Session {
|
||||
let bearerTokenHandler = BearerTokenHandler()
|
||||
sessionManager.adapter = bearerTokenHandler
|
||||
sessionManager.retrier = bearerTokenHandler
|
||||
|
||||
return sessionManager
|
||||
let alamofireSession = super.createAlamofireSession(interceptor: bearerTokenHandler)
|
||||
|
||||
return alamofireSession
|
||||
}
|
||||
}
|
||||
|
||||
class BearerTokenHandler: RequestAdapter, RequestRetrier {
|
||||
class BearerTokenHandler: RequestInterceptor {
|
||||
private static var bearerToken: String? = nil
|
||||
|
||||
func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
|
||||
func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Result<URLRequest, Error>) -> Void) {
|
||||
if let bearerToken = Self.bearerToken {
|
||||
var urlRequest = urlRequest
|
||||
urlRequest.setValue("Bearer \(bearerToken)", forHTTPHeaderField: "Authorization")
|
||||
return urlRequest
|
||||
|
||||
completion(.success(urlRequest))
|
||||
return
|
||||
}
|
||||
|
||||
return urlRequest
|
||||
completion(.success(urlRequest))
|
||||
}
|
||||
|
||||
func should(_: SessionManager, retry request: Request, with _: Error, completion: @escaping RequestRetryCompletion) {
|
||||
func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
|
||||
if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 401 {
|
||||
Self.startRefreshingToken { isTokenRefreshed in
|
||||
completion(isTokenRefreshed, 0.0)
|
||||
completion(.retry)
|
||||
}
|
||||
} else {
|
||||
completion(false, 0.0)
|
||||
completion(.doNotRetryWithError(error))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user