-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create missing async function for URLSession
- Loading branch information
1 parent
594dd51
commit ec7f8b9
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
Sources/Clairvoyant/Extensions/URLSession+Extensions.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,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 |