From 639ba0b69dfa85f5032bd4ace76bdf01e7ed58f9 Mon Sep 17 00:00:00 2001 From: Okhan Okbay Date: Thu, 8 Aug 2024 16:48:45 +0100 Subject: [PATCH] Implement key decoding strategy alteration capability --- .github/workflows/verify-pr.yml | 2 +- Sources/CheckoutNetwork/CheckoutNetworkClient.swift | 5 ++++- .../ConfigurationModels/RequestConfiguration.swift | 7 +++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/verify-pr.yml b/.github/workflows/verify-pr.yml index cc91273..47e3607 100644 --- a/.github/workflows/verify-pr.yml +++ b/.github/workflows/verify-pr.yml @@ -14,7 +14,7 @@ concurrency: jobs: lint: name: SwiftLint - runs-on: macos-latest + runs-on: macos-13-large steps: - name: Checkout diff --git a/Sources/CheckoutNetwork/CheckoutNetworkClient.swift b/Sources/CheckoutNetwork/CheckoutNetworkClient.swift index 5f443be..5165837 100644 --- a/Sources/CheckoutNetwork/CheckoutNetworkClient.swift +++ b/Sources/CheckoutNetwork/CheckoutNetworkClient.swift @@ -47,7 +47,10 @@ public class CheckoutNetworkClient: CheckoutClientInterface { } do { - let dataResponse = try JSONDecoder().decode(T.self, from: data) + let decoder = JSONDecoder() + decoder.keyDecodingStrategy = configuration.decodingStrategy + let dataResponse = try decoder.decode(T.self, from: data) + completionHandler(.success(dataResponse)) } catch { completionHandler(.failure(error)) diff --git a/Sources/CheckoutNetwork/ConfigurationModels/RequestConfiguration.swift b/Sources/CheckoutNetwork/ConfigurationModels/RequestConfiguration.swift index 045ba5d..61cbda6 100644 --- a/Sources/CheckoutNetwork/ConfigurationModels/RequestConfiguration.swift +++ b/Sources/CheckoutNetwork/ConfigurationModels/RequestConfiguration.swift @@ -12,6 +12,7 @@ public struct RequestConfiguration { /// Request created from the consumers configuration private(set) public var request: URLRequest + var decodingStrategy: JSONDecoder.KeyDecodingStrategy /// Create request configuration with provided parameters. In case of failure will throw a `CheckoutNetworkError` /// @@ -26,7 +27,8 @@ public struct RequestConfiguration { queryItems: [String: String] = [:], customHeaders: [String: String] = [:], bodyData: Data? = nil, - mimeType: MIMEType = .JSON) throws { + mimeType: MIMEType = .JSON, + decodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) throws { // Validate URL can be broken down to components for formatting guard var components = URLComponents(url: path.url(), resolvingAgainstBaseURL: true) else { throw CheckoutNetworkError.invalidURL @@ -54,7 +56,8 @@ public struct RequestConfiguration { request.addValue(mimeType.rawValue, forHTTPHeaderField: MIMEType.key) } customHeaders.forEach { request.addValue($0.value, forHTTPHeaderField: $0.key) } - + + self.decodingStrategy = decodingStrategy self.request = request }