From 6d2751d32632a90346202f581cc761d2d6d204fa Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 9 Nov 2021 20:46:04 +0100 Subject: [PATCH] Implement backports --- Package.swift | 3 ++- Sources/AsyncBackports/Backport.swift | 1 - .../AsyncBackports/URLSession+Backport.swift | 25 ++++++++++++++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index 8afa933..5d3b22d 100644 --- a/Package.swift +++ b/Package.swift @@ -5,7 +5,8 @@ import PackageDescription let package = Package( name: "AsyncBackports", - platforms: [ + platforms: [ + // TODO: Xcode 13.2 set targets where backport available .macOS(.v12), .iOS(.v15), .watchOS(.v8), diff --git a/Sources/AsyncBackports/Backport.swift b/Sources/AsyncBackports/Backport.swift index 65519e4..fe18891 100644 --- a/Sources/AsyncBackports/Backport.swift +++ b/Sources/AsyncBackports/Backport.swift @@ -1,4 +1,3 @@ public struct Backport { let original: Original } - diff --git a/Sources/AsyncBackports/URLSession+Backport.swift b/Sources/AsyncBackports/URLSession+Backport.swift index 2b3f464..0c7becd 100644 --- a/Sources/AsyncBackports/URLSession+Backport.swift +++ b/Sources/AsyncBackports/URLSession+Backport.swift @@ -5,7 +5,6 @@ public extension URLSession { } public extension Backport where Original: URLSession { - /// Convenience method to load data using an URLRequest, creates and resumes an URLSessionDataTask internally. /// /// - Parameter request: The URLRequest for which to load data. @@ -16,7 +15,23 @@ public extension Backport where Original: URLSession { @available(tvOS, deprecated: 15.0, message: "No need for .backport any more") @available(watchOS, deprecated: 8.0, message: "No need for .backport any more") func data(for request: URLRequest, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse) { - return try await original.data(for: request, delegate: delegate) + if #available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) { + return try await original.data(for: request, delegate: delegate) + } else { + return try await withUnsafeThrowingContinuation { continuation in + let task = URLSession.shared.dataTask(with: request) { data, response, error in + if let data = data, let response = response { + continuation.resume(returning: (data, response)) + } else if let error = error { + continuation.resume(throwing: error) + } else { + fatalError("Neither response nor error") + } + } + task.delegate = delegate + task.resume() + } + } } /// Convenience method to load data using an URL, creates and resumes an URLSessionDataTask internally. @@ -29,6 +44,10 @@ public extension Backport where Original: URLSession { @available(tvOS, deprecated: 15.0, message: "No need for .backport any more") @available(watchOS, deprecated: 8.0, message: "No need for .backport any more") func data(from url: URL, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse) { - return try await original.data(from: url, delegate: delegate) + if #available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) { + return try await original.data(from: url, delegate: delegate) + } else { + return try await data(for: URLRequest(url: url), delegate: delegate) + } } }