Skip to content

Commit

Permalink
feat: add async alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubenfer committed Jun 18, 2021
1 parent 60224b1 commit fe34747
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.3
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func consumerPrices(startDate: Date, endDate: Date, geo: GEO, completion: @escap
func consumerPrices(date: Date, geo: GEO, completion: @escaping (Result<[PrecioLuzValue], Error>) -> Void)
func consumerPrices(startDate: Date, endDate: Date, geo: GEO) -> AnyPublisher<[PrecioLuzValue], Error>
func consumerPrices(date: Date, geo: GEO) -> AnyPublisher<[PrecioLuzValue], Error>
func consumerPrices(startDate: Date, endDate: Date, geo: GEO) async throws -> [PrecioLuzValue]
func consumerPrices(date: Date, geo: GEO) async throws -> [PrecioLuzValue]
```

### Obtener precios mercado spot
Expand All @@ -42,6 +44,8 @@ func spotPrices(startDate: Date, endDate: Date, geo: GEO, completion: @escaping
func spotPrices(date: Date, geo: GEO, completion: @escaping (Result<[PrecioLuzValue], Error>) -> Void)
func spotPrices(startDate: Date, endDate: Date, geo: GEO) -> AnyPublisher<[PrecioLuzValue], Error>
func spotPrices(date: Date, geo: GEO) -> AnyPublisher<[PrecioLuzValue], Error>
func spotPrices(startDate: Date, endDate: Date) async throws -> [PrecioLuzValue]
func spotPrices(date: Date) async throws -> [PrecioLuzValue]
```

## Licencia de uso y contribución con el proyecto
Expand Down
11 changes: 11 additions & 0 deletions Sources/REESwift/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ 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) }
let decodedData = try decoder.decode(T.self, from: data)
return decodedData
}

func request<T: Decodable>(_ endpoint: Endpoint) -> AnyPublisher<T, Error> {
URLSession.shared
.dataTaskPublisher(for: endpoint.url)
Expand Down
32 changes: 32 additions & 0 deletions Sources/REESwift/REESwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,35 @@ 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 -> [PrecioLuzValue] {
return try await prices(id: "1001", startDate: startDate, endDate: endDate, geo: geo)
}

func consumerPrices(date: Date, geo: GEO) async throws -> [PrecioLuzValue] {
return try await prices(id: "1001", startDate: date.start, endDate: date.end, geo: geo)
}

func spotPrices(startDate: Date, endDate: Date) async throws -> [PrecioLuzValue] {
return try await prices(id: "600", startDate: startDate, endDate: endDate, geo: nil)
}

func spotPrices(date: Date) async throws -> [PrecioLuzValue] {
return try await prices(id: "600", startDate: date.start, endDate: date.end, geo: nil)
}

private func prices(id: String, startDate: Date, endDate: Date, geo: GEO?) async throws -> [PrecioLuzValue] {
let endpoint = Endpoint.prices(startDate: startDate, endDate: endDate, geo: geo)
let apiResponse: APIResponse = try await Network.shared.request(endpoint)
let prices = apiResponse.included.first { $0.id == id }
guard let values = prices?.attributes?.values, !values.isEmpty else { throw URLError(.badServerResponse) }
return values
}

}
70 changes: 70 additions & 0 deletions Tests/REESwiftTests/REESwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,53 @@ 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 todayPrices = try? await ree.consumerPrices(date: now, geo: .peninsula)
XCTAssertNotNil(todayPrices)
XCTAssert(todayPrices?.count == 24)

let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: now)!
let otherPrices = try? await ree.consumerPrices(startDate: yesterday.start, endDate: now.end, geo: .peninsula)
XCTAssertNotNil(otherPrices)
XCTAssert(otherPrices?.count == 48)

let futureDate = Calendar.current.date(byAdding: .month, value: 1, to: now)!
let futurePrices = try? await ree.consumerPrices(date: futureDate, geo: .peninsula)
XCTAssertNil(futurePrices)

}

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

let now = Date.now

let peninsulaPrices = try? await ree.consumerPrices(date: now, geo: .peninsula)
let ceutaPrices = try? await ree.consumerPrices(date: now, geo: .ceuta)
XCTAssertNotNil(peninsulaPrices)
XCTAssertNotNil(ceutaPrices)
XCTAssert(peninsulaPrices?.count == ceutaPrices?.count)
var priceEquals = true
for i in 0..<peninsulaPrices!.count {
if peninsulaPrices![i].value != ceutaPrices![i].value {
priceEquals = false
break
}
}
XCTAssertFalse(priceEquals)

}

func testConsumerPricesCombine() {

let now = Date()
Expand Down Expand Up @@ -85,6 +132,29 @@ 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 todayPrices = try? await ree.spotPrices(date: now)
XCTAssertNotNil(todayPrices)
XCTAssert(todayPrices?.count == 24)

let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: now)!
let otherPrices = try? await ree.spotPrices(startDate: yesterday.start, endDate: now.end)
XCTAssertNotNil(otherPrices)
XCTAssert(otherPrices?.count == 48)

let futureDate = Calendar.current.date(byAdding: .month, value: 1, to: now)!
let futurePrices = try? await ree.spotPrices(date: futureDate)
XCTAssertNil(futurePrices)

}

func testSpotPricesCombine() {

let now = Date()
Expand Down

0 comments on commit fe34747

Please sign in to comment.