From 48d4c42e25fdeee5b785a1389d4f20a18b89a1eb Mon Sep 17 00:00:00 2001 From: Marco Spitzenpfeil <> Date: Mon, 19 Aug 2024 10:48:18 +0200 Subject: [PATCH 1/2] make url session configuration adjustable --- Sources/SchwiftyResources/Network/HttpResource.swift | 12 +++++++++--- .../Network/UrlSession+SchwiftyResources.swift | 9 +++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Sources/SchwiftyResources/Network/HttpResource.swift b/Sources/SchwiftyResources/Network/HttpResource.swift index 0bd7c16..15a2ea3 100644 --- a/Sources/SchwiftyResources/Network/HttpResource.swift +++ b/Sources/SchwiftyResources/Network/HttpResource.swift @@ -53,6 +53,8 @@ public protocol HttpResource { var sendProgressHandler: ProgressHandler? { get } /// This handler will be called whenever the responses progress is evolving. This handler will only be called on iOS 15 and above. Defaults to nil. var receiveProgressHandler: ProgressHandler? { get } + /// The URLSessionConfiguration used for SchwiftyResourcesUrlSession + var urlSessionConfiguration: URLSessionConfiguration { get } } public extension HttpResource { @@ -89,9 +91,9 @@ public extension HttpResource { let urlRequest = try await buildUrlRequest() do { - let (data, urlResponse) = try await URLSession.schwiftyResourcesUrlSession.data(for: urlRequest, - sendProgressHandler: sendProgressHandler, - receiveProgressHandler: receiveProgressHandler) + let (data, urlResponse) = try await URLSession + .makeSchwiftyResourcesUrlSession(with: urlSessionConfiguration) + .data(for: urlRequest, sendProgressHandler: sendProgressHandler, receiveProgressHandler: receiveProgressHandler) do { guard let httpUrlResponse = urlResponse as? HTTPURLResponse else { @@ -132,6 +134,10 @@ public extension HttpResource { var receiveProgressHandler: ProgressHandler? { return nil } + + var urlSessionConfiguration: URLSessionConfiguration { + .default + } // MARK: - Internal functions diff --git a/Sources/SchwiftyResources/Network/UrlSession+SchwiftyResources.swift b/Sources/SchwiftyResources/Network/UrlSession+SchwiftyResources.swift index 966f453..a4bf1f3 100644 --- a/Sources/SchwiftyResources/Network/UrlSession+SchwiftyResources.swift +++ b/Sources/SchwiftyResources/Network/UrlSession+SchwiftyResources.swift @@ -25,10 +25,11 @@ import Foundation extension URLSession { private static var schwiftyResourcesUrlSessionDelegate = SchwiftyResourcesUrlSessionDelegate() - static var schwiftyResourcesUrlSession = URLSession(configuration: .default, - delegate: schwiftyResourcesUrlSessionDelegate, - delegateQueue: nil) - + + internal static func makeSchwiftyResourcesUrlSession(with configuration: URLSessionConfiguration) -> URLSession { + URLSession(configuration: configuration, delegate: schwiftyResourcesUrlSessionDelegate, delegateQueue: nil) + } + /// Convenience method to load data using an URLRequest. /// If using iOS 15 and above the given `sendProgressHandler` and `receiveProgressHandler` will be called while sending and receiving. /// Internally either `bytes(for:delegate)` (>= iOS 15) or `data(for:)` will be used. From 4ded0c1afd9fdc7ba56934dd8045205678485308 Mon Sep 17 00:00:00 2001 From: Marco Spitzenpfeil <> Date: Mon, 19 Aug 2024 12:16:19 +0200 Subject: [PATCH 2/2] fixed description --- Sources/SchwiftyResources/Network/HttpResource.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SchwiftyResources/Network/HttpResource.swift b/Sources/SchwiftyResources/Network/HttpResource.swift index 15a2ea3..1dc9838 100644 --- a/Sources/SchwiftyResources/Network/HttpResource.swift +++ b/Sources/SchwiftyResources/Network/HttpResource.swift @@ -53,7 +53,7 @@ public protocol HttpResource { var sendProgressHandler: ProgressHandler? { get } /// This handler will be called whenever the responses progress is evolving. This handler will only be called on iOS 15 and above. Defaults to nil. var receiveProgressHandler: ProgressHandler? { get } - /// The URLSessionConfiguration used for SchwiftyResourcesUrlSession + /// The URLSessionConfiguration used for SchwiftyResourcesUrlSession. var urlSessionConfiguration: URLSessionConfiguration { get } }