forked from loafle/openapi-generator-original
Merge pull request #1187 from Edubits/swift-2.0
[Swift] Update generated code to Swift 2.0
This commit is contained in:
commit
ee7f447f77
@ -18,7 +18,7 @@ public class APIBase {
|
|||||||
|
|
||||||
if encoded! is [AnyObject] {
|
if encoded! is [AnyObject] {
|
||||||
var dictionary = [String:AnyObject]()
|
var dictionary = [String:AnyObject]()
|
||||||
for (index, item) in enumerate(encoded as! [AnyObject]) {
|
for (index, item) in (encoded as! [AnyObject]).enumerate() {
|
||||||
dictionary["\(index)"] = item
|
dictionary["\(index)"] = item
|
||||||
}
|
}
|
||||||
return dictionary
|
return dictionary
|
||||||
@ -43,9 +43,9 @@ public class RequestBuilder<T> {
|
|||||||
self.isBody = isBody
|
self.isBody = isBody
|
||||||
}
|
}
|
||||||
|
|
||||||
public func execute(completion: (response: Response<T>?, erorr: NSError?) -> Void) { }
|
public func execute(completion: (response: Response<T>?, erorr: ErrorType?) -> Void) { }
|
||||||
|
|
||||||
public func addHeader(#name: String, value: String) -> Self {
|
public func addHeader(name name: String, value: String) -> Self {
|
||||||
if !value.isEmpty {
|
if !value.isEmpty {
|
||||||
headers[name] = value
|
headers[name] = value
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
|
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
override func execute(completion: (response: Response<T>?, erorr: NSError?) -> Void) {
|
override func execute(completion: (response: Response<T>?, erorr: ErrorType?) -> Void) {
|
||||||
let managerId = NSUUID().UUIDString
|
let managerId = NSUUID().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
|
||||||
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
|
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
|
||||||
@ -30,7 +30,8 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
|
|
||||||
let encoding = isBody ? Alamofire.ParameterEncoding.JSON : Alamofire.ParameterEncoding.URL
|
let encoding = isBody ? Alamofire.ParameterEncoding.JSON : Alamofire.ParameterEncoding.URL
|
||||||
let xMethod = Alamofire.Method(rawValue: method)
|
let xMethod = Alamofire.Method(rawValue: method)
|
||||||
let fileKeys = parameters == nil ? [] : map(filter(parameters!) { $1.isKindOfClass(NSURL) }) { $0.0 }
|
let fileKeys = parameters == nil ? [] : parameters!.filter { $1.isKindOfClass(NSURL) }
|
||||||
|
.map { $0.0 }
|
||||||
|
|
||||||
if fileKeys.count > 0 {
|
if fileKeys.count > 0 {
|
||||||
manager.upload(
|
manager.upload(
|
||||||
@ -69,23 +70,16 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func processRequest(request: Request, _ managerId: String, _ completion: (response: Response<T>?, erorr: NSError?) -> Void) {
|
private func processRequest(request: Request, _ managerId: String, _ completion: (response: Response<T>?, erorr: ErrorType?) -> Void) {
|
||||||
if let credential = self.credential {
|
if let credential = self.credential {
|
||||||
request.authenticate(usingCredential: credential)
|
request.authenticate(usingCredential: credential)
|
||||||
}
|
}
|
||||||
|
|
||||||
request.responseJSON(options: .AllowFragments) { (req, res, json, error) in
|
request.responseJSON(options: .AllowFragments) { (req, res, result) in
|
||||||
managerStore.removeValueForKey(managerId)
|
managerStore.removeValueForKey(managerId)
|
||||||
|
|
||||||
if let error = error {
|
if result.isFailure {
|
||||||
completion(response: nil, erorr: error)
|
completion(response: nil, erorr: result.error)
|
||||||
return
|
|
||||||
}
|
|
||||||
if res!.statusCode >= 400 {
|
|
||||||
//TODO: Add error entity
|
|
||||||
let userInfo: [NSObject : AnyObject] = (json != nil) ? ["data": json!] : [:]
|
|
||||||
let error = NSError(domain: res!.URL!.URLString, code: res!.statusCode, userInfo: userInfo)
|
|
||||||
completion(response: nil, erorr: error)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +88,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
|||||||
completion(response: response, erorr: nil)
|
completion(response: response, erorr: nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if let json: AnyObject = json {
|
if let json: AnyObject = result.value {
|
||||||
let body = Decoders.decode(clazz: T.self, source: json)
|
let body = Decoders.decode(clazz: T.self, source: json)
|
||||||
let response = Response(response: res!, body: body)
|
let response = Response(response: res!, body: body)
|
||||||
completion(response: response, erorr: nil)
|
completion(response: response, erorr: nil)
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
github "Alamofire/Alamofire" >= 1.3
|
github "Alamofire/Alamofire" >= 2.0.0{{#usePromiseKit}}
|
||||||
github "mxcl/PromiseKit" >=1.5.3
|
github "mxcl/PromiseKit" >=1.5.3{{/usePromiseKit}}
|
||||||
|
@ -35,17 +35,17 @@ private var once = dispatch_once_t()
|
|||||||
class Decoders {
|
class Decoders {
|
||||||
static private var decoders = Dictionary<String, ((AnyObject) -> AnyObject)>()
|
static private var decoders = Dictionary<String, ((AnyObject) -> AnyObject)>()
|
||||||
|
|
||||||
static func addDecoder<T>(#clazz: T.Type, decoder: ((AnyObject) -> T)) {
|
static func addDecoder<T>(clazz clazz: T.Type, decoder: ((AnyObject) -> T)) {
|
||||||
let key = "\(T.self)"
|
let key = "\(T.self)"
|
||||||
decoders[key] = { decoder($0) as! AnyObject }
|
decoders[key] = { decoder($0) as! AnyObject }
|
||||||
}
|
}
|
||||||
|
|
||||||
static func decode<T>(#clazz: [T].Type, source: AnyObject) -> [T] {
|
static func decode<T>(clazz clazz: [T].Type, source: AnyObject) -> [T] {
|
||||||
let array = source as! [AnyObject]
|
let array = source as! [AnyObject]
|
||||||
return array.map { Decoders.decode(clazz: T.self, source: $0) }
|
return array.map { Decoders.decode(clazz: T.self, source: $0) }
|
||||||
}
|
}
|
||||||
|
|
||||||
static func decode<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
|
static func decode<T, Key: Hashable>(clazz clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
|
||||||
let sourceDictinoary = source as! [Key: AnyObject]
|
let sourceDictinoary = source as! [Key: AnyObject]
|
||||||
var dictionary = [Key:T]()
|
var dictionary = [Key:T]()
|
||||||
for (key, value) in sourceDictinoary {
|
for (key, value) in sourceDictinoary {
|
||||||
@ -54,7 +54,7 @@ class Decoders {
|
|||||||
return dictionary
|
return dictionary
|
||||||
}
|
}
|
||||||
|
|
||||||
static func decode<T>(#clazz: T.Type, source: AnyObject) -> T {
|
static func decode<T>(clazz clazz: T.Type, source: AnyObject) -> T {
|
||||||
initialize()
|
initialize()
|
||||||
if source is T {
|
if source is T {
|
||||||
return source as! T
|
return source as! T
|
||||||
@ -68,7 +68,7 @@ class Decoders {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func decodeOptional<T>(#clazz: T.Type, source: AnyObject?) -> T? {
|
static func decodeOptional<T>(clazz clazz: T.Type, source: AnyObject?) -> T? {
|
||||||
if source is NSNull {
|
if source is NSNull {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ class Decoders {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func decodeOptional<T>(#clazz: [T].Type, source: AnyObject?) -> [T]? {
|
static func decodeOptional<T>(clazz clazz: [T].Type, source: AnyObject?) -> [T]? {
|
||||||
if source is NSNull {
|
if source is NSNull {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -86,7 +86,7 @@ class Decoders {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func decodeOptional<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
|
static func decodeOptional<T, Key: Hashable>(clazz clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
|
||||||
if source is NSNull {
|
if source is NSNull {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -100,6 +100,7 @@ class Decoders {
|
|||||||
let formatters = [
|
let formatters = [
|
||||||
"yyyy-MM-dd",
|
"yyyy-MM-dd",
|
||||||
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
|
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
|
||||||
|
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
|
||||||
"yyyy-MM-dd'T'HH:mm:ss'Z'"
|
"yyyy-MM-dd'T'HH:mm:ss'Z'"
|
||||||
].map { (format: String) -> NSDateFormatter in
|
].map { (format: String) -> NSDateFormatter in
|
||||||
let formatter = NSDateFormatter()
|
let formatter = NSDateFormatter()
|
||||||
@ -120,7 +121,7 @@ class Decoders {
|
|||||||
// Decoder for {{{classname}}}
|
// Decoder for {{{classname}}}
|
||||||
Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject) -> {{{classname}}} in
|
Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject) -> {{{classname}}} in
|
||||||
let sourceDictionary = source as! [NSObject:AnyObject]
|
let sourceDictionary = source as! [NSObject:AnyObject]
|
||||||
var instance = {{classname}}(){{#vars}}{{#isEnum}}
|
let instance = {{classname}}(){{#vars}}{{#isEnum}}
|
||||||
instance.{{name}} = (sourceDictionary["{{name}}"] as? String).map { {{classname}}.{{datatypeWithEnum}}(rawValue: $0)! }{{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}} {{/isEnum}}{{^isEnum}}
|
instance.{{name}} = (sourceDictionary["{{name}}"] as? String).map { {{classname}}.{{datatypeWithEnum}}(rawValue: $0)! }{{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}} {{/isEnum}}{{^isEnum}}
|
||||||
instance.{{name}} = Decoders.decode{{^unwrapRequired}}Optional{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}Optional{{/required}}{{/unwrapRequired}}(clazz: {{{baseType}}}.self, source: sourceDictionary["{{name}}"]{{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}}){{/isEnum}}{{/vars}}
|
instance.{{name}} = Decoders.decode{{^unwrapRequired}}Optional{{/unwrapRequired}}{{#unwrapRequired}}{{^required}}Optional{{/required}}{{/unwrapRequired}}(clazz: {{{baseType}}}.self, source: sourceDictionary["{{name}}"]{{#unwrapRequired}}{{#required}}!{{/required}}{{/unwrapRequired}}){{/isEnum}}{{/vars}}
|
||||||
return instance
|
return instance
|
||||||
|
@ -14,7 +14,7 @@ Pod::Spec.new do |s|
|
|||||||
s.description = '{{podDescription}}'{{/podDescription}}{{#podScreenshots}}
|
s.description = '{{podDescription}}'{{/podDescription}}{{#podScreenshots}}
|
||||||
s.screenshots = {{& podScreenshots}}{{/podScreenshots}}{{#podDocumentationURL}}
|
s.screenshots = {{& podScreenshots}}{{/podScreenshots}}{{#podDocumentationURL}}
|
||||||
s.documentation_url = '{{podDocumentationURL}}'{{/podDocumentationURL}}
|
s.documentation_url = '{{podDocumentationURL}}'{{/podDocumentationURL}}
|
||||||
s.source_files = '{{projectName}}/Classes/Swaggers/**/*.swift'
|
s.source_files = '{{projectName}}/Classes/Swaggers/**/*.swift'{{#usePromiseKit}}
|
||||||
s.dependency 'PromiseKit', '~> 2.1'
|
s.dependency 'PromiseKit', '~> 2.1'{{/usePromiseKit}}
|
||||||
s.dependency 'Alamofire', '~> 1.3'
|
s.dependency 'Alamofire', '~> 2.0.0'
|
||||||
end
|
end
|
||||||
|
@ -5,8 +5,8 @@
|
|||||||
// https://github.com/swagger-api/swagger-codegen
|
// https://github.com/swagger-api/swagger-codegen
|
||||||
//
|
//
|
||||||
|
|
||||||
import Alamofire
|
import Alamofire{{#usePromiseKit}}
|
||||||
import PromiseKit
|
import PromiseKit{{/usePromiseKit}}
|
||||||
|
|
||||||
extension {{projectName}}API {
|
extension {{projectName}}API {
|
||||||
{{#description}}
|
{{#description}}
|
||||||
@ -28,11 +28,11 @@ extension {{projectName}}API {
|
|||||||
- examples: {{{examples}}}{{/examples}}{{#externalDocs}}
|
- examples: {{{examples}}}{{/examples}}{{#externalDocs}}
|
||||||
- externalDocs: {{externalDocs}}{{/externalDocs}}{{#hasParams}}
|
- externalDocs: {{externalDocs}}{{/externalDocs}}{{#hasParams}}
|
||||||
{{/hasParams}}{{#allParams}}
|
{{/hasParams}}{{#allParams}}
|
||||||
:param: {{paramName}} ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
|
- parameter {{paramName}}: ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
|
||||||
|
|
||||||
:returns: Promise<Response<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>> {{description}}
|
- returns: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{description}}
|
||||||
*/
|
*/
|
||||||
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}}{{paramName}} {{/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 URLString = {{projectName}}API.basePath + path
|
let URLString = {{projectName}}API.basePath + path
|
||||||
@ -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: URLString, parameters: parameters, isBody: {{^queryParams}}{{^formParams}}true{{/formParams}}{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}}{{#formParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/formParams}})
|
return requestBuilder.init(method: "{{httpMethod}}", URLString: URLString, parameters: parameters, isBody: {{^queryParams}}{{^formParams}}true{{/formParams}}{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}}{{#formParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/formParams}})
|
||||||
}
|
}
|
||||||
{{/operation}}
|
{{/operation}}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user