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:
Ewan Mellor 2016-11-21 08:19:17 -08:00 committed by wing328
parent 515e723fae
commit 9dc809bdc7
4 changed files with 104 additions and 8 deletions

View File

@ -30,6 +30,25 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
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) {
let managerId:String = UUID().uuidString
// 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! {
switch v {
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
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
@ -72,7 +96,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
}
})
} 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 {
onProgressReady(request.progress)
}

View File

@ -30,6 +30,25 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
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) {
let managerId:String = UUID().uuidString
// 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! {
switch v {
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
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
@ -72,7 +96,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
}
})
} 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 {
onProgressReady(request.progress)
}

View File

@ -30,6 +30,25 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
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) {
let managerId:String = UUID().uuidString
// 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! {
switch v {
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
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
@ -72,7 +96,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
}
})
} 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 {
onProgressReady(request.progress)
}

View File

@ -30,6 +30,25 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
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) {
let managerId:String = UUID().uuidString
// 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! {
switch v {
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
case let string as String:
mpForm.append(string.data(using: String.Encoding.utf8)!, withName: k)
@ -72,7 +96,7 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
}
})
} 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 {
onProgressReady(request.progress)
}