forked from loafle/openapi-generator-original
rollback swift template
This commit is contained in:
parent
d8060f47a8
commit
410144ea1c
@ -29,8 +29,7 @@ return encoded as? [String:AnyObject]
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RequestBuilder
|
class RequestBuilder<T> {
|
||||||
<T> {
|
|
||||||
var credential: NSURLCredential?
|
var credential: NSURLCredential?
|
||||||
var headers: [String:String] = [:]
|
var headers: [String:String] = [:]
|
||||||
let parameters: [String:AnyObject]?
|
let parameters: [String:AnyObject]?
|
||||||
@ -45,9 +44,7 @@ class RequestBuilder
|
|||||||
self.isBody = isBody
|
self.isBody = isBody
|
||||||
}
|
}
|
||||||
|
|
||||||
func execute() -> Promise
|
func execute() -> Promise<Response<T>> { fatalError("Not implemented") }
|
||||||
<Response
|
|
||||||
<T>> { fatalError("Not implemented") }
|
|
||||||
|
|
||||||
func addHeader(#name: String, value: String) -> Self {
|
func addHeader(#name: String, value: String) -> Self {
|
||||||
if !value.isEmpty {
|
if !value.isEmpty {
|
||||||
@ -63,9 +60,7 @@ class RequestBuilder
|
|||||||
}
|
}
|
||||||
|
|
||||||
protocol RequestBuilderFactory {
|
protocol RequestBuilderFactory {
|
||||||
func getBuilder
|
func getBuilder<T>() -> RequestBuilder<T>.Type
|
||||||
<T>() -> RequestBuilder
|
|
||||||
<T>.Type
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,27 +8,20 @@ import Alamofire
|
|||||||
import PromiseKit
|
import PromiseKit
|
||||||
|
|
||||||
class AlamofireRequestBuilderFactory: RequestBuilderFactory {
|
class AlamofireRequestBuilderFactory: RequestBuilderFactory {
|
||||||
func getBuilder
|
func getBuilder<T>() -> RequestBuilder<T>.Type {
|
||||||
<T>() -> RequestBuilder
|
return AlamofireRequestBuilder<T>.self
|
||||||
<T>.Type {
|
|
||||||
return AlamofireRequestBuilder
|
|
||||||
<T>.self
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store manager to retain its reference
|
// Store manager to retain its reference
|
||||||
private var managerStore: [String: Alamofire.Manager] = [:]
|
private var managerStore: [String: Alamofire.Manager] = [:]
|
||||||
|
|
||||||
class AlamofireRequestBuilder
|
class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||||
<T>: RequestBuilder
|
|
||||||
<T> {
|
|
||||||
required init(method: String, URLString: String, parameters: [String : AnyObject]?, isBody: Bool) {
|
required init(method: String, URLString: String, parameters: [String : AnyObject]?, isBody: Bool) {
|
||||||
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
|
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
override func execute() -> Promise
|
override func execute() -> Promise<Response<T>> {
|
||||||
<Response
|
|
||||||
<T>> {
|
|
||||||
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()
|
||||||
@ -37,15 +30,12 @@ func getBuilder
|
|||||||
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:
|
let request = manager.request(Alamofire.Method(rawValue: method)!, URLString, parameters: parameters, encoding: encoding)
|
||||||
parameters, encoding: encoding)
|
|
||||||
if let credential = self.credential {
|
if let credential = self.credential {
|
||||||
request.authenticate(usingCredential: credential)
|
request.authenticate(usingCredential: credential)
|
||||||
}
|
}
|
||||||
|
|
||||||
let defer = Promise
|
let defer = Promise<Response<T>>.defer()
|
||||||
<Response
|
|
||||||
<T>>.defer()
|
|
||||||
request.responseJSON(options: .AllowFragments) { (req, res, json, error) in
|
request.responseJSON(options: .AllowFragments) { (req, res, json, error) in
|
||||||
managerStore.removeValueForKey(managerId)
|
managerStore.removeValueForKey(managerId)
|
||||||
|
|
||||||
@ -72,8 +62,7 @@ func getBuilder
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable
|
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable code"]))
|
||||||
code"]))
|
|
||||||
}
|
}
|
||||||
return defer.promise
|
return defer.promise
|
||||||
}
|
}
|
||||||
|
@ -27,8 +27,7 @@ extension String: JSONEncodable {
|
|||||||
func encode() -> AnyObject { return self }
|
func encode() -> AnyObject { return self }
|
||||||
}
|
}
|
||||||
|
|
||||||
private func encodeIfPossible
|
private func encodeIfPossible<T>(object: T) -> AnyObject {
|
||||||
<T>(object: T) -> AnyObject {
|
|
||||||
if object is JSONEncodable {
|
if object is JSONEncodable {
|
||||||
return (object as! JSONEncodable).encode()
|
return (object as! JSONEncodable).encode()
|
||||||
} else {
|
} else {
|
||||||
|
@ -10,8 +10,7 @@ protocol JSONEncodable {
|
|||||||
func encode() -> AnyObject
|
func encode() -> AnyObject
|
||||||
}
|
}
|
||||||
|
|
||||||
class Response
|
class Response<T> {
|
||||||
<T> {
|
|
||||||
let statusCode: Int
|
let statusCode: Int
|
||||||
let header: [String: String]
|
let header: [String: String]
|
||||||
let body: T
|
let body: T
|
||||||
@ -34,25 +33,19 @@ class Response
|
|||||||
|
|
||||||
private var once = dispatch_once_t()
|
private var once = dispatch_once_t()
|
||||||
class Decoders {
|
class Decoders {
|
||||||
static private var decoders = Dictionary
|
static private var decoders = Dictionary<String, ((AnyObject) -> AnyObject)>()
|
||||||
<String
|
|
||||||
, ((AnyObject) -> AnyObject)>()
|
|
||||||
|
|
||||||
static func addDecoder
|
static func addDecoder<T>(#clazz: T.Type, decoder: ((AnyObject) -> T)) {
|
||||||
<T>(#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
|
static func decode<T>(#clazz: [T].Type, source: AnyObject) -> [T] {
|
||||||
<T>(#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
|
static func decode<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
|
||||||
<T
|
|
||||||
, Key: Hashable>(#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 {
|
||||||
@ -61,8 +54,7 @@ class Response
|
|||||||
return dictionary
|
return dictionary
|
||||||
}
|
}
|
||||||
|
|
||||||
static func decode
|
static func decode<T>(#clazz: T.Type, source: AnyObject) -> T {
|
||||||
<T>(#clazz: T.Type, source: AnyObject) -> T {
|
|
||||||
initialize()
|
initialize()
|
||||||
if source is T {
|
if source is T {
|
||||||
return source as! T
|
return source as! T
|
||||||
@ -76,8 +68,7 @@ class Response
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func decodeOptional
|
static func decodeOptional<T>(#clazz: T.Type, source: AnyObject?) -> T? {
|
||||||
<T>(#clazz: T.Type, source: AnyObject?) -> T? {
|
|
||||||
if source is NSNull {
|
if source is NSNull {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -86,8 +77,7 @@ class Response
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func decodeOptional
|
static func decodeOptional<T>(#clazz: [T].Type, source: AnyObject?) -> [T]? {
|
||||||
<T>(#clazz: [T].Type, source: AnyObject?) -> [T]? {
|
|
||||||
if source is NSNull {
|
if source is NSNull {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -96,9 +86,7 @@ class Response
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func decodeOptional
|
static func decodeOptional<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
|
||||||
<T
|
|
||||||
, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
|
|
||||||
if source is NSNull {
|
if source is NSNull {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -126,10 +114,8 @@ class Response
|
|||||||
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}}
|
var instance = {{classname}}(){{#vars}}{{#isEnum}}
|
||||||
instance.{{name}} = (sourceDictionary["{{name}}"] as? String).map { {{classname}}
|
instance.{{name}} = (sourceDictionary["{{name}}"] as? String).map { {{classname}}.{{datatypeWithEnum}}(rawValue: $0)! }{{#required}}!{{/required}} {{/isEnum}}{{^isEnum}}
|
||||||
.{{datatypeWithEnum}}(rawValue: $0)! }{{#required}}!{{/required}} {{/isEnum}}{{^isEnum}}
|
instance.{{name}} = Decoders.decode{{^required}}Optional{{/required}}(clazz: {{{baseType}}}.self, source: sourceDictionary["{{name}}"]{{#required}}!{{/required}}){{/isEnum}}{{/vars}}
|
||||||
instance.{{name}} = Decoders.decode{{^required}}Optional{{/required}}(clazz: {{{baseType}}}
|
|
||||||
.self, source: sourceDictionary["{{name}}"]{{#required}}!{{/required}}){{/isEnum}}{{/vars}}
|
|
||||||
return instance
|
return instance
|
||||||
}{{/model}}
|
}{{/model}}
|
||||||
{{/models}}
|
{{/models}}
|
||||||
|
@ -28,33 +28,22 @@ class {{classname}}: APIBase {
|
|||||||
{{/hasParams}}{{#allParams}}
|
{{/hasParams}}{{#allParams}}
|
||||||
:param: {{paramName}} ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
|
:param: {{paramName}} ({{#isFormParam}}form{{/isFormParam}}{{#isQueryParam}}query{{/isQueryParam}}{{#isPathParam}}path{{/isPathParam}}{{#isHeaderParam}}header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}}) {{description}}{{/allParams}}
|
||||||
|
|
||||||
:returns: Promise
|
:returns: Promise<Response<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>> {{description}}
|
||||||
<Response
|
|
||||||
<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>> {{description}}
|
|
||||||
*/
|
*/
|
||||||
func {{operationId}}({{#allParams}}{{^secondaryParam}}#{{/secondaryParam}}{{paramName}}
|
func {{operationId}}({{#allParams}}{{^secondaryParam}}#{{/secondaryParam}}{{paramName}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
|
||||||
: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder
|
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{path}}"{{#pathParams}}
|
||||||
<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
|
path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}}
|
||||||
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path =
|
|
||||||
"{{path}}"{{#pathParams}}
|
|
||||||
path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString:
|
|
||||||
"\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}}
|
|
||||||
let url = {{projectName}}API.basePath + path
|
let url = {{projectName}}API.basePath + path
|
||||||
{{#bodyParam}}
|
{{#bodyParam}}
|
||||||
let parameters = {{paramName}}{{^required}}?{{/required}}.encode() as?
|
let parameters = {{paramName}}{{^required}}?{{/required}}.encode() as? [String:AnyObject]{{/bodyParam}}{{^bodyParam}}
|
||||||
[String:AnyObject]{{/bodyParam}}{{^bodyParam}}
|
let nillableParameters: [String:AnyObject?] = {{^queryParams}}[:]{{/queryParams}}{{#queryParams}}{{^secondaryParam}}[{{/secondaryParam}}
|
||||||
let nillableParameters: [String:AnyObject?] = {{^queryParams}}
|
|
||||||
[:]{{/queryParams}}{{#queryParams}}{{^secondaryParam}}[{{/secondaryParam}}
|
|
||||||
"{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}}
|
"{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}}
|
||||||
]{{/hasMore}}{{/queryParams}}
|
]{{/hasMore}}{{/queryParams}}
|
||||||
let parameters = APIHelper.rejectNil(nillableParameters){{/bodyParam}}
|
let parameters = APIHelper.rejectNil(nillableParameters){{/bodyParam}}
|
||||||
|
|
||||||
let requestBuilder: RequestBuilder
|
let requestBuilder: RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.Type = {{projectName}}API.requestBuilderFactory.getBuilder()
|
||||||
<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}>.Type = {{projectName}}
|
|
||||||
API.requestBuilderFactory.getBuilder()
|
|
||||||
|
|
||||||
return requestBuilder(method: "{{httpMethod}}", URLString: url, parameters: parameters, isBody: {{^queryParams}}
|
return requestBuilder(method: "{{httpMethod}}", URLString: url, parameters: parameters, isBody: {{^queryParams}}true{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}})
|
||||||
true{{/queryParams}}{{#queryParams}}{{^secondaryParam}}false{{/secondaryParam}}{{/queryParams}})
|
|
||||||
}
|
}
|
||||||
{{/operation}}
|
{{/operation}}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user