Skip to content

Commit a68081f

Browse files
refactored adyenPrinting to log the module name printing and small other refactoring
1 parent 9089b6f commit a68081f

File tree

5 files changed

+52
-25
lines changed

5 files changed

+52
-25
lines changed

AdyenNetworking/APIClient/APIClient.swift

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,20 @@ public final class APIClient: APIClientProtocol, AsyncAPIClientProtocol {
179179
adyenPrint("---- Request (/\(request.path)) ----")
180180

181181
if let body = urlRequest.httpBody {
182-
printAsJSON(body)
182+
adyenPrintAsJSON(body)
183183
}
184184

185185
adyenPrint("---- Request base url (/\(request.path)) ----")
186186
adyenPrint(apiContext.environment.baseURL)
187187

188188
if let headers = urlRequest.allHTTPHeaderFields {
189189
adyenPrint("---- Request Headers (/\(request.path)) ----")
190-
adyenPrint(headers)
190+
adyenPrintAsJSON(headers)
191191
}
192192

193193
if let queryParams = urlRequest.url?.queryParameters {
194194
adyenPrint("---- Request query (/\(request.path)) ----")
195-
adyenPrint(queryParams)
195+
adyenPrintAsJSON(queryParams)
196196
}
197197

198198
}
@@ -202,10 +202,10 @@ public final class APIClient: APIClientProtocol, AsyncAPIClientProtocol {
202202
adyenPrint(result.statusCode)
203203

204204
adyenPrint("---- Response Headers (/\(request.path)) ----")
205-
adyenPrint(result.headers)
205+
adyenPrintAsJSON(result.headers)
206206

207207
adyenPrint("---- Response (/\(request.path)) ----")
208-
printAsJSON(result.data)
208+
adyenPrintAsJSON(result.data)
209209
}
210210

211211
/// :nodoc:
@@ -232,18 +232,3 @@ public final class APIClient: APIClientProtocol, AsyncAPIClientProtocol {
232232
}
233233

234234
}
235-
236-
private func printAsJSON(_ data: Data) {
237-
guard Logging.isEnabled else { return }
238-
do {
239-
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
240-
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: [.prettyPrinted])
241-
guard let jsonString = String(data: jsonData, encoding: .utf8) else { return }
242-
243-
adyenPrint(jsonString)
244-
} catch {
245-
if let string = String(data: data, encoding: .utf8) {
246-
adyenPrint(string)
247-
}
248-
}
249-
}

AdyenNetworking/APIClient/Errors.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public enum APIClientError: LocalizedError {
2121
public struct ParsingError: LocalizedError {
2222

2323
/// HTTP Headers.
24-
public let headers: [String: String]
24+
public let headers: [String: Any]
2525

2626
/// HTTP Status Code.
2727
public let statusCode: Int

AdyenNetworking/APIClient/HTTPResponse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99
public struct HTTPResponse<R: Response> {
1010

1111
/// HTTP Headers.
12-
public let headers: [String: String]
12+
public let headers: [String: Any]
1313

1414
/// HTTP Status Code.
1515
public let statusCode: Int

AdyenNetworking/Helpers/Logging.swift

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ public enum Logging {
1212
public static var isEnabled: Bool = false
1313
}
1414

15-
internal func adyenPrint(_ items: Any..., separator: String = " ", terminator: String = "\n") {
15+
@_spi(AdyenInternal)
16+
public func adyenPrint(_ items: Any..., separator: String = " ", terminator: String = "\n", fileId: String = #fileID) {
1617
guard Logging.isEnabled else { return }
18+
let moduleName = fileId.split(separator: "/").first ?? "AdyenNetworking"
19+
20+
var items = items
21+
items.insert("[\(moduleName)]", at: 0)
22+
items.insert(ISO8601DateFormatter().string(from: Date()), at: 0)
23+
1724
var idx = items.startIndex
1825
let endIdx = items.endIndex
1926

@@ -22,3 +29,38 @@ internal func adyenPrint(_ items: Any..., separator: String = " ", terminator: S
2229
idx += 1
2330
} while idx < endIdx
2431
}
32+
33+
@_spi(AdyenInternal)
34+
public func adyenPrintAsJSON(_ dictionary: [String: Any]) {
35+
guard Logging.isEnabled else { return }
36+
do {
37+
let jsonData = try JSONSerialization.data(withJSONObject: dictionary, options: jsonOptions())
38+
adyenPrintAsJSON(jsonData)
39+
} catch {
40+
adyenPrint(dictionary)
41+
}
42+
}
43+
44+
@_spi(AdyenInternal)
45+
public func adyenPrintAsJSON(_ data: Data) {
46+
guard Logging.isEnabled else { return }
47+
do {
48+
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
49+
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: jsonOptions())
50+
guard let jsonString = String(data: jsonData, encoding: .utf8) else { return }
51+
52+
adyenPrint(jsonString)
53+
} catch {
54+
if let string = String(data: data, encoding: .utf8) {
55+
adyenPrint(string)
56+
}
57+
}
58+
}
59+
60+
private func jsonOptions() -> JSONSerialization.WritingOptions {
61+
if #available(iOS 13.0, *) {
62+
return [.prettyPrinted, .withoutEscapingSlashes]
63+
} else {
64+
return [.prettyPrinted]
65+
}
66+
}

AdyenNetworking/Helpers/URLSessionHelpers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ internal struct URLSessionSuccess {
1111

1212
internal let statusCode: Int
1313

14-
internal let headers: [String: String]
14+
internal let headers: [String: Any]
1515

1616
internal init(data: Data?, response: URLResponse?) throws {
1717
guard let data = data,
1818
let httpResponse = response as? HTTPURLResponse,
19-
let headers = httpResponse.allHeaderFields as? [String: String] else {
19+
let headers = httpResponse.allHeaderFields as? [String: Any] else {
2020
throw APIClientError.invalidResponse
2121
}
2222

0 commit comments

Comments
 (0)