Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to alter the URLSessionConfiguration. #42

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Example/Tests/Mocks/MockedLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand All @@ -16,4 +17,4 @@ let package = Package(
.target(name: "Cara", dependencies: [], path: "Sources")
],
swiftLanguageVersions: [.v4_2]
)
)
10 changes: 10 additions & 0 deletions Sources/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
4 changes: 0 additions & 4 deletions Sources/Logger/Configuration+Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@ extension Configuration {
func start(urlRequest: URLRequest) {
loggers?.forEach { $0.start(urlRequest: urlRequest) }
}

func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) {
loggers?.forEach { $0.end(urlRequest: urlRequest, urlResponse: urlResponse, metrics: metrics, error: error) }
}
}
49 changes: 0 additions & 49 deletions Sources/Logger/ConsoleLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +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
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)
}
}
}
}
7 changes: 0 additions & 7 deletions Sources/Logger/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +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.
func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?)
}
16 changes: 3 additions & 13 deletions Sources/Service/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,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)
}
}