From 55229d94730a1b45eb51215357823c2e812e48af Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 9 Nov 2021 19:46:11 +0100 Subject: [PATCH] Initial commit --- .gitignore | 7 ++++ Package.swift | 20 +++++++++++ README.md | 3 ++ Sources/AsyncBackports/Backport.swift | 4 +++ .../AsyncBackports/URLSession+Backport.swift | 34 +++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 .gitignore create mode 100644 Package.swift create mode 100644 README.md create mode 100644 Sources/AsyncBackports/Backport.swift create mode 100644 Sources/AsyncBackports/URLSession+Backport.swift diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bb460e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..8afa933 --- /dev/null +++ b/Package.swift @@ -0,0 +1,20 @@ +// swift-tools-version:5.5 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "AsyncBackports", + platforms: [ + .macOS(.v12), + .iOS(.v15), + .watchOS(.v8), + .tvOS(.v15), + ], + products: [ + .library(name: "AsyncBackports", targets: ["AsyncBackports"]), + ], + targets: [ + .target(name: "AsyncBackports", dependencies: []), + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..62602a2 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# AsyncBackports + +A description of this package. diff --git a/Sources/AsyncBackports/Backport.swift b/Sources/AsyncBackports/Backport.swift new file mode 100644 index 0000000..65519e4 --- /dev/null +++ b/Sources/AsyncBackports/Backport.swift @@ -0,0 +1,4 @@ +public struct Backport { + let original: Original +} + diff --git a/Sources/AsyncBackports/URLSession+Backport.swift b/Sources/AsyncBackports/URLSession+Backport.swift new file mode 100644 index 0000000..2b3f464 --- /dev/null +++ b/Sources/AsyncBackports/URLSession+Backport.swift @@ -0,0 +1,34 @@ +import Foundation + +public extension URLSession { + var backport: Backport { .init(original: self) } +} + +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. + /// - Parameter delegate: Task-specific delegate. + /// - Returns: Data and response. + @available(macOS, deprecated: 12.0, message: "No need for .backport any more") + @available(iOS, deprecated: 15.0, message: "No need for .backport any more") + @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) + } + + /// Convenience method to load data using an URL, creates and resumes an URLSessionDataTask internally. + /// + /// - Parameter url: The URL for which to load data. + /// - Parameter delegate: Task-specific delegate. + /// - Returns: Data and response. + @available(macOS, deprecated: 12.0, message: "No need for .backport any more") + @available(iOS, deprecated: 15.0, message: "No need for .backport any more") + @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) + } +}