Skip to content

Commit

Permalink
added possibility to change the JSONDecoder date decoding strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Homann authored and Philipp Homann committed Jun 4, 2021
1 parent 1c9e074 commit 6c93a33
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
30 changes: 23 additions & 7 deletions Sources/UsefulNetworkLayer/NetworkLayer/APIConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public struct APIConfiguration<T,S> where T: ResponseBodyParsable, S: ErrorRespo

/// The timeout value for the request wait time.
public var timeOut: Int

/// The date decoding strategy. Default is .defferedToDate
public var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy


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

var url = URL(string: hostURL)
url?.appendPathComponent(endPoint)
Expand All @@ -100,7 +106,8 @@ public struct APIConfiguration<T,S> where T: ResponseBodyParsable, S: ErrorRespo
cachingTime: cachingTime,
isMainOperation: isMainOperation,
autoCache: autoCache,
timeOut: timeOut)
timeOut: timeOut,
dateDecodingStrategy: dateDecodingStrategy)
}

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

self.requestURL = url
self.requestType = requestType
Expand All @@ -140,6 +149,7 @@ public struct APIConfiguration<T,S> where T: ResponseBodyParsable, S: ErrorRespo
self.autoCache = autoCache
self.timeOut = timeOut
self.errorResponseBodyObject = errorType
self.dateDecodingStrategy = dateDecodingStrategy
}

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

self.init(hostURL: hostURL,
endPoint: endPoint,
Expand All @@ -228,7 +240,8 @@ public extension APIConfiguration where T: ResponseBodyParsable, S == DefaultAPI
cachingTime: cachingTime,
isMainOperation: isMainOperation,
autoCache: autoCache,
timeOut: timeOut)
timeOut: timeOut,
dateDecodingStrategy: dateDecodingStrategy)
}

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

self.init(url: url,
requestType: requestType,
Expand All @@ -266,6 +281,7 @@ public extension APIConfiguration where T: ResponseBodyParsable, S == DefaultAPI
cachingTime: cachingTime,
isMainOperation: isMainOperation,
autoCache: autoCache,
timeOut: timeOut)
timeOut: timeOut,
dateDecodingStrategy: dateDecodingStrategy)
}
}
5 changes: 4 additions & 1 deletion Sources/UsefulNetworkLayer/NetworkLayer/NetworkLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ public class NetworkLayer: NSObject, URLSessionDataDelegate {

if !request.responseBodyObject.shouldUseCustomInitializer {
do {
let jsonObject = try JSONDecoder().decode(request.responseBodyObject, from: data)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = request.dateDecodingStrategy

let jsonObject = try decoder.decode(request.responseBodyObject, from: data)

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

0 comments on commit 6c93a33

Please sign in to comment.