From 6680d4ed87fa4e8589c8f3b3bab5e9d2eb491813 Mon Sep 17 00:00:00 2001 From: Jelle Vandebeeck Date: Mon, 16 Aug 2021 21:59:29 +0200 Subject: [PATCH 1/4] Make it possible to alter the session configuration. --- Sources/Configuration/Configuration.swift | 10 ++++++++++ Sources/Service/NetworkService.swift | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/Configuration/Configuration.swift b/Sources/Configuration/Configuration.swift index cc64fa9..3b643e1 100644 --- a/Sources/Configuration/Configuration.swift +++ b/Sources/Configuration/Configuration.swift @@ -25,4 +25,14 @@ public protocol Configuration { /// /// - return: The request headers. func headers(for request: Request) -> RequestHeaders? + /// Alter the session configuration before triggering the request. + /// + /// - parameters configuration: The session configiguration used by the `URLSession`. + func alter(configuration: URLSessionConfiguration) +} + +public extension Configuration { + func alter(configuration: URLSessionConfiguration) { + // Default implementation does nothing. + } } diff --git a/Sources/Service/NetworkService.swift b/Sources/Service/NetworkService.swift index d807dc7..d81cc6b 100644 --- a/Sources/Service/NetworkService.swift +++ b/Sources/Service/NetworkService.swift @@ -35,7 +35,9 @@ class NetworkService: NSObject { // Trigger the loggers before the request is done. configuration.start(urlRequest: urlRequest) // Prepare the session. - let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil) + let sessionConfiguration: URLSessionConfiguration = .default + configuration.alter(configuration: sessionConfiguration) + let session = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil) // Execute the task. let task = session.dataTask(with: urlRequest) { [weak self] data, urlResponse, error in /// When the url response has a response error and an interceptor is set we check if the request flow From 6e2ba8f3692837719a594cdfb38c44b6f000206a Mon Sep 17 00:00:00 2001 From: Jelle Vandebeeck Date: Wed, 21 Jun 2023 15:13:54 +0200 Subject: [PATCH 2/4] Add watchOS support. --- Package.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index e494237..4f3f2f6 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,8 @@ let package = Package( platforms: [ .macOS(.v10_12), .iOS(.v10), - .tvOS(.v10) + .tvOS(.v10), + .watchOS(.v5) ], products: [ .library(name: "Cara", targets: ["Cara"]) @@ -16,4 +17,4 @@ let package = Package( .target(name: "Cara", dependencies: [], path: "Sources") ], swiftLanguageVersions: [.v4_2] -) \ No newline at end of file +) From 3e75824192a6e4c7fedcf4353070e7a3c5663b55 Mon Sep 17 00:00:00 2001 From: Jelle Vandebeeck Date: Wed, 21 Jun 2023 15:23:20 +0200 Subject: [PATCH 3/4] Fix available --- Sources/Logger/Configuration+Logger.swift | 1 + Sources/Logger/ConsoleLogger.swift | 1 + Sources/Logger/Logger.swift | 1 + 3 files changed, 3 insertions(+) diff --git a/Sources/Logger/Configuration+Logger.swift b/Sources/Logger/Configuration+Logger.swift index 24a6c7d..3850758 100644 --- a/Sources/Logger/Configuration+Logger.swift +++ b/Sources/Logger/Configuration+Logger.swift @@ -12,6 +12,7 @@ extension Configuration { loggers?.forEach { $0.start(urlRequest: urlRequest) } } + @available(iOS 10.0, *) func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) { loggers?.forEach { $0.end(urlRequest: urlRequest, urlResponse: urlResponse, metrics: metrics, error: error) } } diff --git a/Sources/Logger/ConsoleLogger.swift b/Sources/Logger/ConsoleLogger.swift index f9c0b19..d096993 100644 --- a/Sources/Logger/ConsoleLogger.swift +++ b/Sources/Logger/ConsoleLogger.swift @@ -23,6 +23,7 @@ extension ConsoleLogger: Logger { } // swiftlint:disable function_body_length + @available(iOS 10.0, *) public func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) { guard let method = urlRequest.httpMethod, diff --git a/Sources/Logger/Logger.swift b/Sources/Logger/Logger.swift index a20aa1f..08ff1fc 100644 --- a/Sources/Logger/Logger.swift +++ b/Sources/Logger/Logger.swift @@ -19,5 +19,6 @@ public protocol Logger { /// - parameters urlResponse: The response of the request. /// - parameters metrics: The metrics for the request. /// - parameters error: The error is an error occured. + @available(iOS 10.0, *) func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) } From f4927224fec99368ab56285e52ab7fbaa41a5ac7 Mon Sep 17 00:00:00 2001 From: Jelle Vandebeeck Date: Wed, 21 Jun 2023 15:28:53 +0200 Subject: [PATCH 4/4] remove url task metrics --- Example/Tests/Mocks/MockedLogger.swift | 5 --- Sources/Logger/Configuration+Logger.swift | 5 --- Sources/Logger/ConsoleLogger.swift | 50 ----------------------- Sources/Logger/Logger.swift | 8 ---- Sources/Service/NetworkService.swift | 12 ------ 5 files changed, 80 deletions(-) diff --git a/Example/Tests/Mocks/MockedLogger.swift b/Example/Tests/Mocks/MockedLogger.swift index 3e37e2c..752bb35 100644 --- a/Example/Tests/Mocks/MockedLogger.swift +++ b/Example/Tests/Mocks/MockedLogger.swift @@ -14,9 +14,4 @@ class MockedLogger: Logger { func start(urlRequest: URLRequest) { didTriggerStartRequest = urlRequest } - - private(set) var didTriggerEnd: Bool = false - func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) { - didTriggerEnd = true - } } diff --git a/Sources/Logger/Configuration+Logger.swift b/Sources/Logger/Configuration+Logger.swift index 3850758..72cc7ca 100644 --- a/Sources/Logger/Configuration+Logger.swift +++ b/Sources/Logger/Configuration+Logger.swift @@ -11,9 +11,4 @@ extension Configuration { func start(urlRequest: URLRequest) { loggers?.forEach { $0.start(urlRequest: urlRequest) } } - - @available(iOS 10.0, *) - func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) { - loggers?.forEach { $0.end(urlRequest: urlRequest, urlResponse: urlResponse, metrics: metrics, error: error) } - } } diff --git a/Sources/Logger/ConsoleLogger.swift b/Sources/Logger/ConsoleLogger.swift index d096993..04a0e27 100644 --- a/Sources/Logger/ConsoleLogger.swift +++ b/Sources/Logger/ConsoleLogger.swift @@ -21,54 +21,4 @@ extension ConsoleLogger: Logger { let url = urlRequest.url else { return } os_log("☁️ %{public}@: %{public}@", log: OSLog.request, type: .info, method, url.absoluteString) } - - // swiftlint:disable function_body_length - @available(iOS 10.0, *) - public func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) { - guard - let method = urlRequest.httpMethod, - let httpResponse = urlResponse as? HTTPURLResponse, - let url = urlRequest.url, - let transactionMetric = metrics.transactionMetrics.first, - let requestEndDate = transactionMetric.requestEndDate else { return } - - if let domainLookupStartDate = transactionMetric.domainLookupStartDate { - let totalDuration = requestEndDate.timeIntervalSince(domainLookupStartDate) - if let error = error { - os_log("⛈ %{public}@ %i: %{public}@ 💥 %{public}@ ⏳ %.3fms", - log: OSLog.request, - type: .info, - method, - httpResponse.statusCode, - url.absoluteString, - error.localizedDescription, - totalDuration) - } else { - os_log("☀️ %{public}@ %i: %{public}@ ⏳ %.3fms", - log: OSLog.request, - type: .info, - method, - httpResponse.statusCode, - url.absoluteString, - totalDuration) - } - } else { - if let error = error { - os_log("⛈ %{public}@ %i: %{public}@ 💥 %{public}@", - log: OSLog.request, - type: .info, - method, - httpResponse.statusCode, - url.absoluteString, - error.localizedDescription) - } else { - os_log("☀️ %{public}@ %i: %{public}@", - log: OSLog.request, - type: .info, - method, - httpResponse.statusCode, - url.absoluteString) - } - } - } } diff --git a/Sources/Logger/Logger.swift b/Sources/Logger/Logger.swift index 08ff1fc..6d4bde1 100644 --- a/Sources/Logger/Logger.swift +++ b/Sources/Logger/Logger.swift @@ -13,12 +13,4 @@ public protocol Logger { /// /// - parameters urlRequest: The request that is will be executed. func start(urlRequest: URLRequest) - /// The `end` function is triggered just after the request finised collecting the metrics. - /// - /// - parameters urlRequest: The request that is was executed after redirection. - /// - parameters urlResponse: The response of the request. - /// - parameters metrics: The metrics for the request. - /// - parameters error: The error is an error occured. - @available(iOS 10.0, *) - func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) } diff --git a/Sources/Service/NetworkService.swift b/Sources/Service/NetworkService.swift index d81cc6b..a4d9c4e 100644 --- a/Sources/Service/NetworkService.swift +++ b/Sources/Service/NetworkService.swift @@ -93,16 +93,4 @@ extension NetworkService: URLSessionDataDelegate { pinningService.handle(serverTrust, host: host, completionHandler: completionHandler) } - - func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) { - guard - let urlRequest = task.currentRequest, - let urlResponse = task.response else { return } - - // Trigger the loggers when the request finished. - configuration.end(urlRequest: urlRequest, - urlResponse: urlResponse, - metrics: metrics, - error: task.error ?? urlResponse.responseError) - } }