-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels.swift
executable file
·257 lines (222 loc) · 10.8 KB
/
Models.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Models.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//
import Foundation
protocol JSONEncodable {
func encodeToJSON() -> Any
}
public enum ErrorResponse : Error {
case Error(Int, Data?, Error)
}
open class Response<T> {
open let statusCode: Int
open let header: [String: String]
open let body: T?
public init(statusCode: Int, header: [String: String], body: T?) {
self.statusCode = statusCode
self.header = header
self.body = body
}
public convenience init(response: HTTPURLResponse, body: T?) {
let rawHeader = response.allHeaderFields
var header = [String:String]()
for (key, value) in rawHeader {
header[key as! String] = value as? String
}
self.init(statusCode: response.statusCode, header: header, body: body)
}
}
private var once = Int()
class Decoders {
static fileprivate var decoders = Dictionary<String, ((AnyObject) -> AnyObject)>()
static func addDecoder<T>(clazz: T.Type, decoder: @escaping ((AnyObject) -> T)) {
let key = "\(T.self)"
decoders[key] = { decoder($0) as AnyObject }
}
static func decode<T>(clazz: T.Type, discriminator: String, source: AnyObject) -> T {
let key = discriminator;
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<T>(clazz: [T].Type, source: AnyObject) -> [T] {
let array = source as! [AnyObject]
return array.map { Decoders.decode(clazz: T.self, source: $0) }
}
static func decode<T, Key: Hashable>(clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
let sourceDictionary = source as! [Key: AnyObject]
var dictionary = [Key:T]()
for (key, value) in sourceDictionary {
dictionary[key] = Decoders.decode(clazz: T.self, source: value)
}
return dictionary
}
static func decode<T>(clazz: T.Type, source: AnyObject) -> T {
initialize()
if T.self is Int32.Type && source is NSNumber {
return source.int32Value as! T;
}
if T.self is Int64.Type && source is NSNumber {
return source.int64Value as! T;
}
if T.self is UUID.Type && source is String {
return UUID(uuidString: source as! String) as! T
}
if source is T {
return source as! T
}
if T.self is Data.Type && source is String {
return Data(base64Encoded: source as! String) 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 decodeOptional<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)
}
}
private static var __once: () = {
let formatters = [
"yyyy-MM-dd",
"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.SSS"
].map { (format: String) -> DateFormatter in
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter
}
// Decoder for Date
Decoders.addDecoder(clazz: Date.self) { (source: AnyObject) -> Date in
if let sourceString = source as? String {
for formatter in formatters {
if let date = formatter.date(from: sourceString) {
return date
}
}
}
if let sourceInt = source as? Int {
// treat as a java date
return Date(timeIntervalSince1970: Double(sourceInt / 1000) )
}
fatalError("formatter failed to parse \(source)")
}
// Decoder for [Activities]
Decoders.addDecoder(clazz: [Activities].self) { (source: AnyObject) -> [Activities] in
return Decoders.decode(clazz: [Activities].self, source: source)
}
// Decoder for Activities
Decoders.addDecoder(clazz: Activities.self) { (source: AnyObject) -> Activities in
let sourceDictionary = source as! [AnyHashable: Any]
let instance = Activities()
instance.offset = Decoders.decodeOptional(clazz: Int32.self, source: sourceDictionary["offset"] as AnyObject?)
instance.limit = Decoders.decodeOptional(clazz: Int32.self, source: sourceDictionary["limit"] as AnyObject?)
instance.count = Decoders.decodeOptional(clazz: Int32.self, source: sourceDictionary["count"] as AnyObject?)
instance.history = Decoders.decodeOptional(clazz: Array.self, source: sourceDictionary["history"] as AnyObject?)
return instance
}
// Decoder for [Activity]
Decoders.addDecoder(clazz: [Activity].self) { (source: AnyObject) -> [Activity] in
return Decoders.decode(clazz: [Activity].self, source: source)
}
// Decoder for Activity
Decoders.addDecoder(clazz: Activity.self) { (source: AnyObject) -> Activity in
let sourceDictionary = source as! [AnyHashable: Any]
let instance = Activity()
instance.uuid = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["uuid"] as AnyObject?)
return instance
}
// Decoder for [ModelError]
Decoders.addDecoder(clazz: [ModelError].self) { (source: AnyObject) -> [ModelError] in
return Decoders.decode(clazz: [ModelError].self, source: source)
}
// Decoder for ModelError
Decoders.addDecoder(clazz: ModelError.self) { (source: AnyObject) -> ModelError in
let sourceDictionary = source as! [AnyHashable: Any]
let instance = ModelError()
instance.code = Decoders.decodeOptional(clazz: Int32.self, source: sourceDictionary["code"] as AnyObject?)
instance.message = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["message"] as AnyObject?)
instance.fields = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["fields"] as AnyObject?)
return instance
}
// Decoder for [PriceEstimate]
Decoders.addDecoder(clazz: [PriceEstimate].self) { (source: AnyObject) -> [PriceEstimate] in
return Decoders.decode(clazz: [PriceEstimate].self, source: source)
}
// Decoder for PriceEstimate
Decoders.addDecoder(clazz: PriceEstimate.self) { (source: AnyObject) -> PriceEstimate in
let sourceDictionary = source as! [AnyHashable: Any]
let instance = PriceEstimate()
instance.productId = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["product_id"] as AnyObject?)
instance.currencyCode = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["currency_code"] as AnyObject?)
instance.displayName = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["display_name"] as AnyObject?)
instance.estimate = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["estimate"] as AnyObject?)
instance.lowEstimate = Decoders.decodeOptional(clazz: Double.self, source: sourceDictionary["low_estimate"] as AnyObject?)
instance.highEstimate = Decoders.decodeOptional(clazz: Double.self, source: sourceDictionary["high_estimate"] as AnyObject?)
instance.surgeMultiplier = Decoders.decodeOptional(clazz: Double.self, source: sourceDictionary["surge_multiplier"] as AnyObject?)
return instance
}
// Decoder for [Product]
Decoders.addDecoder(clazz: [Product].self) { (source: AnyObject) -> [Product] in
return Decoders.decode(clazz: [Product].self, source: source)
}
// Decoder for Product
Decoders.addDecoder(clazz: Product.self) { (source: AnyObject) -> Product in
let sourceDictionary = source as! [AnyHashable: Any]
let instance = Product()
instance.productId = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["product_id"] as AnyObject?)
instance.description = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["description"] as AnyObject?)
instance.displayName = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["display_name"] as AnyObject?)
instance.capacity = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["capacity"] as AnyObject?)
instance.image = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["image"] as AnyObject?)
return instance
}
// Decoder for [Profile]
Decoders.addDecoder(clazz: [Profile].self) { (source: AnyObject) -> [Profile] in
return Decoders.decode(clazz: [Profile].self, source: source)
}
// Decoder for Profile
Decoders.addDecoder(clazz: Profile.self) { (source: AnyObject) -> Profile in
let sourceDictionary = source as! [AnyHashable: Any]
let instance = Profile()
instance.firstName = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["first_name"] as AnyObject?)
instance.lastName = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["last_name"] as AnyObject?)
instance.email = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["email"] as AnyObject?)
instance.picture = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["picture"] as AnyObject?)
instance.promoCode = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["promo_code"] as AnyObject?)
return instance
}
}()
static fileprivate func initialize() {
_ = Decoders.__once
}
}