Last active
December 14, 2021 15:33
-
-
Save jayrhynas/7d624f1e5e3bdb5ad17dd4ab6005b2e0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Foundation | |
extension URLSession { | |
private func convert<Value>(_ work: (@escaping (Value?, URLResponse?, Error?) -> Void) -> Void) async throws -> (Value, URLResponse) { | |
try await withCheckedThrowingContinuation { continuation in | |
work { value, response, error in | |
continuation.resume(with: Result { | |
guard let value = value, let response = response else { | |
throw error ?? URLError(.badServerResponse) | |
} | |
return (value, response) | |
}) | |
} | |
} | |
} | |
@available(iOS, deprecated: 15.0, message: "Use the built-in API instead.") | |
func data(from url: URL) async throws -> (Data, URLResponse) { | |
try await convert { completion in | |
self.dataTask(with: url, completionHandler: completion).resume() | |
} | |
} | |
@available(iOS, deprecated: 15.0, message: "Use the built-in API instead.") | |
func data(for request: URLRequest) async throws -> (Data, URLResponse) { | |
try await convert { completion in | |
self.dataTask(with: request, completionHandler: completion).resume() | |
} | |
} | |
@available(iOS, deprecated: 15.0, message: "Use the built-in API instead.") | |
func upload(for request: URLRequest, fromFile fileURL: URL) async throws -> (Data, URLResponse) { | |
try await convert { completion in | |
self.uploadTask(with: request, fromFile: fileURL, completionHandler: completion).resume() | |
} | |
} | |
@available(iOS, deprecated: 15.0, message: "Use the built-in API instead.") | |
func upload(for request: URLRequest, from bodyData: Data) async throws -> (Data, URLResponse) { | |
try await convert { completion in | |
self.uploadTask(with: request, from: bodyData, completionHandler: completion).resume() | |
} | |
} | |
@available(iOS, deprecated: 15.0, message: "Use the built-in API instead.") | |
func download(for request: URLRequest) async throws -> (URL, URLResponse) { | |
try await convert { completion in | |
self.downloadTask(with: request, completionHandler: completion).resume() | |
} | |
} | |
@available(iOS, deprecated: 15.0, message: "Use the built-in API instead.") | |
func download(from url: URL) async throws -> (URL, URLResponse) { | |
try await convert { completion in | |
self.downloadTask(with: url, completionHandler: completion).resume() | |
} | |
} | |
@available(iOS, deprecated: 15.0, message: "Use the built-in API instead.") | |
func download(resumeFrom resumeData: Data) async throws -> (URL, URLResponse) { | |
try await convert { completion in | |
self.downloadTask(withResumeData: resumeData, completionHandler: completion).resume() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment