Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/SwiftRestRequests/Deserializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public protocol Deserializer: Sendable {
/// Value for the `Accept` header sent alongside the request (for example `application/json`).
var acceptHeader: String { get }

var jsonDecoder: JSONDecoder? { get }

/// Creates a new instance of the deserializer.
init()

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

public typealias ResponseType = T

public var jsonDecoder: JSONDecoder? = JSONDecoder()

public let acceptHeader = MimeType.ApplicationJson.rawValue

public init() { }

public func deserialize(_ data: Data) throws -> T {
return try JSONDecoder().decode(T.self, from: data)
return try jsonDecoder!.decode(T.self, from: data)
}
}

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

public typealias ResponseType = Void

public var jsonDecoder: JSONDecoder? = nil

public let acceptHeader = MimeType.Void.rawValue

public init() { }
Expand All @@ -74,6 +80,8 @@ public struct VoidDeserializer: Deserializer {
public struct DataDeserializer: Deserializer {

public typealias ResponseType = Data

public var jsonDecoder: JSONDecoder? = nil

public let acceptHeader = MimeType.ApplicationOctetStream.rawValue

Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftRestRequests/RestApiCaller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ open class RestApiCaller : NSObject {
/// - Returns: The data returned by server and the corresponding `HTTPURLResponse`
private func makeCall<T: Deserializer>(_ relativePath: String?, httpMethod: HTTPMethod, payload: Data?, responseDeserializer: T, options: RestOptions) async throws -> (T.ResponseType?, HTTPStatusCode) {

// for deserializer with decoder set date decoding strategy
responseDeserializer.jsonDecoder?.dateDecodingStrategy = options.dateDecodingStrategy

let (data, httpResponse) = try await dataTask(relativePath: relativePath, httpMethod: httpMethod.rawValue, accept: responseDeserializer.acceptHeader, payload: payload, options: options)

let httpStatus = httpResponse.status
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftRestRequests/RestOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public struct RestOptions {
/// Any response outside of this set triggers a `RestError.unexpectedHttpStatusCode`.
public var expectedStatusCodes: [HTTPStatusCode]?

/// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
public var dateDecodingStrategy = JSONDecoder.DateDecodingStrategy.iso8601

/// Creates a new instance with default timeout and without overrides.
public init() {}
}
7 changes: 0 additions & 7 deletions Tests/SwiftRestRequestsTests/AbstractRestApiCallerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ class AbstractRestApiCallerTests: XCTestCase {
loggingLock.lock()
defer { loggingLock.unlock() }

#if os(Linux)
// Configure `swift-log` default logger
#else
/// Configure `swift-log` logging system to use OSLog backend
LoggingSystem.bootstrap(OSLogHandler.init)
#endif

Logger.SwiftRestRequests.security.logLevel = .trace
Logger.SwiftRestRequests.interceptor.logLevel = .trace
Logger.SwiftRestRequests.apiCaller.logLevel = .trace
Expand Down