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

Allow passing URLRequest as PathConfiguration source #127

Merged
merged 2 commits into from
Jul 6, 2023
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
19 changes: 16 additions & 3 deletions Source/Path Configuration/PathConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ public protocol PathConfigurationDelegate: AnyObject {
func pathConfigurationDidUpdate()
}

public struct PathConfigurationLoaderOptions {
public init(urlSessionConfiguration: URLSessionConfiguration? = nil) {
self.urlSessionConfiguration = urlSessionConfiguration
}

/// If present, the ``PathConfigurationLoader`` will initialize a new `URLSession` with
/// this configuration to make its network request
public let urlSessionConfiguration: URLSessionConfiguration?
}

public final class PathConfiguration {
public weak var delegate: PathConfigurationDelegate?

Expand All @@ -26,8 +36,9 @@ public final class PathConfiguration {

/// Multiple sources will be loaded in order
/// Remote sources should be last since they're loaded async
public init(sources: [Source] = []) {
public init(sources: [Source] = [], options: PathConfigurationLoaderOptions? = nil) {
self.sources = sources
self.options = options
load()
}

Expand Down Expand Up @@ -63,11 +74,13 @@ public final class PathConfiguration {
}

// MARK: - Loading

private let options: PathConfigurationLoaderOptions?

private var loader: PathConfigurationLoader?

private func load() {
loader = PathConfigurationLoader(sources: sources)
loader = PathConfigurationLoader(sources: sources, options: options)
loader?.load { [weak self] config in
self?.update(with: config)
}
Expand Down
8 changes: 6 additions & 2 deletions Source/Path Configuration/PathConfigurationLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ final class PathConfigurationLoader {
private let cacheDirectory = "Turbo"
private let configurationCacheFilename = "path-configuration.json"
private let sources: [PathConfiguration.Source]
private let options: PathConfigurationLoaderOptions?
private var completionHandler: PathConfigurationLoaderCompletionHandler?

init(sources: [PathConfiguration.Source]) {
init(sources: [PathConfiguration.Source], options: PathConfigurationLoaderOptions? = nil) {
self.sources = sources
self.options = options
}

func load(then completion: @escaping PathConfigurationLoaderCompletionHandler) {
Expand Down Expand Up @@ -37,7 +39,9 @@ final class PathConfigurationLoader {
loadData(data)
}

URLSession.shared.dataTask(with: url) { [weak self] data, response, error in
let session = options?.urlSessionConfiguration.map { URLSession(configuration: $0) } ?? URLSession.shared

session.dataTask(with: url) { [weak self] data, response, error in
guard let data = data,
let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200
Expand Down