Skip to content

Commit

Permalink
Create missing async function for URLSession
Browse files Browse the repository at this point in the history
  • Loading branch information
christophhagen committed Jan 14, 2023
1 parent 594dd51 commit ec7f8b9
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Sources/Clairvoyant/Extensions/URLSession+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation


#if canImport(FoundationNetworking)
extension URLSession {

func data(for request: URLRequest) async throws -> (Data, URLResponse) {
let result: Result<(Data, URLResponse), Error> = await withCheckedContinuation { continuation in
let task = self.dataTask(with: request) { data, response, error in
if let error {
continuation.resume(returning: .failure(error))
} else {
continuation.resume(returning: .success((data!, response!)))
}
}
task.resume()
}
switch result {
case .failure(let error):
throw error
case .success(let result):
return result
}
}
}
#endif

0 comments on commit ec7f8b9

Please sign in to comment.