Skip to content

Commit

Permalink
Merge pull request #26 from diamirio/feature/os-log
Browse files Browse the repository at this point in the history
Added response logging
  • Loading branch information
kaulex99 authored Apr 11, 2024
2 parents 329c5d9 + dd52a73 commit ea770e4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 26 deletions.
48 changes: 41 additions & 7 deletions Sources/Async/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ open class Session<CL: Client> {

public init(
with client: CL,
using urlSession: URLSession = URLSession.shared
using urlSession: URLSession = URLSession(
configuration: .default,
delegate: URLSessionDelegateHandler(),
delegateQueue: nil
)
) {
self.client = client
self.urlSession = urlSession
Expand All @@ -25,15 +29,12 @@ open class Session<CL: Client> {

let (data, response) = try await urlSession.data(for: urlRequest)

if debug {
guard let response = response as? HTTPURLResponse else {
if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, visionOS 1.0, *) {
Logger.default.debug("\(urlRequest.cURLRepresentation)")
Logger.default.debug("no response.")
} else {
os_log("%s", log: .default, type: .debug, urlRequest.cURLRepresentation)
os_log("no response.", log: .default, type: .debug)
}
}

guard let response = response as? HTTPURLResponse else {
throw EndpointsError(
error: EndpointsParsingError.invalidData(
description: "Response was not a valid HTTPURLResponse"
Expand All @@ -42,6 +43,19 @@ open class Session<CL: Client> {
)
}

if debug {
var message = ""
message += "\(urlRequest.cURLRepresentation)\n"
message += "\(response.debugDescription)\n"
message += "\(data.debugDescription(encoding: response.stringEncoding))"

if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, visionOS 1.0, *) {
Logger.default.debug("\(message, privacy: .private)")
} else {
os_log("%s", log: .default, type: .debug, message)
}
}

do {
try await call.validate(response: response, data: data) // request-specific validation
try await client.validate(response: response, data: data) // global validation
Expand All @@ -53,3 +67,23 @@ open class Session<CL: Client> {
}
}
}

public class URLSessionDelegateHandler: NSObject, URLSessionTaskDelegate {
public func urlSession(
_ session: URLSession,
task: URLSessionTask,
willPerformHTTPRedirection response: HTTPURLResponse,
newRequest request: URLRequest,
completionHandler: @escaping (URLRequest?) -> Void
) {
let message = "\(response.url?.absoluteString ?? "") -> redirected to -> \(request.url?.absoluteString ?? "")"

if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, visionOS 1.0, *) {
Logger.default.debug("\(message, privacy: .private)")
} else {
os_log("%s", log: .default, type: .debug, message)
}

completionHandler(request)
}
}
32 changes: 23 additions & 9 deletions Sources/Core/Debugging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,32 @@ extension URLSessionTaskResult: CustomDebugStringConvertible {
}
}

public extension URLSessionTask {
var requestDescription: String {
guard let originalRequest, let realRequest = currentRequest else {
return "<task not started yet>"
public extension HTTPURLResponse {
override var debugDescription: String {
var description = "\(statusCode)\n"

allHeaderFields.forEach {
description.append("-\($0): \($1)\n")
}

var string = ""
if originalRequest != realRequest {
string = "\(originalRequest.cURLRepresentation)\n-> redirected to ->\n"
return description
}
}

public extension Data {
func debugDescription(encoding: String.Encoding) -> String {
var description = ""

if let string = String(data: self, encoding: encoding) {
if string.isEmpty {
description.append("<empty>")
} else {
description.append("\(string)")
}
} else {
description.append("<no data>")
}
string += "\(realRequest.cURLRepresentation)"

return string
return description
}
}
26 changes: 16 additions & 10 deletions Sources/Testing/FakeSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,13 @@ public class FakeSession<CL: Client>: Session<CL> {
) async throws -> (C.Parser.OutputType, HTTPURLResponse) {
let (response, data) = try await resultProvider.data(for: call)

if debug {
guard let response = response as? HTTPURLResponse else {
if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, visionOS 1.0, *) {
Logger.default.debug("\(call.request.cURLRepresentation)\n\(response)\n\(response)")
Logger.default.debug("no response.")
} else {
os_log(
"%s",
log: .default,
type: .debug,
"\(call.request.cURLRepresentation)\n\(response)\n\(response)"
)
os_log("no response.", log: .default, type: .debug)
}
}

guard let response = response as? HTTPURLResponse else {
throw EndpointsError(
error: EndpointsParsingError.invalidData(
description: "Response was not a valid HTTPURLResponse"
Expand All @@ -41,6 +34,19 @@ public class FakeSession<CL: Client>: Session<CL> {
)
}

if debug {
var message = ""
message += "\(call.request.cURLRepresentation)\n"
message += "\(response.debugDescription)\n"
message += "\(data.debugDescription(encoding: response.stringEncoding))"

if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, visionOS 1.0, *) {
Logger.default.debug("\(message, privacy: .private)")
} else {
os_log("%s", log: .default, type: .debug, message)
}
}

do {
try await call.validate(response: response, data: data) // request-specific validation
try await client.validate(response: response, data: data) // global validation
Expand Down

0 comments on commit ea770e4

Please sign in to comment.