Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubenfer committed Dec 23, 2021
2 parents 11c057b + e5e318d commit 88eeab1
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 17 deletions.
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<[Value], Error>) -> Void)
func consumerPrices(startDate: Date, endDate: Date, geo: GEO) -> AnyPublisher<[Value], Error>
func consumerPrices(date: Date, geo: GEO) -> AnyPublisher<[Value], Error>
func consumerPrices(startDate: Date, endDate: Date, geo: GEO) async throws -> [Value]
func consumerPrices(date: Date, geo: GEO) async throws -> [Value]
```

### 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<[Value], Error>) -> Void)
func spotPrices(startDate: Date, endDate: Date, geo: GEO) -> AnyPublisher<[Value], Error>
func spotPrices(date: Date, geo: GEO) -> AnyPublisher<[Value], Error>
func spotPrices(startDate: Date, endDate: Date) async throws -> [Value]
func spotPrices(date: Date) async throws -> [Value]
```

## Licencia de uso y contribución con el proyecto
Expand Down
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()
}
}
}
7 changes: 7 additions & 0 deletions Sources/REESwift/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class Network {
}
}

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
64 changes: 48 additions & 16 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 @@ -67,3 +71,31 @@ public extension REESwift {
}

}

public extension REESwift {

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

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

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

func spotPrices(date: Date) async throws -> [Value] {
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 -> [Value] {
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
}

}
58 changes: 58 additions & 0 deletions Tests/REESwiftTests/REESwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,45 @@ final class REESwiftTests: XCTestCase {

// MARK: - Test consumer prices

func testConsumerPricesAsync() async {

let now = Date()

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)

}

func testConsumerPricesAsyncOtherGEOs() async {

let now = Date()

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 +124,25 @@ final class REESwiftTests: XCTestCase {

// MARK: - Test spot prices

func testSpotPricesAsync() async {

let now = Date()

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 88eeab1

Please sign in to comment.