-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctopus.py
142 lines (118 loc) · 3.22 KB
/
octopus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import dataclasses
import datetime
import decimal
import os
from typing import List, Optional
import requests
from dotenv import load_dotenv
load_dotenv()
API_URL = os.environ["API_URL"]
OCTOPUS_EMAIL = os.environ["OCTOPUS_EMAIL"]
OCTOPUS_PASSWORD = os.environ["OCTOPUS_PASSWORD"]
@dataclasses.dataclass(frozen=True)
class HHReading:
start_at: datetime.datetime
end_at: datetime.datetime
version: str
value: decimal.Decimal
AUTH_BODY = """
mutation obtainKrakenToken($input: ObtainJSONWebTokenInput!) {
obtainKrakenToken(input: $input) {
refreshToken
refreshExpiresIn
payload
token
}
}
"""
def get_token() -> str:
response = requests.post(
url=API_URL,
json={
"query": AUTH_BODY,
"variables": {
"input": {
"email": OCTOPUS_EMAIL,
"password": OCTOPUS_PASSWORD,
}
},
},
)
response_dict = response.json()
_validate_response(response)
return response_dict["data"]["obtainKrakenToken"]["token"]
GET_ACCOUNT_BODY = """
query accountViewer {
viewer {
accounts {
number
}
}
}
"""
def get_account_number(token: str) -> str:
response = requests.post(
url=API_URL,
json={
"query": GET_ACCOUNT_BODY,
},
headers={"authorization": f"JWT {token}"},
)
response_dict = response.json()
_validate_response(response)
return response_dict["data"]["viewer"]["accounts"][0]["number"]
GET_HH_BODY = """
query halfHourlyReadings($accountNumber: String!, $fromDatetime: DateTime, $toDatetime: DateTime) {
account(accountNumber: $accountNumber) {
properties {
electricitySupplyPoints {
halfHourlyReadings(fromDatetime: $fromDatetime, toDatetime: $toDatetime) {
startAt
endAt
version
value
}
}
}
}
}
"""
def get_hh_readings(
account_number: str,
token: str,
start_at: datetime.datetime,
end_at: Optional[datetime.datetime] = None,
) -> List[HHReading]:
variables = {
"accountNumber": account_number,
"fromDatetime": start_at.isoformat(),
}
if end_at:
variables["toDatetime"] = end_at.isoformat()
response = requests.post(
url=API_URL,
json={
"query": GET_HH_BODY,
"variables": variables,
},
headers={"authorization": f"JWT {token}"},
)
_validate_response(response)
response_dict = response.json()
readings_raw = response_dict["data"]["account"]["properties"][0][
"electricitySupplyPoints"
][0]["halfHourlyReadings"]
readings: List[HHReading] = []
for reading_raw in readings_raw:
readings.append(
HHReading(
start_at=datetime.datetime.fromisoformat(reading_raw["startAt"]),
end_at=datetime.datetime.fromisoformat(reading_raw["endAt"]),
version=reading_raw["version"],
value=decimal.Decimal(reading_raw["value"]),
)
)
return readings
def _validate_response(response: requests.Response) -> None:
if (errors := response.json().get("errors")) and len(errors):
raise ValueError(errors)