Last active
December 24, 2023 11:07
-
-
Save aronbudinszky/66cdb71d734ae48a2609c7f2c094a02d to your computer and use it in GitHub Desktop.
An extension that provides async support for URLSession data function on Linux. See: https://medium.com/hoursofoperation/use-async-urlsession-with-server-side-swift-67821a64fa91?sk=bc142f8d3b6a444253eea660646aa822
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 | |
#if canImport(FoundationNetworking) | |
import FoundationNetworking | |
#endif | |
/// Defines the possible errors | |
public enum URLSessionAsyncErrors: Error { | |
case invalidUrlResponse, missingResponseData | |
} | |
/// An extension that provides async support for fetching a URL | |
/// | |
/// Needed because the Linux version of Swift does not support async URLSession yet. | |
public extension URLSession { | |
/// A reimplementation of `URLSession.shared.data(from: url)` required for Linux | |
/// | |
/// - Parameter url: The URL for which to load data. | |
/// - Returns: Data and response. | |
/// | |
/// - Usage: | |
/// | |
/// let (data, response) = try await URLSession.shared.asyncData(from: url) | |
func asyncData(from url: URL) async throws -> (Data, URLResponse) { | |
return try await withCheckedThrowingContinuation { continuation in | |
let task = self.dataTask(with: url) { data, response, error in | |
if let error = error { | |
continuation.resume(throwing: error) | |
return | |
} | |
guard let response = response as? HTTPURLResponse else { | |
continuation.resume(throwing: URLSessionAsyncErrors.invalidUrlResponse) | |
return | |
} | |
guard let data = data else { | |
continuation.resume(throwing: URLSessionAsyncErrors.missingResponseData) | |
return | |
} | |
continuation.resume(returning: (data, response)) | |
} | |
task.resume() | |
} | |
} | |
} |
One small error, I believe. You shouldn't use
URLSession.shared
as this is an extension onURLSession
itself and you may have different ones for different configurations (e.g. headers, auth, etc.)
Very good point, fixed!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One small error, I believe. You shouldn't use
URLSession.shared
as this is an extension onURLSession
itself and you may have different ones for different configurations (e.g. headers, auth, etc.)