Skip to content

Commit

Permalink
feat: iOS 13+ async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubenfer committed Dec 23, 2021
1 parent 8cdd890 commit e5e318d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 39 deletions.
17 changes: 17 additions & 0 deletions Sources/REESwift/Extensions/URLSession.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation

@available(iOS, deprecated: 15.0)
extension URLSession {
func data(from url: URL) async throws -> (Data, URLResponse) {
try await withCheckedThrowingContinuation { continuation in
let task = self.dataTask(with: url) { data, response, error in
guard let data = data, let response = response else {
let error = error ?? URLError(.badServerResponse)
return continuation.resume(throwing: error)
}
continuation.resume(returning: (data, response))
}
task.resume()
}
}
}
4 changes: 0 additions & 4 deletions Sources/REESwift/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ class Network {
}
}

@available(iOS 15.0, *)
@available(tvOS 15.0, *)
@available(watchOS 8.0, *)
@available(macOS 12, *)
func request<T: Decodable>(_ endpoint: Endpoint) async throws -> T {
let (data, response) = try await URLSession.shared.data(from: endpoint.url)
guard (response as? HTTPURLResponse)?.statusCode == 200 else { throw URLError(.badServerResponse) }
Expand Down
40 changes: 20 additions & 20 deletions Sources/REESwift/REESwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ public extension REESwift {
prices(id: "1001", startDate: date.start, endDate: date.end, geo: geo, completion: completion)
}

func consumerPrices(startDate: Date, endDate: Date, geo: GEO) -> AnyPublisher<[Value], Error> {
return prices(id: "1001", startDate: startDate, endDate: endDate, geo: geo)
}

func consumerPrices(date: Date, geo: GEO) -> AnyPublisher<[Value], Error> {
return prices(id: "1001", startDate: date.start, endDate: date.end, geo: geo)
}

func spotPrices(startDate: Date, endDate: Date, completion: @escaping (Result<[Value], Error>) -> Void) {
prices(id: "600", startDate: startDate, endDate: endDate, geo: nil, completion: completion)
}
Expand All @@ -33,14 +25,6 @@ public extension REESwift {
prices(id: "600", startDate: date.start, endDate: date.end, geo: nil, completion: completion)
}

func spotPrices(startDate: Date, endDate: Date) -> AnyPublisher<[Value], Error> {
return prices(id: "600", startDate: startDate, endDate: endDate, geo: nil)
}

func spotPrices(date: Date) -> AnyPublisher<[Value], Error> {
return prices(id: "600", startDate: date.start, endDate: date.end, geo: nil)
}

private func prices(id: String, startDate: Date, endDate: Date, geo: GEO?, completion: @escaping (Result<[Value], Error>) -> Void) {
let endpoint = Endpoint.prices(startDate: startDate, endDate: endDate, geo: geo)
Network.shared.request(endpoint) { (result: Result<APIResponse, Error>) in
Expand All @@ -55,6 +39,26 @@ public extension REESwift {
}
}

}

public extension REESwift {

func consumerPrices(startDate: Date, endDate: Date, geo: GEO) -> AnyPublisher<[Value], Error> {
return prices(id: "1001", startDate: startDate, endDate: endDate, geo: geo)
}

func consumerPrices(date: Date, geo: GEO) -> AnyPublisher<[Value], Error> {
return prices(id: "1001", startDate: date.start, endDate: date.end, geo: geo)
}

func spotPrices(startDate: Date, endDate: Date) -> AnyPublisher<[Value], Error> {
return prices(id: "600", startDate: startDate, endDate: endDate, geo: nil)
}

func spotPrices(date: Date) -> AnyPublisher<[Value], Error> {
return prices(id: "600", startDate: date.start, endDate: date.end, geo: nil)
}

private func prices(id: String, startDate: Date, endDate: Date, geo: GEO?) -> AnyPublisher<[Value], Error> {
let endpoint = Endpoint.prices(startDate: startDate, endDate: endDate, geo: geo)
return Network.shared.request(endpoint)
Expand All @@ -68,10 +72,6 @@ public extension REESwift {

}

@available(iOS 15.0, *)
@available(tvOS 15.0, *)
@available(watchOS 8.0, *)
@available(macOS 12, *)
public extension REESwift {

func consumerPrices(startDate: Date, endDate: Date, geo: GEO) async throws -> [Value] {
Expand Down
18 changes: 3 additions & 15 deletions Tests/REESwiftTests/REESwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ final class REESwiftTests: XCTestCase {

// MARK: - Test consumer prices

@available(iOS 15.0, *)
@available(tvOS 15.0, *)
@available(watchOS 8.0, *)
@available(macOS 12, *)
func testConsumerPricesAsync() async {

let now = Date.now
let now = Date()

let todayPrices = try? await ree.consumerPrices(date: now, geo: .peninsula)
XCTAssertNotNil(todayPrices)
Expand All @@ -31,13 +27,9 @@ final class REESwiftTests: XCTestCase {

}

@available(iOS 15.0, *)
@available(tvOS 15.0, *)
@available(watchOS 8.0, *)
@available(macOS 12, *)
func testConsumerPricesAsyncOtherGEOs() async {

let now = Date.now
let now = Date()

let peninsulaPrices = try? await ree.consumerPrices(date: now, geo: .peninsula)
let ceutaPrices = try? await ree.consumerPrices(date: now, geo: .ceuta)
Expand Down Expand Up @@ -132,13 +124,9 @@ final class REESwiftTests: XCTestCase {

// MARK: - Test spot prices

@available(iOS 15.0, *)
@available(tvOS 15.0, *)
@available(watchOS 8.0, *)
@available(macOS 12, *)
func testSpotPricesAsync() async {

let now = Date.now
let now = Date()

let todayPrices = try? await ree.spotPrices(date: now)
XCTAssertNotNil(todayPrices)
Expand Down

0 comments on commit e5e318d

Please sign in to comment.