Skip to content

Commit

Permalink
Merge pull request #17 from HUK-COBURG/feature/url_session_configuration
Browse files Browse the repository at this point in the history
make url session configuration adjustable
  • Loading branch information
MarcoSp1911 authored Aug 19, 2024
2 parents c4befc8 + 4ded0c1 commit f574496
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
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

0 comments on commit f574496

Please sign in to comment.