forked from loafle/openapi-generator-original
rollback swift template
This commit is contained in:
parent
d8060f47a8
commit
410144ea1c
@ -5,17 +5,17 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
class APIHelper {
|
class APIHelper {
|
||||||
static func rejectNil(source: [String:AnyObject?]) -> [String:AnyObject]? {
|
static func rejectNil(source: [String:AnyObject?]) -> [String:AnyObject]? {
|
||||||
var destination = [String:AnyObject]()
|
var destination = [String:AnyObject]()
|
||||||
for (key, nillableValue) in source {
|
for (key, nillableValue) in source {
|
||||||
if let value: AnyObject = nillableValue {
|
if let value: AnyObject = nillableValue {
|
||||||
destination[key] = value
|
destination[key] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if destination.isEmpty {
|
if destination.isEmpty {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return destination
|
return destination
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,64 +8,59 @@ import Foundation
|
|||||||
import PromiseKit
|
import PromiseKit
|
||||||
|
|
||||||
class {{projectName}}API {
|
class {{projectName}}API {
|
||||||
static let basePath = "{{^basePathOverride}}{{basePath}}{{/basePathOverride}}{{basePathOverride}}"
|
static let basePath = "{{^basePathOverride}}{{basePath}}{{/basePathOverride}}{{basePathOverride}}"
|
||||||
static var credential: NSURLCredential?
|
static var credential: NSURLCredential?
|
||||||
static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
|
static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
|
||||||
}
|
}
|
||||||
|
|
||||||
class APIBase {
|
class APIBase {
|
||||||
func toParameters(encodable: JSONEncodable?) -> [String: AnyObject]? {
|
func toParameters(encodable: JSONEncodable?) -> [String: AnyObject]? {
|
||||||
let encoded: AnyObject? = encodable?.encode()
|
let encoded: AnyObject? = encodable?.encode()
|
||||||
|
|
||||||
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 enumerate(encoded as! [AnyObject]) {
|
||||||
dictionary["\(index)"] = item
|
dictionary["\(index)"] = item
|
||||||
}
|
}
|
||||||
return dictionary
|
return dictionary
|
||||||
} else {
|
} else {
|
||||||
return encoded as? [String:AnyObject]
|
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]?
|
||||||
let isBody: Bool
|
let isBody: Bool
|
||||||
let method: String
|
let method: String
|
||||||
let URLString: String
|
let URLString: String
|
||||||
|
|
||||||
required init(method: String, URLString: String, parameters: [String:AnyObject]?, isBody: Bool) {
|
required init(method: String, URLString: String, parameters: [String:AnyObject]?, isBody: Bool) {
|
||||||
self.method = method
|
self.method = method
|
||||||
self.URLString = URLString
|
self.URLString = URLString
|
||||||
self.parameters = parameters
|
self.parameters = parameters
|
||||||
self.isBody = isBody
|
self.isBody = isBody
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func execute() -> Promise<Response<T>> { fatalError("Not implemented") }
|
||||||
|
|
||||||
func execute() -> Promise
|
func addHeader(#name: String, value: String) -> Self {
|
||||||
<Response
|
|
||||||
<T>> { fatalError("Not implemented") }
|
|
||||||
|
|
||||||
func addHeader(#name: String, value: String) -> Self {
|
|
||||||
if !value.isEmpty {
|
if !value.isEmpty {
|
||||||
headers[name] = value
|
headers[name] = value
|
||||||
}
|
}
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func addCredential() -> Self {
|
func addCredential() -> Self {
|
||||||
self.credential = {{projectName}}API.credential
|
self.credential = {{projectName}}API.credential
|
||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol RequestBuilderFactory {
|
protocol RequestBuilderFactory {
|
||||||
func getBuilder
|
func getBuilder<T>() -> RequestBuilder<T>.Type
|
||||||
<T>() -> RequestBuilder
|
}
|
||||||
<T>.Type
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,83 +8,72 @@ 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
|
||||||
|
private var managerStore: [String: Alamofire.Manager] = [:]
|
||||||
|
|
||||||
|
class AlamofireRequestBuilder<T>: RequestBuilder<T> {
|
||||||
|
required init(method: String, URLString: String, parameters: [String : AnyObject]?, isBody: Bool) {
|
||||||
|
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
|
||||||
|
}
|
||||||
|
|
||||||
|
override func execute() -> Promise<Response<T>> {
|
||||||
|
let managerId = NSUUID().UUIDString
|
||||||
|
// Create a new manager for each request to customize its request header
|
||||||
|
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
|
||||||
|
configuration.HTTPAdditionalHeaders = buildHeaders()
|
||||||
|
let manager = Alamofire.Manager(configuration: configuration)
|
||||||
|
managerStore[managerId] = manager
|
||||||
|
|
||||||
|
let encoding = isBody ? Alamofire.ParameterEncoding.JSON : Alamofire.ParameterEncoding.URL
|
||||||
|
let request = manager.request(Alamofire.Method(rawValue: method)!, URLString, parameters: parameters, encoding: encoding)
|
||||||
|
if let credential = self.credential {
|
||||||
|
request.authenticate(usingCredential: credential)
|
||||||
|
}
|
||||||
|
|
||||||
|
let defer = Promise<Response<T>>.defer()
|
||||||
|
request.responseJSON(options: .AllowFragments) { (req, res, json, error) in
|
||||||
|
managerStore.removeValueForKey(managerId)
|
||||||
|
|
||||||
|
if let error = error {
|
||||||
|
defer.reject(error)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
if res!.statusCode >= 400 {
|
||||||
|
//TODO: Add error entity
|
||||||
|
let error = NSError(domain: res!.URL!.URLString, code: res!.statusCode, userInfo: [:])
|
||||||
|
defer.reject(error)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store manager to retain its reference
|
if () is T {
|
||||||
private var managerStore: [String: Alamofire.Manager] = [:]
|
let response = Response(response: res!, body: () as! T)
|
||||||
|
defer.fulfill(response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if let json: AnyObject = json {
|
||||||
|
let body = Decoders.decode(clazz: T.self, source: json)
|
||||||
|
let response = Response(response: res!, body: body)
|
||||||
|
defer.fulfill(response)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable code"]))
|
||||||
|
}
|
||||||
|
return defer.promise
|
||||||
|
}
|
||||||
|
|
||||||
class AlamofireRequestBuilder
|
private func buildHeaders() -> [String: AnyObject] {
|
||||||
<T>: RequestBuilder
|
var httpHeaders = Manager.defaultHTTPHeaders
|
||||||
<T> {
|
for (key, value) in self.headers {
|
||||||
required init(method: String, URLString: String, parameters: [String : AnyObject]?, isBody: Bool) {
|
httpHeaders[key] = value
|
||||||
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
|
}
|
||||||
}
|
return httpHeaders
|
||||||
|
}
|
||||||
override func execute() -> Promise
|
}
|
||||||
<Response
|
|
||||||
<T>> {
|
|
||||||
let managerId = NSUUID().UUIDString
|
|
||||||
// Create a new manager for each request to customize its request header
|
|
||||||
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
|
|
||||||
configuration.HTTPAdditionalHeaders = buildHeaders()
|
|
||||||
let manager = Alamofire.Manager(configuration: configuration)
|
|
||||||
managerStore[managerId] = manager
|
|
||||||
|
|
||||||
let encoding = isBody ? Alamofire.ParameterEncoding.JSON : Alamofire.ParameterEncoding.URL
|
|
||||||
let request = manager.request(Alamofire.Method(rawValue: method)!, URLString, parameters:
|
|
||||||
parameters, encoding: encoding)
|
|
||||||
if let credential = self.credential {
|
|
||||||
request.authenticate(usingCredential: credential)
|
|
||||||
}
|
|
||||||
|
|
||||||
let defer = Promise
|
|
||||||
<Response
|
|
||||||
<T>>.defer()
|
|
||||||
request.responseJSON(options: .AllowFragments) { (req, res, json, error) in
|
|
||||||
managerStore.removeValueForKey(managerId)
|
|
||||||
|
|
||||||
if let error = error {
|
|
||||||
defer.reject(error)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if res!.statusCode >= 400 {
|
|
||||||
//TODO: Add error entity
|
|
||||||
let error = NSError(domain: res!.URL!.URLString, code: res!.statusCode, userInfo: [:])
|
|
||||||
defer.reject(error)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if () is T {
|
|
||||||
let response = Response(response: res!, body: () as! T)
|
|
||||||
defer.fulfill(response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if let json: AnyObject = json {
|
|
||||||
let body = Decoders.decode(clazz: T.self, source: json)
|
|
||||||
let response = Response(response: res!, body: body)
|
|
||||||
defer.fulfill(response)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer.reject(NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable
|
|
||||||
code"]))
|
|
||||||
}
|
|
||||||
return defer.promise
|
|
||||||
}
|
|
||||||
|
|
||||||
private func buildHeaders() -> [String: AnyObject] {
|
|
||||||
var httpHeaders = Manager.defaultHTTPHeaders
|
|
||||||
for (key, value) in self.headers {
|
|
||||||
httpHeaders[key] = value
|
|
||||||
}
|
|
||||||
return httpHeaders
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,46 +8,45 @@ import Alamofire
|
|||||||
import PromiseKit
|
import PromiseKit
|
||||||
|
|
||||||
extension Bool: JSONEncodable {
|
extension Bool: JSONEncodable {
|
||||||
func encode() -> AnyObject { return self }
|
func encode() -> AnyObject { return self }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Float: JSONEncodable {
|
extension Float: JSONEncodable {
|
||||||
func encode() -> AnyObject { return self }
|
func encode() -> AnyObject { return self }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Int: JSONEncodable {
|
extension Int: JSONEncodable {
|
||||||
func encode() -> AnyObject { return self }
|
func encode() -> AnyObject { return self }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Double: JSONEncodable {
|
extension Double: JSONEncodable {
|
||||||
func encode() -> AnyObject { return self }
|
func encode() -> AnyObject { return self }
|
||||||
}
|
}
|
||||||
|
|
||||||
extension String: JSONEncodable {
|
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 {
|
||||||
return object as! AnyObject
|
return object as! AnyObject
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extension Array: JSONEncodable {
|
extension Array: JSONEncodable {
|
||||||
func encode() -> AnyObject {
|
func encode() -> AnyObject {
|
||||||
return self.map(encodeIfPossible)
|
return self.map(encodeIfPossible)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extension Dictionary: JSONEncodable {
|
extension Dictionary: JSONEncodable {
|
||||||
func encode() -> AnyObject {
|
func encode() -> AnyObject {
|
||||||
var dictionary = [NSObject:AnyObject]()
|
var dictionary = [NSObject:AnyObject]()
|
||||||
for (key, value) in self {
|
for (key, value) in self {
|
||||||
dictionary[key as! NSObject] = encodeIfPossible(value)
|
dictionary[key as! NSObject] = encodeIfPossible(value)
|
||||||
}
|
}
|
||||||
return dictionary
|
return dictionary
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -7,132 +7,118 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
protocol JSONEncodable {
|
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
|
||||||
|
|
||||||
init(statusCode: Int, header: [String: String], body: T) {
|
init(statusCode: Int, header: [String: String], body: T) {
|
||||||
self.statusCode = statusCode
|
self.statusCode = statusCode
|
||||||
self.header = header
|
self.header = header
|
||||||
self.body = body
|
self.body = body
|
||||||
}
|
}
|
||||||
|
|
||||||
convenience init(response: NSHTTPURLResponse, body: T) {
|
convenience init(response: NSHTTPURLResponse, body: T) {
|
||||||
let rawHeader = response.allHeaderFields
|
let rawHeader = response.allHeaderFields
|
||||||
var header = [String:String]()
|
var header = [String:String]()
|
||||||
for (key, value) in rawHeader {
|
for (key, value) in rawHeader {
|
||||||
header[key as! String] = value as? String
|
header[key as! String] = value as? String
|
||||||
}
|
}
|
||||||
self.init(statusCode: response.statusCode, header: header, body: body)
|
self.init(statusCode: response.statusCode, header: header, body: body)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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<T>(#clazz: T.Type, decoder: ((AnyObject) -> T)) {
|
||||||
|
|
||||||
static func addDecoder
|
|
||||||
<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<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
|
||||||
static func decode
|
let sourceDictinoary = source as! [Key: AnyObject]
|
||||||
<T
|
var dictionary = [Key:T]()
|
||||||
, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
|
for (key, value) in sourceDictinoary {
|
||||||
let sourceDictinoary = source as! [Key: AnyObject]
|
|
||||||
var dictionary = [Key:T]()
|
|
||||||
for (key, value) in sourceDictinoary {
|
|
||||||
dictionary[key] = Decoders.decode(clazz: T.self, source: value)
|
dictionary[key] = Decoders.decode(clazz: T.self, source: value)
|
||||||
}
|
}
|
||||||
return dictionary
|
return dictionary
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func decode<T>(#clazz: T.Type, source: AnyObject) -> T {
|
||||||
|
initialize()
|
||||||
|
if source is T {
|
||||||
|
return source as! T
|
||||||
|
}
|
||||||
|
|
||||||
|
let key = "\(T.self)"
|
||||||
|
if let decoder = decoders[key] {
|
||||||
|
return decoder(source) as! T
|
||||||
|
} else {
|
||||||
|
fatalError("Source \(source) is not convertible to type \(clazz): Maybe swagger file is insufficient")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static func decode
|
static func decodeOptional<T>(#clazz: T.Type, source: AnyObject?) -> T? {
|
||||||
<T>(#clazz: T.Type, source: AnyObject) -> T {
|
if source is NSNull {
|
||||||
initialize()
|
return nil
|
||||||
if source is T {
|
}
|
||||||
return source as! T
|
return source.map { (source: AnyObject) -> T in
|
||||||
|
Decoders.decode(clazz: clazz, source: source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static func decodeOptional<T>(#clazz: [T].Type, source: AnyObject?) -> [T]? {
|
||||||
|
if source is NSNull {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return source.map { (someSource: AnyObject) -> [T] in
|
||||||
|
Decoders.decode(clazz: clazz, source: someSource)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static func decodeOptional<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
|
||||||
|
if source is NSNull {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return source.map { (someSource: AnyObject) -> [Key:T] in
|
||||||
|
Decoders.decode(clazz: clazz, source: someSource)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static private func initialize() {
|
||||||
|
dispatch_once(&once) {
|
||||||
|
let dateTimeFormatter = NSDateFormatter()
|
||||||
|
dateTimeFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
|
||||||
|
let dateFormatter = NSDateFormatter()
|
||||||
|
dateFormatter.dateFormat = "yyyy-MM-dd"
|
||||||
|
// Decoder for NSDate
|
||||||
|
Decoders.addDecoder(clazz: NSDate.self) { (source: AnyObject) -> NSDate in
|
||||||
|
let sourceString = source as! String
|
||||||
|
if count(sourceString) == 10 {
|
||||||
|
return dateFormatter.dateFromString(sourceString)!
|
||||||
}
|
}
|
||||||
|
return dateTimeFormatter.dateFromString(sourceString)!
|
||||||
|
} {{#models}}{{#model}}
|
||||||
|
|
||||||
let key = "\(T.self)"
|
// Decoder for {{{classname}}}
|
||||||
if let decoder = decoders[key] {
|
Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject) -> {{{classname}}} in
|
||||||
return decoder(source) as! T
|
let sourceDictionary = source as! [NSObject:AnyObject]
|
||||||
} else {
|
var instance = {{classname}}(){{#vars}}{{#isEnum}}
|
||||||
fatalError("Source \(source) is not convertible to type \(clazz): Maybe swagger file is insufficient")
|
instance.{{name}} = (sourceDictionary["{{name}}"] as? String).map { {{classname}}.{{datatypeWithEnum}}(rawValue: $0)! }{{#required}}!{{/required}} {{/isEnum}}{{^isEnum}}
|
||||||
}
|
instance.{{name}} = Decoders.decode{{^required}}Optional{{/required}}(clazz: {{{baseType}}}.self, source: sourceDictionary["{{name}}"]{{#required}}!{{/required}}){{/isEnum}}{{/vars}}
|
||||||
}
|
return instance
|
||||||
|
}{{/model}}
|
||||||
static func decodeOptional
|
{{/models}}
|
||||||
<T>(#clazz: T.Type, source: AnyObject?) -> T? {
|
}
|
||||||
if source is NSNull {
|
}
|
||||||
return nil
|
}
|
||||||
}
|
|
||||||
return source.map { (source: AnyObject) -> T in
|
|
||||||
Decoders.decode(clazz: clazz, source: source)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static func decodeOptional
|
|
||||||
<T>(#clazz: [T].Type, source: AnyObject?) -> [T]? {
|
|
||||||
if source is NSNull {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return source.map { (someSource: AnyObject) -> [T] in
|
|
||||||
Decoders.decode(clazz: clazz, source: someSource)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static func decodeOptional
|
|
||||||
<T
|
|
||||||
, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
|
|
||||||
if source is NSNull {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return source.map { (someSource: AnyObject) -> [Key:T] in
|
|
||||||
Decoders.decode(clazz: clazz, source: someSource)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static private func initialize() {
|
|
||||||
dispatch_once(&once) {
|
|
||||||
let dateTimeFormatter = NSDateFormatter()
|
|
||||||
dateTimeFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
|
|
||||||
let dateFormatter = NSDateFormatter()
|
|
||||||
dateFormatter.dateFormat = "yyyy-MM-dd"
|
|
||||||
// Decoder for NSDate
|
|
||||||
Decoders.addDecoder(clazz: NSDate.self) { (source: AnyObject) -> NSDate in
|
|
||||||
let sourceString = source as! String
|
|
||||||
if count(sourceString) == 10 {
|
|
||||||
return dateFormatter.dateFromString(sourceString)!
|
|
||||||
}
|
|
||||||
return dateTimeFormatter.dateFromString(sourceString)!
|
|
||||||
} {{#models}}{{#model}}
|
|
||||||
|
|
||||||
// Decoder for {{{classname}}}
|
|
||||||
Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject) -> {{{classname}}} in
|
|
||||||
let sourceDictionary = source as! [NSObject:AnyObject]
|
|
||||||
var instance = {{classname}}(){{#vars}}{{#isEnum}}
|
|
||||||
instance.{{name}} = (sourceDictionary["{{name}}"] as? String).map { {{classname}}
|
|
||||||
.{{datatypeWithEnum}}(rawValue: $0)! }{{#required}}!{{/required}} {{/isEnum}}{{^isEnum}}
|
|
||||||
instance.{{name}} = Decoders.decode{{^required}}Optional{{/required}}(clazz: {{{baseType}}}
|
|
||||||
.self, source: sourceDictionary["{{name}}"]{{#required}}!{{/required}}){{/isEnum}}{{/vars}}
|
|
||||||
return instance
|
|
||||||
}{{/model}}
|
|
||||||
{{/models}}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -9,54 +9,43 @@ import Alamofire
|
|||||||
import PromiseKit
|
import PromiseKit
|
||||||
|
|
||||||
extension {{projectName}}API {
|
extension {{projectName}}API {
|
||||||
{{#description}}
|
{{#description}}
|
||||||
/** {{description}} */{{/description}}
|
/** {{description}} */{{/description}}
|
||||||
class {{classname}}: APIBase {
|
class {{classname}}: APIBase {
|
||||||
{{#operation}}
|
{{#operation}}
|
||||||
/**
|
/**
|
||||||
{{#summary}}
|
{{#summary}}
|
||||||
{{{summary}}}
|
{{{summary}}}
|
||||||
{{/summary}}
|
{{/summary}}
|
||||||
- {{httpMethod}} {{path}}{{#notes}}
|
- {{httpMethod}} {{path}}{{#notes}}
|
||||||
- {{{notes}}}{{/notes}}{{#subresourceOperation}}
|
- {{{notes}}}{{/notes}}{{#subresourceOperation}}
|
||||||
- subresourceOperation: {{subresourceOperation}}{{/subresourceOperation}}{{#defaultResponse}}
|
- subresourceOperation: {{subresourceOperation}}{{/subresourceOperation}}{{#defaultResponse}}
|
||||||
- defaultResponse: {{defaultResponse}}{{/defaultResponse}}{{#authMethods}}
|
- defaultResponse: {{defaultResponse}}{{/defaultResponse}}{{#authMethods}}
|
||||||
- authMethods: {{authMethods}}{{/authMethods}}{{#responseHeaders}}
|
- authMethods: {{authMethods}}{{/authMethods}}{{#responseHeaders}}
|
||||||
- responseHeaders: {{responseHeaders}}{{/responseHeaders}}{{#examples}}
|
- responseHeaders: {{responseHeaders}}{{/responseHeaders}}{{#examples}}
|
||||||
- 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}}
|
: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}}: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
|
||||||
*/
|
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path = "{{path}}"{{#pathParams}}
|
||||||
func {{operationId}}({{#allParams}}{{^secondaryParam}}#{{/secondaryParam}}{{paramName}}
|
path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString: "\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}}
|
||||||
: {{{dataType}}}{{^required}}?{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) -> RequestBuilder
|
let url = {{projectName}}API.basePath + path
|
||||||
<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {
|
{{#bodyParam}}
|
||||||
{{^pathParams}}let{{/pathParams}}{{#pathParams}}{{^secondaryParam}}var{{/secondaryParam}}{{/pathParams}} path =
|
let parameters = {{paramName}}{{^required}}?{{/required}}.encode() as? [String:AnyObject]{{/bodyParam}}{{^bodyParam}}
|
||||||
"{{path}}"{{#pathParams}}
|
let nillableParameters: [String:AnyObject?] = {{^queryParams}}[:]{{/queryParams}}{{#queryParams}}{{^secondaryParam}}[{{/secondaryParam}}
|
||||||
path = path.stringByReplacingOccurrencesOfString("{{=<% %>=}}{<%paramName%>}<%={{ }}=%>", withString:
|
"{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}}
|
||||||
"\({{paramName}})", options: .LiteralSearch, range: nil){{/pathParams}}
|
]{{/hasMore}}{{/queryParams}}
|
||||||
let url = {{projectName}}API.basePath + path
|
|
||||||
{{#bodyParam}}
|
|
||||||
let parameters = {{paramName}}{{^required}}?{{/required}}.encode() as?
|
|
||||||
[String:AnyObject]{{/bodyParam}}{{^bodyParam}}
|
|
||||||
let nillableParameters: [String:AnyObject?] = {{^queryParams}}
|
|
||||||
[:]{{/queryParams}}{{#queryParams}}{{^secondaryParam}}[{{/secondaryParam}}
|
|
||||||
"{{paramName}}": {{paramName}}{{#hasMore}},{{/hasMore}}{{^hasMore}}
|
|
||||||
]{{/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}}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
{{/operations}}
|
{{/operations}}
|
||||||
|
@ -9,27 +9,27 @@ import Foundation
|
|||||||
|
|
||||||
{{#description}}
|
{{#description}}
|
||||||
|
|
||||||
/** {{description}} */{{/description}}
|
/** {{description}} */{{/description}}
|
||||||
class {{classname}}: JSONEncodable {
|
class {{classname}}: JSONEncodable {
|
||||||
{{#vars}}{{#isEnum}}
|
{{#vars}}{{#isEnum}}
|
||||||
enum {{datatypeWithEnum}}: String { {{#allowableValues}}{{#values}}
|
enum {{datatypeWithEnum}}: String { {{#allowableValues}}{{#values}}
|
||||||
case {{enum}} = "{{raw}}"{{/values}}{{/allowableValues}}
|
case {{enum}} = "{{raw}}"{{/values}}{{/allowableValues}}
|
||||||
}
|
}
|
||||||
{{/isEnum}}{{/vars}}
|
{{/isEnum}}{{/vars}}
|
||||||
{{#vars}}{{#isEnum}}{{#description}}/** {{description}} */
|
{{#vars}}{{#isEnum}}{{#description}}/** {{description}} */
|
||||||
{{/description}}var {{name}}: {{{datatypeWithEnum}}}{{^required}}?{{/required}}{{#required}}!{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/isEnum}}{{^isEnum}}{{#description}}/** {{description}} */
|
{{/description}}var {{name}}: {{{datatypeWithEnum}}}{{^required}}?{{/required}}{{#required}}!{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/isEnum}}{{^isEnum}}{{#description}}/** {{description}} */
|
||||||
{{/description}}var {{name}}: {{{datatype}}}{{^required}}?{{/required}}{{#required}}!{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/isEnum}}
|
{{/description}}var {{name}}: {{{datatype}}}{{^required}}?{{/required}}{{#required}}!{{/required}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{/isEnum}}
|
||||||
{{/vars}}
|
{{/vars}}
|
||||||
|
|
||||||
// MARK: JSONEncodable
|
// MARK: JSONEncodable
|
||||||
func encode() -> AnyObject {
|
func encode() -> AnyObject {
|
||||||
var nillableDictionary = [String:AnyObject?](){{#vars}}{{#isNotContainer}}{{#isPrimitiveType}}{{^isEnum}}
|
var nillableDictionary = [String:AnyObject?](){{#vars}}{{#isNotContainer}}{{#isPrimitiveType}}{{^isEnum}}
|
||||||
nillableDictionary["{{name}}"] = self.{{name}}{{/isEnum}}{{/isPrimitiveType}}{{#isEnum}}
|
nillableDictionary["{{name}}"] = self.{{name}}{{/isEnum}}{{/isPrimitiveType}}{{#isEnum}}
|
||||||
nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.rawValue{{/isEnum}}{{^isPrimitiveType}}
|
nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.rawValue{{/isEnum}}{{^isPrimitiveType}}
|
||||||
nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.encode(){{/isPrimitiveType}}{{/isNotContainer}}{{#isContainer}}
|
nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.encode(){{/isPrimitiveType}}{{/isNotContainer}}{{#isContainer}}
|
||||||
nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.encode(){{/isContainer}}{{/vars}}
|
nillableDictionary["{{name}}"] = self.{{name}}{{^required}}?{{/required}}.encode(){{/isContainer}}{{/vars}}
|
||||||
let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:]
|
let dictionary: [String:AnyObject] = APIHelper.rejectNil(nillableDictionary) ?? [:]
|
||||||
return dictionary
|
return dictionary
|
||||||
}
|
}
|
||||||
}{{/model}}
|
}{{/model}}
|
||||||
{{/models}}
|
{{/models}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user