Add async methods to Transport.

This commit is contained in:
Brent Simmons 2024-04-06 12:06:06 -07:00
parent cee961cfa5
commit e7abe3fa7a
1 changed files with 30 additions and 0 deletions

View File

@ -10,6 +10,20 @@ import Foundation
extension Transport { extension Transport {
public func send<R: Decodable>(request: URLRequest, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> (HTTPURLResponse, R?) {
try await withCheckedThrowingContinuation { continuation in
self.send(request: request, resultType: resultType, dateDecoding: dateDecoding, keyDecoding: keyDecoding) { result in
switch result {
case .success(let (response, decoded)):
continuation.resume(returning: (response, decoded))
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
/** /**
Sends an HTTP get and returns JSON object(s) Sends an HTTP get and returns JSON object(s)
*/ */
@ -121,6 +135,22 @@ extension Transport {
} }
} }
public func send<R: Decodable>(request: URLRequest, method: String, data: Data, resultType: R.Type, dateDecoding: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecoding: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> (HTTPURLResponse, R?) {
try await withCheckedThrowingContinuation { continuation in
self.send(request: request, method: method, data: data, resultType: resultType, dateDecoding: dateDecoding, keyDecoding: keyDecoding) { result in
switch result {
case .success(let (response, decoded)):
continuation.resume(returning: (response, decoded))
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
/** /**
Sends the specified HTTP method with a Raw payload and returns JSON object(s). Sends the specified HTTP method with a Raw payload and returns JSON object(s).
*/ */