From 37da482bd148ad7b2f0ea9938ed3d1798b897e79 Mon Sep 17 00:00:00 2001 From: Aleksei Tiurnin Date: Wed, 20 Dec 2023 17:54:27 +0300 Subject: [PATCH] update for min iOS 13 - remove URLSession extension - remove AsyncAwaitHelper - remove ProgressWrapper - remove AsyncAwaitHelper tests --- .../Helpers/AsyncAwaitHelper.swift | 24 ------ .../Helpers/ProgressWrapper.swift | 39 --------- .../Helpers/URLSession+Concurrency.swift | 83 ------------------- .../AsyncAwaitHelperTests.swift | 40 --------- 4 files changed, 186 deletions(-) delete mode 100644 Sources/ApexyURLSession/Helpers/AsyncAwaitHelper.swift delete mode 100644 Sources/ApexyURLSession/Helpers/ProgressWrapper.swift delete mode 100644 Sources/ApexyURLSession/Helpers/URLSession+Concurrency.swift delete mode 100644 Tests/ApexyURLSessionTests/AsyncAwaitHelperTests.swift diff --git a/Sources/ApexyURLSession/Helpers/AsyncAwaitHelper.swift b/Sources/ApexyURLSession/Helpers/AsyncAwaitHelper.swift deleted file mode 100644 index 71211ba..0000000 --- a/Sources/ApexyURLSession/Helpers/AsyncAwaitHelper.swift +++ /dev/null @@ -1,24 +0,0 @@ -import Foundation - -@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) -enum AsyncAwaitHelper { - - enum AsyncError: Error, Equatable { - case cancelledBeforeStart - } - - public typealias ContentContinuation = CheckedContinuation - - public static func adaptToAsync(dataTaskClosure: (ContentContinuation) -> Progress) async throws -> T { - let progressWrapper = ProgressWrapper() - return try await withTaskCancellationHandler(handler: { - progressWrapper.cancel() - }, operation: { - try Task.checkCancellation() - return try await withCheckedThrowingContinuation { (continuation: ContentContinuation) in - let progress = dataTaskClosure(continuation) - progressWrapper.progress = progress - } - }) - } -} diff --git a/Sources/ApexyURLSession/Helpers/ProgressWrapper.swift b/Sources/ApexyURLSession/Helpers/ProgressWrapper.swift deleted file mode 100644 index bcf8b12..0000000 --- a/Sources/ApexyURLSession/Helpers/ProgressWrapper.swift +++ /dev/null @@ -1,39 +0,0 @@ -import Foundation - -@propertyWrapper struct Locked { - - var wrappedValue: T { - get { - lock.lock() - defer { lock.unlock() } - return _value - } - set { - lock.lock() - defer { lock.unlock() } - _value = newValue - } - } - - private var _value: T - private let lock = NSLock() - - init(wrappedValue: T) { - self._value = wrappedValue - } -} - -@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) -final class ProgressWrapper { - - @Locked - var progress: Progress? - - init(_progress: Progress? = nil) { - self.progress = progress - } - - func cancel() { - progress?.cancel() - } -} diff --git a/Sources/ApexyURLSession/Helpers/URLSession+Concurrency.swift b/Sources/ApexyURLSession/Helpers/URLSession+Concurrency.swift deleted file mode 100644 index dcce12d..0000000 --- a/Sources/ApexyURLSession/Helpers/URLSession+Concurrency.swift +++ /dev/null @@ -1,83 +0,0 @@ -// -// URLSession+Concurrency.swift -// ApexyURLSession -// -// Created by Aleksei Tiurnin on 20.01.2022. -// - -import Apexy -import Foundation - -@available(macOS, introduced: 10.15, deprecated: 12, message: "Extension is no longer necessary. Use API built into SDK") -@available(iOS, introduced: 13, deprecated: 15, message: "Extension is no longer necessary. Use API built into SDK") -@available(watchOS, introduced: 6, deprecated: 8, message: "Extension is no longer necessary. Use API built into SDK") -@available(tvOS, introduced: 13, deprecated: 15, message: "Extension is no longer necessary. Use API built into SDK") -extension URLSession { - - /// Send request - /// - Parameters: - /// - request: A URL request object that provides the URL, cache policy, request type, and so on. The body stream and body data in this request object are ignored. - /// - delegate: Delegate to get events about request (NOT WORKINGđź’€) - /// - Returns: Tuple with Data and URLResponse - public func data( - forRequest request: URLRequest, - delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse) { - return try await AsyncAwaitHelper.adaptToAsync(dataTaskClosure: { continuation in - let task = dataTask(with: request) { data, response, error in - guard let data = data, let response = response else { - let error = error ?? URLError(.badServerResponse) - return continuation.resume(throwing: error) - } - continuation.resume(returning: (data, response)) - } - task.resume() - return task.progress - }) - } - - /// Send request - /// - Parameters: - /// - request: A URL request object that provides the URL, cache policy, request type, and so on. The body stream and body data in this request object are ignored. - /// - fromFile: The URL of the file to upload. - /// - delegate: Delegate to get events about request (NOT WORKINGđź’€) - /// - Returns: Tuple with Data and URLResponse - public func upload( - for request: URLRequest, - fromFile fileURL: URL, - delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse) { - return try await AsyncAwaitHelper.adaptToAsync(dataTaskClosure: { continuation in - let task = uploadTask(with: request, fromFile: fileURL) { data, response, error in - guard let data = data, let response = response else { - let error = error ?? URLError(.badServerResponse) - return continuation.resume(throwing: error) - } - continuation.resume(returning: (data, response)) - } - task.resume() - return task.progress - }) - } - - /// Send request - /// - Parameters: - /// - request: A URL request object that provides the URL, cache policy, request type, and so on. The body stream and body data in this request object are ignored. - /// - bodyData: The body data for the request. - /// - delegate: Delegate to get events about request (NOT WORKINGđź’€) - /// - Returns: Tuple with Data and URLResponse - public func upload( - for request: URLRequest, - from bodyData: Data, - delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, URLResponse) { - return try await AsyncAwaitHelper.adaptToAsync(dataTaskClosure: { continuation in - let task = uploadTask(with: request, from: bodyData) { data, response, error in - guard let data = data, let response = response else { - let error = error ?? URLError(.badServerResponse) - return continuation.resume(throwing: error) - } - continuation.resume(returning: (data, response)) - } - task.resume() - return task.progress - }) - } -} diff --git a/Tests/ApexyURLSessionTests/AsyncAwaitHelperTests.swift b/Tests/ApexyURLSessionTests/AsyncAwaitHelperTests.swift deleted file mode 100644 index 0682178..0000000 --- a/Tests/ApexyURLSessionTests/AsyncAwaitHelperTests.swift +++ /dev/null @@ -1,40 +0,0 @@ -// -// AsyncAwitHelperTests.swift -// -// -// Created by Aleksei Tiurnin on 31.10.2022. -// - -import XCTest -@testable import ApexyURLSession - -final class AsyncAwaitHelperTests: XCTestCase { - - func testExample() async throws { - let task = Task(priority: .background) { - try await AsyncAwaitHelper.adaptToAsync(dataTaskClosure: { continuation in - continuation.resume(returning: "123") - return Progress() - }) - } - let value = try await task.value - XCTAssertEqual(value, "123") - } - - func testCancelExample() async throws { - let task = Task(priority: .background) { - try await AsyncAwaitHelper.adaptToAsync(dataTaskClosure: { continuation in - continuation.resume(returning: "123") - return Progress() - }) - } - task.cancel() - do { - _ = try await task.value - XCTFail("Task has been cancelled") - } catch { - XCTAssert(error is CancellationError) - } - } - -}