-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
let_query.js
40 lines (36 loc) · 1.06 KB
/
let_query.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
var request = require('request');
function let_query(url, query, options = null) {
if (options === null) {
options = {
url: url,
qs: query,
headers: {
'User-Agent': 'Node.js'
},
basic_auth: null,
auth: null,
delay: 1000
};
}
const delay = options.delay;
if (options.basic_auth) {
const [user, pass] = options.basic_auth.split(':');
options.auth = {
user,
pass
};
}
return new Promise(function (resolve, reject) {
request(options, function (error, res, body) {
if (res.statusCode == 200) {
resolve(JSON.parse(body));
} else if (+res.headers['x-ratelimit-remaining'] == 0) {
const date = new Date(+res.headers['x-ratelimit-reset'] * delay);
reject('Rate limit util ' + date);
} else {
reject('Error code ' + res.statusCode);
}
});
});
}
module.exports = let_query;