Skip to content

Commit 2fe4c23

Browse files
committed
Extended RestOptions with dateDecodingStrategy so for each request this can be set by default using .iso8601
1 parent cd24feb commit 2fe4c23

4 files changed

Lines changed: 15 additions & 8 deletions

File tree

Sources/SwiftRestRequests/Deserializer.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public protocol Deserializer: Sendable {
2929
/// Value for the `Accept` header sent alongside the request (for example `application/json`).
3030
var acceptHeader: String { get }
3131

32+
var jsonDecoder: JSONDecoder? { get }
33+
3234
/// Creates a new instance of the deserializer.
3335
init()
3436

@@ -44,12 +46,14 @@ public struct DecodableDeserializer<T: Decodable & Sendable>: Deserializer {
4446

4547
public typealias ResponseType = T
4648

49+
public var jsonDecoder: JSONDecoder? = JSONDecoder()
50+
4751
public let acceptHeader = MimeType.ApplicationJson.rawValue
4852

4953
public init() { }
5054

5155
public func deserialize(_ data: Data) throws -> T {
52-
return try JSONDecoder().decode(T.self, from: data)
56+
return try jsonDecoder!.decode(T.self, from: data)
5357
}
5458
}
5559

@@ -59,6 +63,8 @@ public struct VoidDeserializer: Deserializer {
5963

6064
public typealias ResponseType = Void
6165

66+
public var jsonDecoder: JSONDecoder? = nil
67+
6268
public let acceptHeader = MimeType.Void.rawValue
6369

6470
public init() { }
@@ -74,6 +80,8 @@ public struct VoidDeserializer: Deserializer {
7480
public struct DataDeserializer: Deserializer {
7581

7682
public typealias ResponseType = Data
83+
84+
public var jsonDecoder: JSONDecoder? = nil
7785

7886
public let acceptHeader = MimeType.ApplicationOctetStream.rawValue
7987

Sources/SwiftRestRequests/RestApiCaller.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ open class RestApiCaller : NSObject {
272272
/// - Returns: The data returned by server and the corresponding `HTTPURLResponse`
273273
private func makeCall<T: Deserializer>(_ relativePath: String?, httpMethod: HTTPMethod, payload: Data?, responseDeserializer: T, options: RestOptions) async throws -> (T.ResponseType?, HTTPStatusCode) {
274274

275+
// for deserializer with decoder set date decoding strategy
276+
responseDeserializer.jsonDecoder?.dateDecodingStrategy = options.dateDecodingStrategy
277+
275278
let (data, httpResponse) = try await dataTask(relativePath: relativePath, httpMethod: httpMethod.rawValue, accept: responseDeserializer.acceptHeader, payload: payload, options: options)
276279

277280
let httpStatus = httpResponse.status

Sources/SwiftRestRequests/RestOptions.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public struct RestOptions {
4343
/// Any response outside of this set triggers a `RestError.unexpectedHttpStatusCode`.
4444
public var expectedStatusCodes: [HTTPStatusCode]?
4545

46+
/// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
47+
public var dateDecodingStrategy = JSONDecoder.DateDecodingStrategy.iso8601
48+
4649
/// Creates a new instance with default timeout and without overrides.
4750
public init() {}
4851
}

Tests/SwiftRestRequestsTests/AbstractRestApiCallerTests.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ class AbstractRestApiCallerTests: XCTestCase {
2222
loggingLock.lock()
2323
defer { loggingLock.unlock() }
2424

25-
#if os(Linux)
26-
// Configure `swift-log` default logger
27-
#else
28-
/// Configure `swift-log` logging system to use OSLog backend
29-
LoggingSystem.bootstrap(OSLogHandler.init)
30-
#endif
31-
3225
Logger.SwiftRestRequests.security.logLevel = .trace
3326
Logger.SwiftRestRequests.interceptor.logLevel = .trace
3427
Logger.SwiftRestRequests.apiCaller.logLevel = .trace

0 commit comments

Comments
 (0)