feat: global API request/response logging in APIClient

Every API call now logs: method, path, statusCode, response preview.
Network errors caught and logged before re-throwing.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
wangdl 2026-06-19 12:22:35 +08:00
parent 78d2c93b41
commit 5a7ee670af
2 changed files with 11 additions and 7 deletions

View File

@ -56,20 +56,24 @@ actor APIClient {
}
if let body {
let encoded = try JSONEncoder().encode(AnyEncodable(body))
request.httpBody = encoded
print("[APIClient] \(method) \(path) bodySize=\(encoded.count)")
} else {
print("[APIClient] \(method) \(path)")
request.httpBody = try JSONEncoder().encode(AnyEncodable(body))
}
let (data, response) = try await session.data(for: request)
print("[APIClient] \(method) \(path) -> response received")
let (data, response): (Data, URLResponse)
do {
(data, response) = try await session.data(for: request)
} catch {
print("[API] \(method) \(path)\(error.localizedDescription)")
throw APIError.networkError(error)
}
guard let httpResponse = response as? HTTPURLResponse else {
throw APIError.networkError(NSError(domain: "", code: -1))
}
let responseBody = String(data: data, encoding: .utf8)?.prefix(300) ?? ""
print("[API] \(method) \(path)\(httpResponse.statusCode) | \(responseBody)")
switch httpResponse.statusCode {
case 200, 201:
return try decodeResponse(data)