Skip to content

Commit 4aa2975

Browse files
committed
Async await for ResourceWithError
1 parent 48a8fab commit 4aa2975

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

Source/NetworkService+Async.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,29 @@ public extension NetworkService {
3535
})
3636
}
3737

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+
3863
}

Tests/NetworkServiceTest.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,7 @@ class NetworkServiceTest: XCTestCase {
239239
XCTAssertEqual(networkAccess.request?.url?.absoluteString, "https://bahn.de/train")
240240
}
241241

242-
@available(watchOS 6.0, *)
243-
@available(macOS 10.15.0, *)
244-
@available(iOS 13.0.0, *)
242+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
245243
func testGIVEN_aRequest_WHEN_requestWithAsyncResultAndResponse_THEN_ShouldRespond() async throws {
246244
// GIVEN
247245
networkAccess.changeMock(data: Train.validJSONData, response: .defaultMock, error: nil)
@@ -256,9 +254,7 @@ class NetworkServiceTest: XCTestCase {
256254
XCTAssertEqual(networkAccess.request?.url?.absoluteString, "https://bahn.de/train")
257255
}
258256

259-
@available(watchOS 6.0, *)
260-
@available(macOS 10.15.0, *)
261-
@available(iOS 13.0.0, *)
257+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
262258
func testGIVEN_aRequest_WHEN_requestWithAsyncResultAndResponse_THEN_ShouldThwo() async {
263259
// GIVEN
264260
let error = NSError(domain: "", code: 0, userInfo: nil)

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)