Skip to content

Commit 0f8a54c

Browse files
committed
Add network debugging
1 parent bba4024 commit 0f8a54c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

WordPress/Classes/Networking/WordPressDotComClient.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Foundation
22
import WordPressAPI
33
import WordPressAPIInternal
44
import Combine
5+
import OSLog
56

67
actor WordPressDotComClient {
78

@@ -14,7 +15,9 @@ actor WordPressDotComClient {
1415
let delegate = WpApiClientDelegate(
1516
authProvider: .dynamic(dynamicAuthenticationProvider: provider),
1617
requestExecutor: WpRequestExecutor(urlSession: session),
17-
middlewarePipeline: WpApiMiddlewarePipeline(middlewares: []),
18+
middlewarePipeline: WpApiMiddlewarePipeline(middlewares: [
19+
WpComTrafficDebugger()
20+
]),
1821
appNotifier: WpComNotifier()
1922
)
2023

@@ -94,3 +97,27 @@ final class WpComNotifier: WpAppNotifier {
9497
NotificationCenter.default.post(name: Self.notificationName, object: nil)
9598
}
9699
}
100+
101+
final class WpComTrafficDebugger: Middleware {
102+
func process(
103+
requestExecutor: any WordPressAPIInternal.RequestExecutor,
104+
response: WordPressAPIInternal.WpNetworkResponse,
105+
request: WordPressAPIInternal.WpNetworkRequest,
106+
context: WordPressAPIInternal.RequestContext?
107+
) async throws -> WordPressAPIInternal.WpNetworkResponse {
108+
Logger.networking.debug("[\(request.method())] \(request.url())")
109+
return response
110+
}
111+
}
112+
113+
extension RequestMethod: @retroactive CustomStringConvertible {
114+
public var description: String {
115+
switch self {
116+
case .get: "GET"
117+
case .post: "POST"
118+
case .put: "PUT"
119+
case .delete: "DELETE"
120+
case .head: "HEAD"
121+
}
122+
}
123+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import OSLog
2+
3+
extension Logger {
4+
/// Using your bundle identifier is a great way to ensure a unique identifier.
5+
private static let subsystem = Bundle.main.bundleIdentifier!
6+
7+
/// Logs the view cycles like a view that appeared.
8+
static let networking = Logger(subsystem: subsystem, category: "network")
9+
}

0 commit comments

Comments
 (0)