@@ -9,10 +9,20 @@ public final class Client {
9
9
10
10
internal( set) public var session : URLSession
11
11
12
+ private let encoder : JSONEncoder
13
+ private let decoder : JSONDecoder
14
+
12
15
public init ( session: URLSession = URLSession ( configuration: . default) , host: URL ? = nil , apiKey: String ) {
13
16
self . session = session
14
17
self . host = host ?? Self . defaultHost
15
18
self . apiKey = apiKey
19
+ self . encoder = JSONEncoder ( )
20
+ self . decoder = JSONDecoder ( )
21
+ self . decoder. dateDecodingStrategy = . custom { decoder in
22
+ let container = try decoder. singleValueContainer ( )
23
+ let dateInt = try container. decode ( Int . self)
24
+ return Date ( timeIntervalSince1970: TimeInterval ( dateInt) )
25
+ }
16
26
}
17
27
18
28
public enum Error : Swift . Error , CustomStringConvertible {
@@ -185,7 +195,7 @@ extension Client {
185
195
req. httpBody = try encodeMultipartFormData ( body, boundary: boundary)
186
196
} else {
187
197
req. setValue ( " application/json; charset=utf-8 " , forHTTPHeaderField: " Content-Type " )
188
- req. httpBody = try JSONEncoder ( ) . encode ( body)
198
+ req. httpBody = try encoder . encode ( body)
189
199
}
190
200
}
191
201
return req
@@ -218,14 +228,4 @@ extension Client {
218
228
data. append ( " -- \( boundary) -- \r \n " . data ( using: . utf8) !)
219
229
return data
220
230
}
221
-
222
- private var decoder : JSONDecoder {
223
- let decoder = JSONDecoder ( )
224
- decoder. dateDecodingStrategy = . custom { decoder in
225
- let container = try decoder. singleValueContainer ( )
226
- let dateInt = try container. decode ( Int . self)
227
- return Date ( timeIntervalSince1970: TimeInterval ( dateInt) )
228
- }
229
- return decoder
230
- }
231
231
}
0 commit comments