forked from loafle/openapi-generator-original
Add two override points inside AlamofireRequestBuilder in the Swift 3 template. (#4170)
* Add two override points inside AlamofireRequestBuilder in the Swift 3 template. These allow the caller to control the request configuration (e.g. to override the cache policy) and to control the Content-Type that is given to an uploaded form part. * Regenerate with ./bin/swift3-petstore-all.sh to match recent changes. This includes a few minor changes that weren't made in this branch, so this apparently wasn't run on master after some other recent changes.
This commit is contained in:
parent
515e723fae
commit
9dc809bdc7
@ -30,6 +30,25 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
return Alamofire.SessionManager(configuration: configuration)
|
return Alamofire.SessionManager(configuration: configuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
May be overridden by a subclass if you want to control the Content-Type
|
||||||
|
that is given to an uploaded form part.
|
||||||
|
|
||||||
|
Return nil to use the default behavior (inferring the Content-Type from
|
||||||
|
the file extension). Return the desired Content-Type otherwise.
|
||||||
|
*/
|
||||||
|
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
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) -> DataRequest {
|
||||||
|
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding)
|
||||||
|
}
|
||||||
|
|
||||||
override open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
override open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
||||||
let managerId:String = UUID().uuidString
|
let managerId:String = UUID().uuidString
|
||||||
// Create a new manager for each request to customize its request header
|
// Create a new manager for each request to customize its request header
|
||||||
@ -47,7 +66,12 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
for (k, v) in self.parameters! {
|
for (k, v) in self.parameters! {
|
||||||
switch v {
|
switch v {
|
||||||
case let fileURL as URL:
|
case let fileURL as URL:
|
||||||
mpForm.append(fileURL, withName: k)
|
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
|
||||||
|
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mpForm.append(fileURL, withName: k)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case let string as String:
|
case let string as String:
|
||||||
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
|
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
|
||||||
@ -72,7 +96,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let request = manager.request(URLString, method: xMethod!, parameters: parameters, encoding: encoding)
|
let request = makeRequest(manager: manager, method: xMethod!, encoding: encoding)
|
||||||
if let onProgressReady = self.onProgressReady {
|
if let onProgressReady = self.onProgressReady {
|
||||||
onProgressReady(request.progress)
|
onProgressReady(request.progress)
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,25 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
return Alamofire.SessionManager(configuration: configuration)
|
return Alamofire.SessionManager(configuration: configuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
May be overridden by a subclass if you want to control the Content-Type
|
||||||
|
that is given to an uploaded form part.
|
||||||
|
|
||||||
|
Return nil to use the default behavior (inferring the Content-Type from
|
||||||
|
the file extension). Return the desired Content-Type otherwise.
|
||||||
|
*/
|
||||||
|
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
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) -> DataRequest {
|
||||||
|
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding)
|
||||||
|
}
|
||||||
|
|
||||||
override open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
override open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
||||||
let managerId:String = UUID().uuidString
|
let managerId:String = UUID().uuidString
|
||||||
// Create a new manager for each request to customize its request header
|
// Create a new manager for each request to customize its request header
|
||||||
@ -47,7 +66,12 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
for (k, v) in self.parameters! {
|
for (k, v) in self.parameters! {
|
||||||
switch v {
|
switch v {
|
||||||
case let fileURL as URL:
|
case let fileURL as URL:
|
||||||
mpForm.append(fileURL, withName: k)
|
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
|
||||||
|
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mpForm.append(fileURL, withName: k)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case let string as String:
|
case let string as String:
|
||||||
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
|
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
|
||||||
@ -72,7 +96,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let request = manager.request(URLString, method: xMethod!, parameters: parameters, encoding: encoding)
|
let request = makeRequest(manager: manager, method: xMethod!, encoding: encoding)
|
||||||
if let onProgressReady = self.onProgressReady {
|
if let onProgressReady = self.onProgressReady {
|
||||||
onProgressReady(request.progress)
|
onProgressReady(request.progress)
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,25 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
return Alamofire.SessionManager(configuration: configuration)
|
return Alamofire.SessionManager(configuration: configuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
May be overridden by a subclass if you want to control the Content-Type
|
||||||
|
that is given to an uploaded form part.
|
||||||
|
|
||||||
|
Return nil to use the default behavior (inferring the Content-Type from
|
||||||
|
the file extension). Return the desired Content-Type otherwise.
|
||||||
|
*/
|
||||||
|
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
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) -> DataRequest {
|
||||||
|
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding)
|
||||||
|
}
|
||||||
|
|
||||||
override open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
override open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
||||||
let managerId:String = UUID().uuidString
|
let managerId:String = UUID().uuidString
|
||||||
// Create a new manager for each request to customize its request header
|
// Create a new manager for each request to customize its request header
|
||||||
@ -47,7 +66,12 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
for (k, v) in self.parameters! {
|
for (k, v) in self.parameters! {
|
||||||
switch v {
|
switch v {
|
||||||
case let fileURL as URL:
|
case let fileURL as URL:
|
||||||
mpForm.append(fileURL, withName: k)
|
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
|
||||||
|
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mpForm.append(fileURL, withName: k)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case let string as String:
|
case let string as String:
|
||||||
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
|
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
|
||||||
@ -72,7 +96,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let request = manager.request(URLString, method: xMethod!, parameters: parameters, encoding: encoding)
|
let request = makeRequest(manager: manager, method: xMethod!, encoding: encoding)
|
||||||
if let onProgressReady = self.onProgressReady {
|
if let onProgressReady = self.onProgressReady {
|
||||||
onProgressReady(request.progress)
|
onProgressReady(request.progress)
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,25 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
return Alamofire.SessionManager(configuration: configuration)
|
return Alamofire.SessionManager(configuration: configuration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
May be overridden by a subclass if you want to control the Content-Type
|
||||||
|
that is given to an uploaded form part.
|
||||||
|
|
||||||
|
Return nil to use the default behavior (inferring the Content-Type from
|
||||||
|
the file extension). Return the desired Content-Type otherwise.
|
||||||
|
*/
|
||||||
|
open func contentTypeForFormPart(fileURL: URL) -> String? {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
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) -> DataRequest {
|
||||||
|
return manager.request(URLString, method: method, parameters: parameters, encoding: encoding)
|
||||||
|
}
|
||||||
|
|
||||||
override open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
override open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: Error?) -> Void) {
|
||||||
let managerId:String = UUID().uuidString
|
let managerId:String = UUID().uuidString
|
||||||
// Create a new manager for each request to customize its request header
|
// Create a new manager for each request to customize its request header
|
||||||
@ -47,7 +66,12 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
for (k, v) in self.parameters! {
|
for (k, v) in self.parameters! {
|
||||||
switch v {
|
switch v {
|
||||||
case let fileURL as URL:
|
case let fileURL as URL:
|
||||||
mpForm.append(fileURL, withName: k)
|
if let mimeType = self.contentTypeForFormPart(fileURL: fileURL) {
|
||||||
|
mpForm.append(fileURL, withName: k, fileName: fileURL.lastPathComponent, mimeType: mimeType)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mpForm.append(fileURL, withName: k)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case let string as String:
|
case let string as String:
|
||||||
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
|
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
|
||||||
@ -72,7 +96,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
let request = manager.request(URLString, method: xMethod!, parameters: parameters, encoding: encoding)
|
let request = makeRequest(manager: manager, method: xMethod!, encoding: encoding)
|
||||||
if let onProgressReady = self.onProgressReady {
|
if let onProgressReady = self.onProgressReady {
|
||||||
onProgressReady(request.progress)
|
onProgressReady(request.progress)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user