Add async cover methods to Transport.

This commit is contained in:
Brent Simmons 2024-04-05 18:29:23 -07:00
parent ad151b5fa4
commit 0f80c932a1
1 changed files with 48 additions and 0 deletions

View File

@ -120,12 +120,18 @@ public protocol Transport {
func cancelAll()
/// Sends URLRequest and returns the HTTP headers and the data payload.
func send(request: URLRequest) async throws -> (HTTPURLResponse, Data?)
func send(request: URLRequest, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void)
/// Sends URLRequest that doesn't require any result information.
func send(request: URLRequest, method: String) async throws
func send(request: URLRequest, method: String, completion: @escaping (Result<Void, Error>) -> Void)
/// Sends URLRequest with a data payload and returns the HTTP headers and the data payload.
func send(request: URLRequest, method: String, payload: Data) async throws -> (HTTPURLResponse, Data?)
func send(request: URLRequest, method: String, payload: Data, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void)
}
@ -140,6 +146,20 @@ extension URLSession: Transport {
}
}
public func send(request: URLRequest) async throws -> (HTTPURLResponse, Data?) {
try await withCheckedThrowingContinuation { continuation in
self.send(request: request) { result in
switch result {
case .success(let (response, data)):
continuation.resume(returning: (response, data))
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
public func send(request: URLRequest, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) {
let task = self.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
@ -162,6 +182,20 @@ extension URLSession: Transport {
task.resume()
}
public func send(request: URLRequest, method: String) async throws {
try await withCheckedThrowingContinuation { continuation in
self.send(request: request, method: method) { result in
switch result {
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
public func send(request: URLRequest, method: String, completion: @escaping (Result<Void, Error>) -> Void) {
var sendRequest = request
@ -188,6 +222,20 @@ extension URLSession: Transport {
task.resume()
}
public func send(request: URLRequest, method: String, payload: Data) async throws -> (HTTPURLResponse, Data?) {
try await withCheckedThrowingContinuation { continuation in
self.send(request: request, method: method, payload: payload) { result in
switch result {
case .success(let (response, data)):
continuation.resume(returning: (response, data))
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
public func send(request: URLRequest, method: String, payload: Data, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) {
var sendRequest = request