-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial Address Implementation * AbsoluteURLSessionClient and updated Downloader deprecation
- Loading branch information
1 parent
eb2ace5
commit c45e400
Showing
18 changed files
with
627 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Foundation | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public extension URLCache { | ||
enum Capacity { | ||
case bytes(Int) | ||
case megabytes(Int) | ||
case gigabytes(Int) | ||
|
||
public static var twentyFiveMB: Capacity = .megabytes(25) | ||
public static var twoHundredMB: Capacity = .megabytes(200) | ||
|
||
public var bytes: Int { | ||
switch self { | ||
case .bytes(let value): | ||
return value | ||
case .megabytes(let value): | ||
return value * (1024 * 1024) | ||
case .gigabytes(let value): | ||
return value * (1024 * 1024 * 1024) | ||
} | ||
} | ||
} | ||
|
||
convenience init(memoryCapacity: Capacity = .twentyFiveMB, diskCapacity: Capacity = .twoHundredMB) { | ||
#if canImport(FoundationNetworking) | ||
self.init(memoryCapacity: memoryCapacity.bytes, diskCapacity: diskCapacity.bytes, diskPath: "SessionPlusCache") | ||
#else | ||
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) { | ||
self.init(memoryCapacity: memoryCapacity.bytes, diskCapacity: diskCapacity.bytes) | ||
} else { | ||
#if targetEnvironment(macCatalyst) | ||
self.init(memoryCapacity: memoryCapacity.bytes, diskCapacity: diskCapacity.bytes) | ||
#else | ||
self.init(memoryCapacity: memoryCapacity.bytes, diskCapacity: diskCapacity.bytes, diskPath: "SessionPlusCache") | ||
#endif | ||
} | ||
#endif | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
Sources/SessionPlus/Extensions/URLSessionConfiguration+SessionPlus.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Foundation | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
|
||
public extension URLSessionConfiguration { | ||
/// A `URLSessionConfiguration` which includes a `URLCache` and has the `.returnCacheDataElseLoad` policy applied. | ||
static func cachingElseLoad( | ||
memoryCapacity: URLCache.Capacity = .twentyFiveMB, | ||
diskCapacity: URLCache.Capacity = .twoHundredMB | ||
) -> URLSessionConfiguration { | ||
let configuration: URLSessionConfiguration = .default | ||
configuration.urlCache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity) | ||
configuration.requestCachePolicy = .returnCacheDataElseLoad | ||
return configuration | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
Sources/SessionPlus/Implementation/AbsoluteURLSessionClient.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import Foundation | ||
#if canImport(FoundationNetworking) | ||
import FoundationNetworking | ||
#endif | ||
#if canImport(Combine) | ||
import Combine | ||
#endif | ||
|
||
/// A `Client` implementation that operates expecting all requests use _absolute_ urls. | ||
open class AbsoluteURLSessionClient: Client { | ||
|
||
public let session: URLSession | ||
|
||
public init(sessionConfiguration: URLSessionConfiguration = .default, sessionDelegate: URLSessionDelegate? = nil) { | ||
self.session = URLSession(configuration: sessionConfiguration, delegate: sessionDelegate, delegateQueue: nil) | ||
} | ||
|
||
#if swift(>=5.5.2) && (os(macOS) || os(iOS) || os(tvOS) || os(watchOS)) | ||
/// Implementation that uses the `URLSession` async/await concurrency apis for handling a `Request`/`Response` interaction. | ||
/// | ||
/// The `URLSession` api is only available on Apple platforms, as the `FoundationNetworking` version has not been updated. | ||
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) | ||
public func performRequest(_ request: Request) async throws -> Response { | ||
let urlRequest = try URLRequest(request: request) | ||
let sessionResponse = try await session.data(for: urlRequest) | ||
return AnyResponse(statusCode: sessionResponse.1.statusCode, headers: sessionResponse.1.headers, data: sessionResponse.0) | ||
} | ||
#endif | ||
|
||
#if canImport(Combine) | ||
/// Implementation that uses the `URLSession.DataTaskPublisher` to handle the `Request`/`Response` interaction. | ||
public func performRequest(_ request: Request) -> AnyPublisher<Response, Error> { | ||
let urlRequest: URLRequest | ||
do { | ||
urlRequest = try URLRequest(request: request) | ||
} catch { | ||
return Fail(outputType: Response.self, failure: error).eraseToAnyPublisher() | ||
} | ||
|
||
return session | ||
.dataTaskPublisher(for: urlRequest) | ||
.tryMap { taskResponse -> Response in | ||
AnyResponse(statusCode: taskResponse.response.statusCode, headers: taskResponse.response.headers, data: taskResponse.data) | ||
} | ||
.eraseToAnyPublisher() | ||
} | ||
#endif | ||
|
||
/// Implementation that uses the default `URLSessionDataTask` methods for handling a `Request`/`Response` interaction. | ||
public func performRequest(_ request: Request, completion: @escaping (Result<Response, Error>) -> Void) { | ||
let urlRequest: URLRequest | ||
do { | ||
urlRequest = try URLRequest(request: request) | ||
} catch { | ||
completion(.failure(error)) | ||
return | ||
} | ||
|
||
session.dataTask(with: urlRequest) { data, urlResponse, error in | ||
guard error == nil else { | ||
completion(.failure(error!)) | ||
return | ||
} | ||
|
||
guard let httpResponse = urlResponse else { | ||
completion(.failure(URLError(.cannotParseResponse))) | ||
return | ||
} | ||
|
||
let response = AnyResponse(statusCode: httpResponse.statusCode, headers: httpResponse.headers, data: data ?? Data()) | ||
completion(.success(response)) | ||
} | ||
.resume() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.