Skip to content

Commit 0bf9ecb

Browse files
authored
Merge pull request #92 from futuredapp/feature/async-backport
Make async methods available on backportable systems
2 parents a8c9c19 + c3f3021 commit 0bf9ecb

4 files changed

Lines changed: 106 additions & 5 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Test – macOS
1+
name: macOS 10.15
22

33
on:
44
push:
@@ -10,7 +10,7 @@ on:
1010

1111
jobs:
1212
test:
13-
runs-on: macos-latest
13+
runs-on: macos-10.15
1414

1515
steps:
1616
- uses: actions/checkout@v2

.github/workflows/macos-11.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: macOS 11
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- '*'
10+
11+
jobs:
12+
test:
13+
runs-on: macos-11
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Setup Xcode version
18+
uses: maxim-lobanov/setup-xcode@v1.4.0
19+
with:
20+
xcode-version: 13.1
21+
- name: Lint
22+
run: |
23+
swiftlint --strict
24+
- name: Pod lib lint
25+
run: |
26+
gem install bundler
27+
bundle install --jobs 4 --retry 3
28+
bundle exec pod lib lint --allow-warnings
29+
- name: Swift build & test
30+
run: |
31+
swift build
32+
swift test

Sources/FTAPIKit/URLServer+Async.swift

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,76 @@
1-
#if swift(>=5.5)
21
import Foundation
32

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, *)
574
public extension URLServer {
675

776
/// Performs call to endpoint which does not return any data in the HTTP response.

Tests/FTAPIKitTests/AsyncTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Foundation
33
import XCTest
44

5-
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
5+
@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
66
final class AsyncTests: XCTestCase {
77
func testCallWithoutResponse() async throws {
88
let server = HTTPBinServer()

0 commit comments

Comments
 (0)