-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.js
130 lines (127 loc) · 4.33 KB
/
Network.js
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
const url = "https://api.oejp-kraken.energy/v1/graphql/";
module.exports.getReadings = async (start, end) => {
const authToken = Keychain.get("AuthToken");
if (!authToken) {
throw Error("Invalid token");
}
const getReadingsRequest = new Request(url);
getReadingsRequest.headers = {
"content-type": "application/json",
AUTHORIZATION: `${authToken}`,
};
getReadingsRequest.method = "POST";
getReadingsRequest.body = JSON.stringify({
query: `query halfHourlyReadings(
$accountNumber: String!
$fromDatetime: DateTime
$toDatetime: DateTime
$productCode: String!
) {
account(accountNumber: $accountNumber) {
properties {
electricitySupplyPoints {
halfHourlyReadings(
fromDatetime: $fromDatetime
toDatetime: $toDatetime
productCode: $productCode
) {
startAt
value
costEstimate
}
}
}
}
}`,
variables: {
accountNumber: Keychain.get("OctopusAccount"),
fromDatetime: start,
toDatetime: end,
productCode: "JPN_STANDARD_OCTOPUS_JAN_22",
},
});
response = await getReadingsRequest.loadJSON();
if (response.data.detail) {
throw new Error("Invalid URL");
}
if (response.data.errors) {
if (response.data.errors[0].message === "Invalid data") {
throw new Error("Invalid log-in info");
}
if (response.data.errors[0].message) {
throw new Error("Invalid query");
}
}
return response.data.account.properties[0].electricitySupplyPoints[0]
.halfHourlyReadings;
};
module.exports.getTokens = async (email, password) => {
if (email.length === 0 || password.length === 0) {
throw Error("Invalid field");
}
const getTokensRequest = new Request(url);
getTokensRequest.headers = { "content-type": "application/json" };
getTokensRequest.method = "POST";
getTokensRequest.body = JSON.stringify({
query: `mutation login($input: ObtainJSONWebTokenInput!) {
obtainKrakenToken(input: $input) {
token
refreshToken
}
}`,
variables: {
input: {
email: email,
password: password,
},
},
});
response = await getTokensRequest.loadJSON();
if (response.data.detail) {
throw new Error("Invalid URL");
}
if (response.data.errors) {
if (response.data.errors[0].message === "Invalid data") {
throw new Error("Invalid log-in info");
}
if (response.data.errors[0].message) {
throw new Error("Invalid query");
}
}
const { token, refreshToken } = response.data.obtainKrakenToken;
Keychain.set("AuthToken", token);
Keychain.set("RefreshToken", refreshToken);
};
module.exports.renewAuthToken = async () => {
const refreshToken = Keychain.get("RefreshToken");
if (!refreshToken) throw Error("Invalid refresh token");
const renewAuthTokenRequest = new Request(url);
renewAuthTokenRequest.headers = { "content-type": "application/json" };
renewAuthTokenRequest.method = "POST";
renewAuthTokenRequest.body = JSON.stringify({
query: `mutation login($input: ObtainJSONWebTokenInput!) {
obtainKrakenToken(input: $input) {
token
}
}`,
variables: {
input: {
refreshToken: refreshToken,
},
},
});
response = await renewAuthTokenRequest.loadJSON();
if (response.data.detail) {
throw new Error("Invalid URL");
}
if (response.data.errors) {
if (response.data.errors[0].message === "Invalid data") {
throw new Error("Invalid log-in info");
}
if (response.data.errors[0].message) {
throw new Error("Invalid query");
}
}
const { token } = response.data.obtainKrakenToken;
Keychain.set("AuthToken", token);
};