-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLegoApi.js
54 lines (46 loc) · 976 Bytes
/
LegoApi.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
const _ = require("lodash");
const request = require("request-promise-native");
const LEGO_BASE_URL = "https://shop.lego.com";
const OAUTH_URI = "oauth/accessToken";
const PRODUCT_URI = "sh/rest/products/pab/elements";
const defaultOptions = {
baseUrl: LEGO_BASE_URL,
qs: {
api_version: 1,
accept_language: "en-CA",
},
json: true,
};
class LegoApi {
static get DARK_GREEN() {
return 28;
}
constructor() {
this.request = request.defaults(defaultOptions);
this.token = null;
}
async getToken() {
if (!this.token) {
const rawResponse = await this.request.post({
uri: OAUTH_URI,
});
this.token = rawResponse.access_token;
}
return this.token;
}
async getProductWithColor(color) {
const rawData = await this.request.get({
auth: {
bearer: await this.getToken(),
},
uri: PRODUCT_URI,
qs: {
exact_color: color,
offset: 0,
limit: 10000,
},
});
return rawData.elements;
}
}
module.exports = LegoApi;