Skip to content

Commit 5aafb1d

Browse files
Merge pull request #108 from dbsystel/async-await
Adds support for asny await
2 parents c285f16 + 4aa2975 commit 5aafb1d

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.0
1+
// swift-tools-version:5.5
22
//
33
// Package.swift
44
//

Source/NetworkService+Async.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Lukas Schmidt on 19.12.21.
6+
//
7+
8+
import Foundation
9+
10+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
11+
public extension NetworkService {
12+
13+
/**
14+
Fetches a resource asynchronously from remote location. Execution of the requests starts immediately.
15+
16+
**Example**:
17+
```swift
18+
let networkService: NetworkService = //
19+
let resource: Resource<String> = //
20+
21+
let (result, response) = try await networkService.request(resource)
22+
```
23+
24+
- parameter resource: The resource you want to fetch.
25+
26+
- returns: a touple containing the parsed result and the HTTP response
27+
- Throws: A `NetworkError`
28+
*/
29+
@discardableResult
30+
func request<Result>(_ resource: Resource<Result>) async throws -> (Result, HTTPURLResponse) {
31+
return try await withCheckedThrowingContinuation({ coninuation in
32+
request(resource: resource, onCompletionWithResponse: {
33+
coninuation.resume(with: $0)
34+
})
35+
})
36+
}
37+
38+
/**
39+
Fetches a resource asynchronously from remote location. Execution of the requests starts immediately.
40+
41+
**Example**:
42+
```swift
43+
let networkService: NetworkService = //
44+
let resource: ResourceWithError<String, CustomError> = //
45+
46+
let (result, response) = try await networkService.request(resource)
47+
```
48+
49+
- parameter resource: The resource you want to fetch.
50+
51+
- returns: a touple containing the parsed result and the HTTP response
52+
- Throws: Custom Error provided by ResourceWithError
53+
*/
54+
@discardableResult
55+
func request<Result, E: Error>(_ resource: ResourceWithError<Result, E>) async throws -> (Result, HTTPURLResponse) {
56+
return try await withCheckedThrowingContinuation({ coninuation in
57+
request(resource: resource, onCompletionWithResponse: {
58+
coninuation.resume(with: $0)
59+
})
60+
})
61+
}
62+
63+
}

Tests/NetworkServiceTest.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,34 @@ class NetworkServiceTest: XCTestCase {
238238
}
239239
XCTAssertEqual(networkAccess.request?.url?.absoluteString, "https://bahn.de/train")
240240
}
241+
242+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
243+
func testGIVEN_aRequest_WHEN_requestWithAsyncResultAndResponse_THEN_ShouldRespond() async throws {
244+
// GIVEN
245+
networkAccess.changeMock(data: Train.validJSONData, response: .defaultMock, error: nil)
246+
247+
//When
248+
let (result, response) = try await networkService.request(resource)
249+
250+
251+
//Then
252+
XCTAssertEqual(result.name, self.trainName)
253+
XCTAssertEqual(response, .defaultMock)
254+
XCTAssertEqual(networkAccess.request?.url?.absoluteString, "https://bahn.de/train")
255+
}
256+
257+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
258+
func testGIVEN_aRequest_WHEN_requestWithAsyncResultAndResponse_THEN_ShouldThwo() async {
259+
// GIVEN
260+
let error = NSError(domain: "", code: 0, userInfo: nil)
261+
networkAccess.changeMock(data: nil, response: nil, error: error)
262+
263+
//When
264+
do {
265+
try await networkService.request(resource)
266+
XCTFail("Schould throw")
267+
} catch let error {
268+
XCTAssertTrue(error is NetworkError)
269+
}
270+
}
241271
}

Tests/NetworkServiceWithErrorTest.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,34 @@ class NetworkServiceWithErrorTest: XCTestCase {
164164
}
165165
XCTAssertEqual(networkAccess.request?.url?.absoluteString, "https://bahn.de/train")
166166
}
167+
168+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
169+
func testGIVEN_aRequest_WHEN_requestWithAsyncResultAndResponse_THEN_ShouldRespond() async throws {
170+
// GIVEN
171+
networkAccess.changeMock(data: Train.validJSONData, response: .defaultMock, error: nil)
172+
173+
//When
174+
let (result, response) = try await networkService.request(resource)
175+
176+
177+
//Then
178+
XCTAssertEqual(result.name, self.trainName)
179+
XCTAssertEqual(response, .defaultMock)
180+
XCTAssertEqual(networkAccess.request?.url?.absoluteString, "https://bahn.de/train")
181+
}
182+
183+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
184+
func testGIVEN_aRequest_WHEN_requestWithAsyncResultAndResponse_THEN_ShouldThwo() async {
185+
// GIVEN
186+
let error = NSError(domain: "", code: 0, userInfo: nil)
187+
networkAccess.changeMock(data: nil, response: nil, error: error)
188+
189+
//When
190+
do {
191+
try await networkService.request(resource)
192+
XCTFail("Schould throw")
193+
} catch let error {
194+
XCTAssertTrue(error is CustomError)
195+
}
196+
}
167197
}

0 commit comments

Comments
 (0)