Skip to content

Commit

Permalink
Merge pull request #90 from 417-72KI/async-await-void
Browse files Browse the repository at this point in the history
`async/await` Void function
  • Loading branch information
pietbrauer authored Sep 6, 2022
2 parents 9eb9354 + 7d0f035 commit 8b0258e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 20 deletions.
67 changes: 47 additions & 20 deletions Sources/RequestKit/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public protocol Router {
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func load<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
func request() -> URLRequest?

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func load<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType _: T.Type) async throws -> T
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func load<T: Codable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func load(_ session: RequestKitURLSession) async throws
#endif
}

public extension Router {
Expand Down Expand Up @@ -194,8 +203,34 @@ public extension Router {
return task
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func load(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ error: Error?) -> Void) -> URLSessionDataTaskProtocol? {
guard let request = request() else {
return nil
}

let task = session.dataTask(with: request) { data, response, err in
if let response = response as? HTTPURLResponse {
if response.wasSuccessful == false {
var userInfo = [String: Any]()
if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
userInfo[RequestKitErrorKey] = json as Any?
}
let error = NSError(domain: self.configuration.errorDomain, code: response.statusCode, userInfo: userInfo)
completion(error)
return
}
}

completion(err)
}
task.resume()
return task
}
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
public extension Router {
func load<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T {
guard let request = request() else {
throw NSError(domain: configuration.errorDomain, code: -876, userInfo: nil)
Expand All @@ -216,40 +251,32 @@ public extension Router {
return try decoder.decode(T.self, from: responseTuple.0)
}

@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func load<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T {
let decoder = JSONDecoder()
if let dateDecodingStrategy = dateDecodingStrategy {
decoder.dateDecodingStrategy = dateDecodingStrategy
}
return try await load(session, decoder: decoder, expectedResultType: expectedResultType)
}
#endif

func load(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ error: Error?) -> Void) -> URLSessionDataTaskProtocol? {
func load(_ session: RequestKitURLSession = URLSession.shared) async throws {
guard let request = request() else {
return nil
throw NSError(domain: configuration.errorDomain, code: -876, userInfo: nil)
}

let task = session.dataTask(with: request) { data, response, err in
if let response = response as? HTTPURLResponse {
if response.wasSuccessful == false {
var userInfo = [String: Any]()
if let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
userInfo[RequestKitErrorKey] = json as Any?
}
let error = NSError(domain: self.configuration.errorDomain, code: response.statusCode, userInfo: userInfo)
completion(error)
return
let responseTuple = try await session.data(for: request, delegate: nil)
if let response = responseTuple.1 as? HTTPURLResponse {
if response.wasSuccessful == false {
var userInfo = [String: Any]()
if let json = try? JSONSerialization.jsonObject(with: responseTuple.0, options: .mutableContainers) as? [String: Any] {
userInfo[RequestKitErrorKey] = json as Any?
}
throw NSError(domain: configuration.errorDomain, code: response.statusCode, userInfo: userInfo)
}

completion(err)
}
task.resume()
return task
}
}
#endif

private extension CharacterSet {
// https://github.com/Alamofire/Alamofire/blob/3.5rameterEncoding.swift#L220-L225
Expand Down
17 changes: 17 additions & 0 deletions Tests/RequestKitTests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ class RouterTests: XCTestCase {
XCTAssertTrue(session.wasCalled)
XCTAssertTrue(receivedFailureResponse)
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func testEmptyResponseWithLoadAndIgnoreResponseBodyAsync() async throws {
let session = RequestKitURLTestSession(expectedURL: "https://example.com/some_route", expectedHTTPMethod: "POST", response: "", statusCode: 204)

var receivedSuccessResponse = false
do {
_ = try await TestInterface().loadAndIgnoreResponseBody(session)
receivedSuccessResponse = true
} catch {
XCTFail("should not catch any error")
}
XCTAssertTrue(session.wasCalled)
XCTAssertTrue(receivedSuccessResponse)
}
#endif
}

enum TestRouter: Router {
Expand Down
8 changes: 8 additions & 0 deletions Tests/RequestKitTests/TestInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ class TestInterface {
}
}
}

#if compiler(>=5.5.2) && canImport(_Concurrency)
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func loadAndIgnoreResponseBody(_ session: RequestKitURLSession) async throws {
let router = JSONTestRouter.testPOST(configuration)
return try await router.load(session)
}
#endif
}

enum JSONTestRouter: JSONPostRouter {
Expand Down

0 comments on commit 8b0258e

Please sign in to comment.