|
1 | | -#if swift(>=5.5) |
2 | 1 | import Foundation |
3 | 2 |
|
4 | | -@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) |
| 3 | +// This extension is duplicated to support Xcode 13.0 and Xcode 13.1, |
| 4 | +// where backported concurrency is not available. |
| 5 | + |
| 6 | +// Support of async-await for Xcode 13.2+. |
| 7 | +#if swift(>=5.5.2) |
| 8 | +@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) |
| 9 | +public extension URLServer { |
| 10 | + |
| 11 | + /// Performs call to endpoint which does not return any data in the HTTP response. |
| 12 | + /// - Note: This call maps ``call(endpoint:completion:)`` to the async/await API |
| 13 | + /// - Parameters: |
| 14 | + /// - endpoint: The endpoint |
| 15 | + /// - Throws: Throws in case that result is .failure |
| 16 | + /// - Returns: Void on success |
| 17 | + func call(endpoint: Endpoint) async throws { |
| 18 | + return try await withCheckedThrowingContinuation { continuation in |
| 19 | + call(endpoint: endpoint) { result in |
| 20 | + switch result { |
| 21 | + case .success: |
| 22 | + continuation.resume() |
| 23 | + case .failure(let error): |
| 24 | + continuation.resume(throwing: error) |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// Performs call to endpoint which returns an arbitrary data in the HTTP response, that should not be parsed by the decoder of the |
| 31 | + /// server. |
| 32 | + /// - Note: This call maps ``call(data:completion:)`` to the async/await API |
| 33 | + /// - Parameters: |
| 34 | + /// - endpoint: The endpoint |
| 35 | + /// - Throws: Throws in case that result is .failure |
| 36 | + /// - Returns: Plain data returned with the HTTP Response |
| 37 | + func call(data endpoint: Endpoint) async throws -> Data { |
| 38 | + return try await withCheckedThrowingContinuation { continuation in |
| 39 | + call(data: endpoint) { result in |
| 40 | + switch result { |
| 41 | + case .success(let data): |
| 42 | + continuation.resume(returning: data) |
| 43 | + case .failure(let error): |
| 44 | + continuation.resume(throwing: error) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /// Performs call to endpoint which returns data that are supposed to be parsed by the decoder of the instance |
| 51 | + /// conforming to ``Server`` in the HTTP response. |
| 52 | + /// - Note: This call maps ``call(response:completion:)`` to the async/await API |
| 53 | + /// - Parameters: |
| 54 | + /// - endpoint: The endpoint |
| 55 | + /// - Throws: Throws in case that result is .failure |
| 56 | + /// - Returns: Instance of the required type |
| 57 | + func call<EP: ResponseEndpoint>(response endpoint: EP) async throws -> EP.Response { |
| 58 | + return try await withCheckedThrowingContinuation { continuation in |
| 59 | + call(response: endpoint) { result in |
| 60 | + switch result { |
| 61 | + case .success(let response): |
| 62 | + continuation.resume(returning: response) |
| 63 | + case .failure(let error): |
| 64 | + continuation.resume(throwing: error) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// Support of async-await for Xcode 13 and 13.1. |
| 72 | +#elseif swift(>=5.5) |
| 73 | +@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *) |
5 | 74 | public extension URLServer { |
6 | 75 |
|
7 | 76 | /// Performs call to endpoint which does not return any data in the HTTP response. |
|
0 commit comments