-
Notifications
You must be signed in to change notification settings - Fork 10
/
BringClient.js
252 lines (236 loc) · 9.25 KB
/
BringClient.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const axios = require("axios").default;
const Store = require("data-store");
const querystring = require("querystring");
const https = require("https");
class BringClient {
/**
* This is an Axios client instance that is intended for making authenticated requests to
* the 'api.getbring.com' domain using normal TLS rules.
*/
#authenticatedClient;
/**
* This is an Axios client instance that won't fail a request if there's issues with the SSL certificate.
* This is necessary for requests to the 'web.getbring.com' domain as long as their certificate chain
* still includes a broken "Sectigo" certificate.
*/
#insecureClient;
constructor(opts, modulePath) {
this.userId = undefined;
this.articles = undefined;
this.defaultListId = "";
this.store = new Store({path: modulePath + "/bring.config.json", cwd: "store"});
this._init(opts);
}
_init(opts) {
this.locale = "de-DE";
if (opts.locale) {
this.locale = opts.locale;
}
this.#insecureClient = axios.create({
baseURL: "https://web.getbring.com/",
headers: {
"x-bring-api-key": "cof4Nc6D8saplXjE3h3HXqHH8m7VU2i1Gs0g85Sp"
},
httpsAgent: new https.Agent({
// This setting disables SSL certificate errors and reduces security
rejectUnauthorized: false
})
});
this.#authenticatedClient = axios.create({
baseURL: "https://api.getbring.com/rest/v2/",
headers: {
"x-bring-api-key": "cof4Nc6D8saplXjE3h3HXqHH8m7VU2i1Gs0g85Sp"
},
});
if (this.mustLogin()) {
this.login(opts.email, opts.password).then(() => {
this.userId = this.store.get("user_id");
this.defaultListId = this.store.get("default_list_id");
this.#authenticatedClient.defaults.headers.common["Authorization"] = "Bearer " + this.store.get("access_token");
});
} else {
this.userId = this.store.get("user_id");
this.defaultListId = this.store.get("default_list_id");
this.#authenticatedClient.defaults.headers.common["Authorization"] = "Bearer " + this.store.get("access_token");
}
}
mustLogin() {
return !this.store.get("access_token") || !this.store.get("valid_until") || new Date().getTime() > this.store.get("valid_until");
}
login(email, password) {
return this.#authenticatedClient
.post(
"/bringauth",
querystring.stringify({
email: email,
password: password
})
)
.then((response) => {
const loginObj = response.data;
this.store.set("user_id", loginObj["uuid"]);
this.store.set("default_list_id", loginObj["bringListUUID"]);
this.store.set("access_token", loginObj["access_token"]);
this.store.set("valid_until", new Date().getTime() + (loginObj["expires_in"] * 1000));
}).catch(error => {
console.error("Error while Logging-in with Bring-Client in MMM-Bring:", "HTTP" + error.response.status, error.response.data);
});
}
getLists() {
return this.#authenticatedClient
.get(
"/bringusers/" + this.userId + "/lists"
)
.then((response) => {
// Remember the names of the Lists
for (let i = 0, len = response.data.lists.length; i < len; i++) {
this.store.set("list_name_" + response.data.lists[i].listUuid, response.data.lists[i].name);
this.store.set("list_id_" + response.data.lists[i].name, response.data.lists[i].listUuid);
}
return response.data.lists;
})
.catch((error) => console.error(error));
}
getList(withDetails, listName) {
let listId = this.defaultListId;
if (!!listName) {
listId = this.store.get("list_id_" + listName);
}
return this.#authenticatedClient
.get("/bringlists/" + listId)
.then((response) => {
const list = response.data;
if (list && list.uuid) {
list.name = this.store.get("list_name_" + list.uuid);
}
return list;
}
).then(list => {
if (withDetails && !this.articles) {
return this.getArticles().then((articles) => {
this.articles = articles;
return list;
});
} else {
return list;
}
})
.then((list) => {
if (withDetails) {
return this.getListDetails(listId).then(details => {
const detailsMap = {};
for (let i = 0, len = details.length; i < len; i++) {
detailsMap[details[i].itemId] = details[i];
}
for (let i = 0, len = list.purchase.length; i < len; i++) {
list.purchase[i].details = detailsMap[list.purchase[i].name];
list.purchase[i].imageSrc = this.getImageSrc(list.purchase[i]);
// Translate it
if (
this.articles &&
this.articles[list.purchase[i].name] != null
) {
list.purchase[i].name = this.articles[list.purchase[i].name];
}
}
for (let i = 0, len = list.recently.length; i < len; i++) {
list.recently[i].details = detailsMap[list.recently[i].name];
list.recently[i].imageSrc = this.getImageSrc(list.recently[i]);
// Translate it
if (
this.articles &&
this.articles[list.recently[i].name] != null
) {
list.recently[i].name = this.articles[list.recently[i].name];
}
}
return list;
});
} else {
return list;
}
}).catch(error => console.error(error));
}
getListDetails(listId) {
return this.#authenticatedClient
.get("/bringlists/" + listId + "/details")
.then((response) => {
return response.data;
}).catch(error => console.error(error));
}
getArticles(locale) {
if (!locale) {
locale = this.locale;
}
const axiosResponseData = this.#insecureClient
.get(`/locale/articles.${locale}.json`)
.then((response) => response.data)
.catch((e) => console.error(e));
return axiosResponseData;
}
getImageSrc(item) {
let name = item.name;
if (item.details && item.details.userIconItemId) {
name = item.details.userIconItemId;
}
if (
this.articles &&
JSON.stringify(this.articles)
.toLowerCase()
.indexOf(name.toLowerCase()) === -1
) {
const articleKeys = Object.keys(this.articles).map(key => key.toLowerCase());
let foundAlternativeImage = false;
for (let i = 0, len = articleKeys.length; i < len; i++) {
if (item.name.toLowerCase().indexOf(articleKeys[i]) >= 0) {
name = articleKeys[i];
foundAlternativeImage = true;
break;
}
}
if (!foundAlternativeImage) {
name = item.name.substr(0, 1);
}
}
return "https://web.getbring.com/assets/images/items/" + name
.replace(/[.*+-?^${}()|/[\]\\]/g, "_")
.replace(/&/g, "_")
.replace(/\s/g, "_")
.replace(/ä/ig, "ae")
.replace(/ö/ig, "oe")
.replace(/ü/ig, "ue")
.replace(/__/g, "_")
.replace(/__/g, "_")
.normalize("NFD").replace(/[\u0300-\u036f]/g, "") // è, é
.toLowerCase() + ".png";
}
purchase(itemName, listId) {
const config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
return this.#authenticatedClient
.put(
"/bringlists/" + listId,
querystring.stringify({
uuid: listId,
purchase: itemName
}), config).catch(error => console.error(error));
}
recently(itemName, listId) {
const config = {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
return this.#authenticatedClient
.put(
"/bringlists/" + listId,
querystring.stringify({
uuid: listId,
recently: itemName
}), config).catch(error => console.error(error));
}
}
module.exports = BringClient;