diff --git a/Sources/TibberSwift/Entities/PriceInfoToday.swift b/Sources/TibberSwift/Entities/PriceInfoToday.swift new file mode 100644 index 0000000..a42176d --- /dev/null +++ b/Sources/TibberSwift/Entities/PriceInfoToday.swift @@ -0,0 +1,23 @@ +import Foundation + +/// The structure of the user's price info today +public struct PriceInfoToday: Codable { + public struct Home: Codable, Identifiable { + public struct CurrentSubscription: Codable { + public struct PriceInfo: Codable { + public struct PriceInfoElement: Codable { + public let total: Double + public let energy: Double + public let tax: Double + public let startsAt: Date + public let currency: String + } + public let today: [PriceInfoElement] + } + public let priceInfo: PriceInfo + } + public let currentSubscription: CurrentSubscription + public let id: UUID + } + public let homes: [Home] +} diff --git a/Sources/TibberSwift/Operations/PriceInfoTodayOperation.swift b/Sources/TibberSwift/Operations/PriceInfoTodayOperation.swift new file mode 100644 index 0000000..0b22a39 --- /dev/null +++ b/Sources/TibberSwift/Operations/PriceInfoTodayOperation.swift @@ -0,0 +1,10 @@ +import Foundation + +public extension GraphQLOperation where Input == EmptyInput, Output == PriceInfoToday { + + /// Fetches the logged in user's price info of today + /// - Returns: GraphQL Operation for homes + static func priceInfoToday() throws -> Self { + try GraphQLOperation(input: EmptyInput(), queryFilename: "PriceInfoToday") + } +} diff --git a/Sources/TibberSwift/Queries/PriceInfoToday.graphql b/Sources/TibberSwift/Queries/PriceInfoToday.graphql new file mode 100644 index 0000000..cb1f76f --- /dev/null +++ b/Sources/TibberSwift/Queries/PriceInfoToday.graphql @@ -0,0 +1,18 @@ +{ + viewer { + homes { + id + currentSubscription { + priceInfo { + today { + total + energy + tax + startsAt + currency + } + } + } + } + } +} diff --git a/Sources/TibberSwift/TibberSwift.swift b/Sources/TibberSwift/TibberSwift.swift index 846e9df..15a54a9 100644 --- a/Sources/TibberSwift/TibberSwift.swift +++ b/Sources/TibberSwift/TibberSwift.swift @@ -54,6 +54,13 @@ public extension TibberSwift { let operation = try GraphQLOperation.consumption(resolution: .hourly, last: 100) return try await performOperation(operation) } + + /// Fetches the user's price information of today + /// - Returns: A list of all price information + func priceInfoToday() async throws -> PriceInfoToday { + let operation = try GraphQLOperation.priceInfoToday() + return try await performOperation(operation) + } /// Custom GraphQL operation /// - Parameter operation: The operation in question diff --git a/Tests/TibberSwiftTests/TibberSwiftTests.swift b/Tests/TibberSwiftTests/TibberSwiftTests.swift index c70e182..5bc4f38 100644 --- a/Tests/TibberSwiftTests/TibberSwiftTests.swift +++ b/Tests/TibberSwiftTests/TibberSwiftTests.swift @@ -150,4 +150,26 @@ final class TibberSwiftTests: XCTestCase { "Authorization": "consumptionKey", "User-Agent": "TibberSwift"]) } + + // MARK: - Test PriceInfoToday + + func testPriceInfoToday() async throws { + // Given + let data = TestDataManager.getData(forFile: "json/PriceInfoToday.json")! + urlSessionMock.dataForReturnValue = (data, TestDataManager.dummyResponse()) + + let sut = TibberSwift(apiKey: "priceInfoTodayKey", urlSession: urlSessionMock) + + // When + let result = try await sut.priceInfoToday() + + // Then + XCTAssertEqual(result.homes.first?.id, UUID(uuidString: "f2a876f3-e09e-4bba-af19-79b038893c3d")) + XCTAssert(result.homes.first?.currentSubscription.priceInfo.today.isEmpty == false) + XCTAssertEqual(result.homes.first?.currentSubscription.priceInfo.today.first?.total, 0.256) + XCTAssertEqual(result.homes.first?.currentSubscription.priceInfo.today.first?.energy, 0.0789) + XCTAssertEqual(result.homes.first?.currentSubscription.priceInfo.today.first?.tax, 0.1771) + XCTAssertEqual(result.homes.first?.currentSubscription.priceInfo.today.first?.startsAt.timeIntervalSinceReferenceDate, 745970400.0) + XCTAssertEqual(result.homes.first?.currentSubscription.priceInfo.today.first?.currency, "EUR") + } } diff --git a/Tests/TibberSwiftTests/json/PriceInfoToday.json b/Tests/TibberSwiftTests/json/PriceInfoToday.json new file mode 100644 index 0000000..e2b5a2c --- /dev/null +++ b/Tests/TibberSwiftTests/json/PriceInfoToday.json @@ -0,0 +1,185 @@ +{ + "data": { + "viewer": { + "homes": [ + { + "id": "f2a876f3-e09e-4bba-af19-79b038893c3d", + "currentSubscription": { + "priceInfo": { + "today": [ + { + "total": 0.256, + "energy": 0.0789, + "tax": 0.1771, + "startsAt": "2024-08-22T00:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.249, + "energy": 0.073, + "tax": 0.176, + "startsAt": "2024-08-22T01:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2578, + "energy": 0.0805, + "tax": 0.1773, + "startsAt": "2024-08-22T02:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2575, + "energy": 0.0802, + "tax": 0.1773, + "startsAt": "2024-08-22T03:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2457, + "energy": 0.0702, + "tax": 0.1755, + "startsAt": "2024-08-22T04:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2637, + "energy": 0.0854, + "tax": 0.1783, + "startsAt": "2024-08-22T05:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.281, + "energy": 0.1, + "tax": 0.181, + "startsAt": "2024-08-22T06:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.3161, + "energy": 0.1295, + "tax": 0.1866, + "startsAt": "2024-08-22T07:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2966, + "energy": 0.113, + "tax": 0.1836, + "startsAt": "2024-08-22T08:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2491, + "energy": 0.0731, + "tax": 0.176, + "startsAt": "2024-08-22T09:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.1797, + "energy": 0.0148, + "tax": 0.1649, + "startsAt": "2024-08-22T10:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.1621, + "energy": 0, + "tax": 0.1621, + "startsAt": "2024-08-22T11:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.1616, + "energy": -0.0004, + "tax": 0.162, + "startsAt": "2024-08-22T12:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.159, + "energy": -0.0026, + "tax": 0.1616, + "startsAt": "2024-08-22T13:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.1603, + "energy": -0.0015, + "tax": 0.1618, + "startsAt": "2024-08-22T14:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.1569, + "energy": -0.0044, + "tax": 0.1613, + "startsAt": "2024-08-22T15:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.162, + "energy": -0.0001, + "tax": 0.1621, + "startsAt": "2024-08-22T16:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.1917, + "energy": 0.0249, + "tax": 0.1668, + "startsAt": "2024-08-22T17:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2685, + "energy": 0.0895, + "tax": 0.179, + "startsAt": "2024-08-22T18:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2963, + "energy": 0.1127, + "tax": 0.1836, + "startsAt": "2024-08-22T19:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2948, + "energy": 0.1115, + "tax": 0.1833, + "startsAt": "2024-08-22T20:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.293, + "energy": 0.11, + "tax": 0.183, + "startsAt": "2024-08-22T21:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2757, + "energy": 0.0955, + "tax": 0.1802, + "startsAt": "2024-08-22T22:00:00.000+02:00", + "currency": "EUR" + }, + { + "total": 0.2564, + "energy": 0.0792, + "tax": 0.1772, + "startsAt": "2024-08-22T23:00:00.000+02:00", + "currency": "EUR" + } + ] + } + } + } + ] + } + } +}