Skip to content

Commit

Permalink
Introduce configuration to the loader to allow URLSession configuration
Browse files Browse the repository at this point in the history
This set up allows for future configuration of the loader and keeps the PathConfiguration.Source enum focused on the source of the config file rather than the specific method of obtaining the file.
  • Loading branch information
zoejessica committed Jul 6, 2023
1 parent cb1b6f6 commit 4ef7797
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
20 changes: 16 additions & 4 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 All @@ -92,6 +105,5 @@ extension PathConfiguration {
case data(Data)
case file(URL)
case server(URL)
case serverRequest(URLRequest)
}
}
15 changes: 6 additions & 9 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 All @@ -23,8 +25,6 @@ final class PathConfigurationLoader {
loadFile(url)
case .server(let url):
download(from: url)
case .serverRequest(let request):
download(with: request)
}
}
}
Expand All @@ -34,17 +34,14 @@ final class PathConfigurationLoader {
private func download(from url: URL) {
precondition(!url.isFileURL, "URL provided for server is a file url")

download(with: URLRequest(url: url))
}

private func download(with request: URLRequest) {

// Immediately load most recent cached version if available
if let data = cachedData() {
loadData(data)
}

URLSession.shared.dataTask(with: request) { [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 4ef7797

Please sign in to comment.