Skip to content

Commit 6c93a33

Browse files
Philipp HomannPhilipp Homann
Philipp Homann
authored and
Philipp Homann
committed
added possibility to change the JSONDecoder date decoding strategy
1 parent 1c9e074 commit 6c93a33

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

Sources/UsefulNetworkLayer/NetworkLayer/APIConfiguration.swift

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public struct APIConfiguration<T,S> where T: ResponseBodyParsable, S: ErrorRespo
5757

5858
/// The timeout value for the request wait time.
5959
public var timeOut: Int
60+
61+
/// The date decoding strategy. Default is .defferedToDate
62+
public var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy
63+
6064

6165
/**
6266
Initializes Configuration with the host URL and endpoint separately.
@@ -72,6 +76,7 @@ public struct APIConfiguration<T,S> where T: ResponseBodyParsable, S: ErrorRespo
7276
- parameter body: Request body for the API request.
7377
- parameter responseBodyObject: Type of the Response Object to create.
7478
- parameter autoCache: To use that, override `cachingEndsAt:` method of Response Body Object.
79+
- parameter dateDecodingStrategy: The JSON date decoding strategy. Default is `.defferedToDate`
7580
Then specified custom caching will be applied for that request.
7681
*/
7782
public init?(hostURL: String, endPoint: String,
@@ -84,7 +89,8 @@ public struct APIConfiguration<T,S> where T: ResponseBodyParsable, S: ErrorRespo
8489
cachingTime: NetworkLayer.CachingTime = NetworkLayer.CachingTime(seconds: 60 * 60),
8590
isMainOperation: Bool = false,
8691
autoCache: Bool = false,
87-
timeOut: Int = 30) {
92+
timeOut: Int = 30,
93+
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate) {
8894

8995
var url = URL(string: hostURL)
9096
url?.appendPathComponent(endPoint)
@@ -100,7 +106,8 @@ public struct APIConfiguration<T,S> where T: ResponseBodyParsable, S: ErrorRespo
100106
cachingTime: cachingTime,
101107
isMainOperation: isMainOperation,
102108
autoCache: autoCache,
103-
timeOut: timeOut)
109+
timeOut: timeOut,
110+
dateDecodingStrategy: dateDecodingStrategy)
104111
}
105112

106113
/**
@@ -115,6 +122,7 @@ public struct APIConfiguration<T,S> where T: ResponseBodyParsable, S: ErrorRespo
115122
- parameter body: Request body for the API request.
116123
- parameter responseBodyObject: Type of the Response Object to create.
117124
- parameter autoCache: To use that, override `cachingEndsAt:` method of Response Body Object.
125+
- parameter dateDecodingStrategy: The JSON date decoding strategy. Default is `.defferedToDate`
118126
Then specified custom caching will be applied for that request.
119127
*/
120128
public init(url: URL,
@@ -127,7 +135,8 @@ public struct APIConfiguration<T,S> where T: ResponseBodyParsable, S: ErrorRespo
127135
cachingTime: NetworkLayer.CachingTime = NetworkLayer.CachingTime(seconds: 60 * 60),
128136
isMainOperation: Bool = false,
129137
autoCache: Bool = false,
130-
timeOut: Int = 30) {
138+
timeOut: Int = 30,
139+
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate) {
131140

132141
self.requestURL = url
133142
self.requestType = requestType
@@ -140,6 +149,7 @@ public struct APIConfiguration<T,S> where T: ResponseBodyParsable, S: ErrorRespo
140149
self.autoCache = autoCache
141150
self.timeOut = timeOut
142151
self.errorResponseBodyObject = errorType
152+
self.dateDecodingStrategy = dateDecodingStrategy
143153
}
144154

145155
/// Tries to create URL request by specified parameters.
@@ -204,6 +214,7 @@ public extension APIConfiguration where T: ResponseBodyParsable, S == DefaultAPI
204214
- parameter body: Request body for the API request.
205215
- parameter responseBodyObject: Type of the Response Object to create.
206216
- parameter autoCache: To use that, override `cachingEndsAt:` method of Response Body Object.
217+
- parameter dateDecodingStrategy: The JSON date decoding strategy. Default is `.defferedToDate`
207218
Then specified custom caching will be applied for that request.
208219
*/
209220
init?(hostURL: String, endPoint: String,
@@ -215,7 +226,8 @@ public extension APIConfiguration where T: ResponseBodyParsable, S == DefaultAPI
215226
cachingTime: NetworkLayer.CachingTime = NetworkLayer.CachingTime(seconds: 60 * 60),
216227
isMainOperation: Bool = false,
217228
autoCache: Bool = false,
218-
timeOut: Int = 30) {
229+
timeOut: Int = 30,
230+
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate) {
219231

220232
self.init(hostURL: hostURL,
221233
endPoint: endPoint,
@@ -228,7 +240,8 @@ public extension APIConfiguration where T: ResponseBodyParsable, S == DefaultAPI
228240
cachingTime: cachingTime,
229241
isMainOperation: isMainOperation,
230242
autoCache: autoCache,
231-
timeOut: timeOut)
243+
timeOut: timeOut,
244+
dateDecodingStrategy: dateDecodingStrategy)
232245
}
233246

234247
/**
@@ -243,6 +256,7 @@ public extension APIConfiguration where T: ResponseBodyParsable, S == DefaultAPI
243256
- parameter body: Request body for the API request.
244257
- parameter responseBodyObject: Type of the Response Object to create.
245258
- parameter autoCache: To use that, override `cachingEndsAt:` method of Response Body Object.
259+
- parameter dateDecodingStrategy: The JSON date decoding strategy. Default is `.defferedToDate`
246260
Then specified custom caching will be applied for that request.
247261
*/
248262
init(url: URL,
@@ -254,7 +268,8 @@ public extension APIConfiguration where T: ResponseBodyParsable, S == DefaultAPI
254268
cachingTime: NetworkLayer.CachingTime = NetworkLayer.CachingTime(seconds: 60 * 60),
255269
isMainOperation: Bool = false,
256270
autoCache: Bool = false,
257-
timeOut: Int = 30) {
271+
timeOut: Int = 30,
272+
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate) {
258273

259274
self.init(url: url,
260275
requestType: requestType,
@@ -266,6 +281,7 @@ public extension APIConfiguration where T: ResponseBodyParsable, S == DefaultAPI
266281
cachingTime: cachingTime,
267282
isMainOperation: isMainOperation,
268283
autoCache: autoCache,
269-
timeOut: timeOut)
284+
timeOut: timeOut,
285+
dateDecodingStrategy: dateDecodingStrategy)
270286
}
271287
}

Sources/UsefulNetworkLayer/NetworkLayer/NetworkLayer.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ public class NetworkLayer: NSObject, URLSessionDataDelegate {
233233

234234
if !request.responseBodyObject.shouldUseCustomInitializer {
235235
do {
236-
let jsonObject = try JSONDecoder().decode(request.responseBodyObject, from: data)
236+
let decoder = JSONDecoder()
237+
decoder.dateDecodingStrategy = request.dateDecodingStrategy
238+
239+
let jsonObject = try decoder.decode(request.responseBodyObject, from: data)
237240

238241
if request.autoCache, let cacheTiming = jsonObject.cachingEndsAt() {
239242
self._cache?.storeResponse(response, data: data, for: dataTask, expiry: cacheTiming)

0 commit comments

Comments
 (0)