Skip to content

Commit d4f91f0

Browse files
Merge pull request Adyen#12 from Adyen/adyen-print-module-name
refactored adyenPrinting to log the module name printing and small other refactoring
2 parents 9089b6f + dac63f8 commit d4f91f0

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
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/Helpers/Logging.swift

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55
//
66

77
import func Darwin.fputs
8+
import Foundation
89

910
/// Provides control over SDK logging.
1011
public enum Logging {
1112
/// Indicates whether to enable printing to the console.
1213
public static var isEnabled: Bool = false
1314
}
1415

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

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

0 commit comments

Comments
 (0)