-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURLRequest+Logging.swift
More file actions
146 lines (131 loc) · 5.37 KB
/
URLRequest+Logging.swift
File metadata and controls
146 lines (131 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// Request.swift
// Africa
// // Created by Håkon Bogen on 01/06/2023. //
import Combine
import Foundation
extension URLSession {
/// Use this instead, to get logging on your requests
func dataTask(withLogging request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> ()) -> URLSessionDataTask {
return self.dataTask(with: request) { data, response, error in
URLRequest.debugPrintYAML(request: request, response: response, received: data, error: error)
completionHandler(data, response, error)
}
}
}
extension URLRequest {
private static func headerTransform(_ header: [AnyHashable: Any]?, indent: Int) -> String {
return (header ?? [:]).map { "\(Array(repeating: " ", count: indent).joined())\($0): \($1)" }.joined(separator: "\n")
}
private static func dataTransform(_ data: Data?) -> String {
return data.flatMap { String(data: $0, encoding: .utf8) } ?? "null"
}
private static func bodyToJson(data: Data?) -> String {
let logPrettyPrint = true // Set this in some kind of debug place
if logPrettyPrint {
if let httpBody = data,
let jsonObject = try? JSONSerialization.jsonObject(with: httpBody, options: []),
let data = try? JSONSerialization.data(withJSONObject: jsonObject, options: JSONSerialization.WritingOptions.prettyPrinted),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) {
return prettyPrintedString as String
}
return ""
} else {
return dataTransform(data)
}
}
private static func debugYAML(request: URLRequest?) -> String? {
guard let request = request,
let method = request.httpMethod,
let url = request.url
else { return nil }
return """
Request:
Method: \(method)
URL: \(url)
Header:
\(headerTransform(request.allHTTPHeaderFields, indent: 2))
Body: \(bodyToJson(data: request.httpBody))
"""
}
private static func debugCURL(request: URLRequest?) -> String? {
guard let request = request,
let httpMethod = request.httpMethod,
let url = request.url, let allHTTPHeaderFields = request.allHTTPHeaderFields else { return nil }
let bodyComponents: [String]
if let data = request.httpBody.flatMap({ String(data: $0, encoding: .utf8) }) {
// switch parameterEncoding {
// case .query:
// bodyComponents = data.split(separator: "&").map { "-F \($0)" }
// default:
bodyComponents = ["-d", "'\(data)'"]
// }
} else {
// bodyComponents = []
return nil
}
let method = "-X \(httpMethod)"
let headers = allHTTPHeaderFields.map { "-H '\($0.key): \($0.value)'" }
return ((["curl", method] + headers + bodyComponents + [url.absoluteString]) as [String])
.joined(separator: " ")
}
private static func debugYAML(response: URLResponse?, data: Data?) -> String? {
guard let response = response as? HTTPURLResponse else { return nil }
return """
Response:
Code: \(response.statusCode)
Header:
\(headerTransform(response.allHeaderFields, indent: 2))
Body: \(bodyToJson(data: data))
"""
}
private static func debugYAML(responseError error: Error?) -> String? {
guard let error = error else { return nil }
return """
Response:
Error: \(error)
"""
}
private static func curlPrintable(request: URLRequest?) -> String {
if let string = debugCURL(request: request) {
return """
# cURL format:
# \(string)
#######################
"""
}
return ""
}
static func debugPrintYAML(request: URLRequest?, response: URLResponse?, received: Data?, error: Error? = nil) {
let responseYaml = debugYAML(responseError: error) ?? debugYAML(response: response, data: received)
let yaml = [debugYAML(request: request), responseYaml]
.compactMap { $0 }
.joined(separator: "\n")
let info = """
#######################
##### Request Log #####
#######################
\(curlPrintable(request: request))
# YAML format:
\(yaml)
#######################
"""
print("\n\(info)\n")
}
}
// For Combine requests
extension Publisher where Output == (data: Data, response: URLResponse),
Self == URLSession.DataTaskPublisher
{
func sinkWithLogging(completion: @escaping (Subscribers.Completion<Self.Failure>) -> Void, receiveValue: @escaping ((_ data: Data, _ response: URLResponse) -> ())) -> AnyCancellable {
return self.sink { error in
if case .failure(let error) = error {
URLRequest.debugPrintYAML(request: self.request, response: nil, received: nil, error: error)
}
completion(error)
} receiveValue: { data, response in
URLRequest.debugPrintYAML(request: self.request, response: response, received: data)
receiveValue(data, response)
}
}
}