Skip to content

Commit

Permalink
Merge pull request #127 from hotwired/path-config-request
Browse files Browse the repository at this point in the history
Allow passing URLRequest as PathConfiguration source
  • Loading branch information
zoejessica authored Jul 6, 2023
2 parents dcab269 + 4ef7797 commit 39059e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
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

0 comments on commit 39059e1

Please sign in to comment.