Skip to content

Commit acd6b51

Browse files
committed
Issue #179: added swift 3 and 4 mustaches templates.
1 parent 5edd8a7 commit acd6b51

34 files changed

+2987
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// APIHelper.swift
2+
//
3+
// Generated by swagger-codegen
4+
// https://github.com/swagger-api/swagger-codegen
5+
//
6+
7+
import Foundation
8+
9+
class APIHelper {
10+
static func rejectNil(_ source: [String:Any?]) -> [String:Any]? {
11+
var destination = [String:Any]()
12+
for (key, nillableValue) in source {
13+
if let value: Any = nillableValue {
14+
destination[key] = value
15+
}
16+
}
17+
18+
if destination.isEmpty {
19+
return nil
20+
}
21+
return destination
22+
}
23+
24+
static func rejectNilHeaders(_ source: [String:Any?]) -> [String:String] {
25+
var destination = [String:String]()
26+
for (key, nillableValue) in source {
27+
if let value: Any = nillableValue {
28+
destination[key] = "\(value)"
29+
}
30+
}
31+
return destination
32+
}
33+
34+
static func convertBoolToString(_ source: [String: Any]?) -> [String:Any]? {
35+
guard let source = source else {
36+
return nil
37+
}
38+
var destination = [String:Any]()
39+
let theTrue = NSNumber(value: true as Bool)
40+
let theFalse = NSNumber(value: false as Bool)
41+
for (key, value) in source {
42+
switch value {
43+
case let x where x as? NSNumber === theTrue || x as? NSNumber === theFalse:
44+
destination[key] = "\(value as! Bool)" as Any?
45+
default:
46+
destination[key] = value
47+
}
48+
}
49+
return destination
50+
}
51+
52+
static func mapValuesToQueryItems(values: [String:Any?]) -> [URLQueryItem]? {
53+
let returnValues = values
54+
.filter { $0.1 != nil }
55+
.map { (item: (_key: String, _value: Any?)) -> [URLQueryItem] in
56+
if let value = item._value as? Array<String> {
57+
return value.map { (v) -> URLQueryItem in
58+
URLQueryItem(
59+
name: item._key,
60+
value: v
61+
)
62+
}
63+
} else {
64+
return [URLQueryItem(
65+
name: item._key,
66+
value: "\(item._value!)"
67+
)]
68+
}
69+
}
70+
.flatMap { $0 }
71+
72+
if returnValues.isEmpty { return nil }
73+
return returnValues
74+
}
75+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// APIs.swift
2+
//
3+
// Generated by swagger-codegen
4+
// https://github.com/swagger-api/swagger-codegen
5+
//
6+
7+
import Foundation
8+
9+
open class {{projectName}}API {
10+
open static var basePath = "{{{basePath}}}"
11+
open static var credential: URLCredential?
12+
open static var customHeaders: [String:String] = [:]
13+
open static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
14+
}
15+
16+
open class APIBase {
17+
func toParameters(_ encodable: JSONEncodable?) -> [String: Any]? {
18+
let encoded: Any? = encodable?.encodeToJSON()
19+
20+
if encoded! is [Any] {
21+
var dictionary = [String:Any]()
22+
for (index, item) in (encoded as! [Any]).enumerated() {
23+
dictionary["\(index)"] = item
24+
}
25+
return dictionary
26+
} else {
27+
return encoded as? [String:Any]
28+
}
29+
}
30+
}
31+
32+
open class RequestBuilder<T> {
33+
var credential: URLCredential?
34+
var headers: [String:String]
35+
public let parameters: Any?
36+
public let isBody: Bool
37+
public let method: String
38+
public let URLString: String
39+
40+
/// Optional block to obtain a reference to the request's progress instance when available.
41+
public var onProgressReady: ((Progress) -> ())?
42+
43+
required public init(method: String, URLString: String, parameters: Any?, isBody: Bool, headers: [String:String] = [:]) {
44+
self.method = method
45+
self.URLString = URLString
46+
self.parameters = parameters
47+
self.isBody = isBody
48+
self.headers = headers
49+
50+
addHeaders({{projectName}}API.customHeaders)
51+
}
52+
53+
open func addHeaders(_ aHeaders:[String:String]) {
54+
for (header, value) in aHeaders {
55+
addHeader(name: header, value: value)
56+
}
57+
}
58+
59+
open func execute(_ completion: @escaping (_ response: Response<T>?, _ error: ErrorResponse?) -> Void) { }
60+
61+
@discardableResult public func addHeader(name: String, value: String) -> Self {
62+
if !value.isEmpty {
63+
headers[name] = value
64+
}
65+
return self
66+
}
67+
68+
open func addCredential() -> Self {
69+
self.credential = {{projectName}}API.credential
70+
return self
71+
}
72+
}
73+
74+
public protocol RequestBuilderFactory {
75+
func getBuilder<T>() -> RequestBuilder<T>.Type
76+
}
77+

0 commit comments

Comments
 (0)