Merge pull request #1007 from ngs/swift-alamofire-1.3.0

[Swift] Support file upload with Alamofire 1.3.0
This commit is contained in:
wing328 2015-07-27 08:29:34 +08:00
commit 9ce824d720
5 changed files with 47 additions and 7 deletions

View File

@ -100,7 +100,7 @@ public class SwiftCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("number", "Double"); typeMapping.put("number", "Double");
typeMapping.put("double", "Double"); typeMapping.put("double", "Double");
typeMapping.put("object", "String"); typeMapping.put("object", "String");
typeMapping.put("file", "NSData"); typeMapping.put("file", "NSURL");
importMapping = new HashMap<String, String>(); importMapping = new HashMap<String, String>();

View File

@ -29,7 +29,47 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
managerStore[managerId] = manager managerStore[managerId] = manager
let encoding = isBody ? Alamofire.ParameterEncoding.JSON : Alamofire.ParameterEncoding.URL let encoding = isBody ? Alamofire.ParameterEncoding.JSON : Alamofire.ParameterEncoding.URL
let request = manager.request(Alamofire.Method(rawValue: method)!, URLString, parameters: parameters, encoding: encoding) let xMethod = Alamofire.Method(rawValue: method)
let fileKeys = parameters == nil ? [] : map(filter(parameters!) { $1.isKindOfClass(NSURL) }) { $0.0 }
if fileKeys.count > 0 {
manager.upload(
xMethod!, URLString, headers: nil,
multipartFormData: { mpForm in
for (k, v) in self.parameters! {
switch v {
case let fileURL as NSURL:
mpForm.appendBodyPart(fileURL: fileURL, name: k)
break
case let string as NSString:
mpForm.appendBodyPart(data: string.dataUsingEncoding(NSUTF8StringEncoding)!, name: k)
break
case let number as NSNumber:
mpForm.appendBodyPart(data: number.stringValue.dataUsingEncoding(NSUTF8StringEncoding)!, name: k)
break
default:
fatalError("Unprocessable value \(v) with key \(k)")
break
}
}
},
encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
self.processRequest(upload, managerId, completion)
case .Failure(let encodingError):
completion(response: nil, erorr: encodingError)
}
}
)
} else {
processRequest(manager.request(xMethod!, URLString, parameters: parameters, encoding: encoding), managerId, completion)
}
}
private func processRequest(request: Request, _ managerId: String, _ completion: (response: Response<T>?, erorr: NSError?) -> Void) {
if let credential = self.credential { if let credential = self.credential {
request.authenticate(usingCredential: credential) request.authenticate(usingCredential: credential)
} }

View File

@ -1,2 +1,2 @@
github "Alamofire/Alamofire" >= 1.2 github "Alamofire/Alamofire" >= 1.3
github "mxcl/PromiseKit" >=1.5.3 github "mxcl/PromiseKit" >=1.5.3

View File

@ -15,5 +15,5 @@ Pod::Spec.new do |s|
s.documentation_url = '{{podDocumentationURL}}'{{/podDocumentationURL}} s.documentation_url = '{{podDocumentationURL}}'{{/podDocumentationURL}}
s.source_files = '{{projectName}}/Classes/Swaggers/**/*.swift' s.source_files = '{{projectName}}/Classes/Swaggers/**/*.swift'
s.dependency 'PromiseKit', '~> 2.1' s.dependency 'PromiseKit', '~> 2.1'
s.dependency 'Alamofire', '~> 1.2' s.dependency 'Alamofire', '~> 1.3'
end end

View File

@ -35,7 +35,7 @@ extension {{projectName}}API {
public class func {{operationId}}({{#allParams}}{{^secondaryParam}}#{{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> { public class func {{operationId}}({{#allParams}}{{^secondaryParam}}#{{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{path}}"{{#pathParams}} {{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{path}}"{{#pathParams}}
path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}} path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}}
let url = {{projectName}}API.basePath + path let URLString = {{projectName}}API.basePath + path
{{#bodyParam}} {{#bodyParam}}
let parameters = {{paramName}}{{^required}}?{{/required}}.encodeToJSON() as? [String:AnyObject]{{/bodyParam}}{{^bodyParam}} let parameters = {{paramName}}{{^required}}?{{/required}}.encodeToJSON() as? [String:AnyObject]{{/bodyParam}}{{^bodyParam}}
let nillableParameters: [String:AnyObject?] = {{^queryParams}}{{^formParams}}[:]{{/formParams}}{{#formParams}}{{^secondaryParam}}[{{/secondaryParam}} let nillableParameters: [String:AnyObject?] = {{^queryParams}}{{^formParams}}[:]{{/formParams}}{{#formParams}}{{^secondaryParam}}[{{/secondaryParam}}
@ -47,7 +47,7 @@ extension {{projectName}}API {
let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.getBuilder() let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.getBuilder()
return requestBuilder(method: "{{httpMethod}}", URLString: url, parameters: parameters, isBody: {{^queryParams}}{{^formParams}}true{{/formParams}}{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}}{{#formParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/formParams}}) return requestBuilder(method: "{{httpMethod}}", URLString: URLString, parameters: parameters, isBody: {{^queryParams}}{{^formParams}}true{{/formParams}}{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}}{{#formParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/formParams}})
} }
{{/operation}} {{/operation}}
} }