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 url session configuration adjustable #17

Merged
merged 2 commits into from
Aug 19, 2024
Merged
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
12 changes: 9 additions & 3 deletions Sources/SchwiftyResources/Network/HttpResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -132,6 +134,10 @@ public extension HttpResource {
var receiveProgressHandler: ProgressHandler? {
return nil
}

var urlSessionConfiguration: URLSessionConfiguration {
.default
}

// MARK: - Internal functions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading