Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
],
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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}}
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [:]) {
Expand All @@ -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

Expand All @@ -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)
}

Expand All @@ -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?

Expand All @@ -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:
Expand All @@ -117,38 +117,36 @@ 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)
}
}

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 = {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/swift5/alamofireLibrary/Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "Alamofire/Alamofire" ~> 4.9.1
github "Alamofire/Alamofire" ~> 5.4.3
github "Flight-School/AnyCodable" ~> 0.6.1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions samples/client/petstore/swift5/alamofireLibrary/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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' }
Expand All @@ -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
Loading