DnsUpdater/Sources/App/Util/URLSession+Async.swift

28 lines
845 B
Swift
Raw Normal View History

2024-11-15 10:46:29 +01:00
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
extension URLSession {
func data(for request: URLRequest) async throws -> (Data, URLResponse) {
let result: Result<(response: URLResponse, data: Data), Error> = await withCheckedContinuation { continuation in
let task = dataTask(with: request) { data, response, error in
if let error {
continuation.resume(returning: .failure(error))
} else {
continuation.resume(returning: .success((response!, data!)))
}
}
task.resume()
}
switch result {
case .failure(let error):
throw error
case .success(let result):
return (result.data, result.response)
}
}
}
#endif