Skip to content

Commit

Permalink
Added async support (#11)
Browse files Browse the repository at this point in the history
* Added async support

* added availability checks & refactored async dataTask

* added missing task execution

* cleanup code

* update changelog

* updated podspec
  • Loading branch information
kaulex99 authored Jul 6, 2022
1 parent db268f1 commit 0ae6209
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 2.1.0
Added `async` concurrency support.

# 2.0.0

See [Migration Guide](Migration/V2_0_0.md).
See [Migration Guide](Migration/V2_0_0.md).
7 changes: 5 additions & 2 deletions Endpoints.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
Pod::Spec.new do |s|
s.name = "Endpoints"
s.version = "2.0.0"
s.version = "2.1.0"
s.license = { :type => "MIT", :file => "LICENSE" }
s.summary = "Type-Safe Swift Networking"
s.homepage = "https://github.com/tailoredmedia/Endpoints"
s.authors = { "Peter Weishapl" => "pw@tailored-apps.com",
"Robin Mayerhofer" => "rm@tailored-apps.com" }
"Robin Mayerhofer" => "rm@tailored-apps.com",
"Dominik Arnhof" => "da@tailored-apps.com",
"Alexander Kauer" => "ak@tailored-apps.com"
}
s.source = {
:git => "https://github.com/tailoredmedia/Endpoints.git",
:tag => s.version
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 Tailored Media GmbH
Copyright (c) 2022 Tailored Media GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 10 additions & 0 deletions Sources/Convenience/Session+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,14 @@ public extension Session {
tsk.resume()
return tsk
}

#if compiler(>=5.5) && canImport(_Concurrency)

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
func start<C: Call>(call: C) async throws -> Result<C.Parser.OutputType> {
return try await dataTask(for: call)
}

#endif

}
18 changes: 18 additions & 0 deletions Sources/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ public class Session<C: Client> {

return task
}

#if compiler(>=5.5) && canImport(_Concurrency)

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public func dataTask<C: Call>(for call: C) async throws -> Result<C.Parser.OutputType> {
return try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<Result<C.Parser.OutputType>, Error>) in
let task = dataTask(for: call, completion: { result in
result.onSuccess { _ in
continuation.resume(returning: result)
}.onError { error in
continuation.resume(throwing: error)
}
})
task.resume()
})
}

#endif

func transform<C: Call>(sessionResult: URLSessionTaskResult, for call: C) -> Result<C.Parser.OutputType> {
var result = Result<C.Parser.OutputType>(response: sessionResult.httpResponse)
Expand Down
9 changes: 9 additions & 0 deletions Tests/ClientTester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class ClientTester<C: Client> {
test.waitForExpectations(timeout: 30, handler: nil)
}

#if compiler(>=5.5) && canImport(_Concurrency)

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
func testAsync<C: Call>(call: C) async throws -> Result<C.Parser.OutputType> {
return try await session.start(call: call)
}

#endif

func assert<Output>(result: Result<Output>, isSuccess: Bool=true, status code: Int?=nil) {
if isSuccess {
XCTAssertNil(result.error)
Expand Down
22 changes: 22 additions & 0 deletions Tests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ class ClientTests: XCTestCase {
}
}

#if compiler(>=5.5) && canImport(_Concurrency)

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
func testStatusErrorAsync() async {
do {
let c = AnyCall<DataResponseParser>(Request(.get, "status/400"))
_ = try await tester.testAsync(call: c)
XCTFail("Should throw exception")
} catch {
XCTAssertEqual(error.localizedDescription, "bad request")
}
}

@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
func testGetDataAsync() async throws {
let c = AnyCall<DataResponseParser>(Request(.get, "get"))
let result = try await tester.testAsync(call: c)
tester.assert(result: result, isSuccess: true, status: 200)
}

#endif

func testGetData() {
let c = AnyCall<DataResponseParser>(Request(.get, "get"))

Expand Down

0 comments on commit 0ae6209

Please sign in to comment.