From 9dc809bdc7c6dca5dc4c71b9669e183ee755450f Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Mon, 21 Nov 2016 08:19:17 -0800 Subject: [PATCH] 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. --- .../swift3/AlamofireImplementations.mustache | 28 +++++++++++++++++-- .../Swaggers/AlamofireImplementations.swift | 28 +++++++++++++++++-- .../Swaggers/AlamofireImplementations.swift | 28 +++++++++++++++++-- .../Swaggers/AlamofireImplementations.swift | 28 +++++++++++++++++-- 4 files changed, 104 insertions(+), 8 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/swift3/AlamofireImplementations.mustache b/modules/swagger-codegen/src/main/resources/swift3/AlamofireImplementations.mustache index 03e421b7f10..2857c85889d 100644 --- a/modules/swagger-codegen/src/main/resources/swift3/AlamofireImplementations.mustache +++ b/modules/swagger-codegen/src/main/resources/swift3/AlamofireImplementations.mustache @@ -30,6 +30,25 @@ open class AlamofireRequestBuilder: RequestBuilder { 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?, _ 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: RequestBuilder { 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: RequestBuilder { } }) } 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) } diff --git a/samples/client/petstore/swift3/default/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift b/samples/client/petstore/swift3/default/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift index 03e421b7f10..2857c85889d 100644 --- a/samples/client/petstore/swift3/default/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift +++ b/samples/client/petstore/swift3/default/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift @@ -30,6 +30,25 @@ open class AlamofireRequestBuilder: RequestBuilder { 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?, _ 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: RequestBuilder { 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: RequestBuilder { } }) } 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) } diff --git a/samples/client/petstore/swift3/promisekit/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift b/samples/client/petstore/swift3/promisekit/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift index 03e421b7f10..2857c85889d 100644 --- a/samples/client/petstore/swift3/promisekit/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift +++ b/samples/client/petstore/swift3/promisekit/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift @@ -30,6 +30,25 @@ open class AlamofireRequestBuilder: RequestBuilder { 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?, _ 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: RequestBuilder { 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: RequestBuilder { } }) } 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) } diff --git a/samples/client/petstore/swift3/rxswift/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift b/samples/client/petstore/swift3/rxswift/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift index 03e421b7f10..2857c85889d 100644 --- a/samples/client/petstore/swift3/rxswift/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift +++ b/samples/client/petstore/swift3/rxswift/PetstoreClient/Classes/Swaggers/AlamofireImplementations.swift @@ -30,6 +30,25 @@ open class AlamofireRequestBuilder: RequestBuilder { 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?, _ 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: RequestBuilder { 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: RequestBuilder { } }) } 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) }