Skip to content

add price info today #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Sources/TibberSwift/Entities/PriceInfoToday.swift
Original file line number Diff line number Diff line change
@@ -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]
}
10 changes: 10 additions & 0 deletions Sources/TibberSwift/Operations/PriceInfoTodayOperation.swift
Original file line number Diff line number Diff line change
@@ -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")
}
}
18 changes: 18 additions & 0 deletions Sources/TibberSwift/Queries/PriceInfoToday.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
viewer {
homes {
id
currentSubscription {
priceInfo {
today {
total
energy
tax
startsAt
currency
}
}
}
}
}
}
7 changes: 7 additions & 0 deletions Sources/TibberSwift/TibberSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions Tests/TibberSwiftTests/TibberSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
185 changes: 185 additions & 0 deletions Tests/TibberSwiftTests/json/PriceInfoToday.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
}
]
}
}
}
Loading