-
Notifications
You must be signed in to change notification settings - Fork 2
/
echo.js
47 lines (46 loc) · 1.72 KB
/
echo.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
var echo = require('echojs')({
key: process.env.ECHONEST_KEY
});
var quota = require('quota');
var quotaServer = new quota.Server();
quotaServer.addManager('echonest');
var quotaClient = new quota.Client(quotaServer);
module.exports = function(p, pp) {
var gen = function(type) {
var finalFunct = function(params, callback, onFail) {
quotaClient.requestQuota('echonest', {}, { requests: 1 }, { maxWait: 10000 })
.then(function(grant) {
echo(p)[type](params, function(err, json, response) {
if (response.statusCode != 429) {
grant.dismiss({
forRule: {
main: {
limit: parseInt(response.headers['x-ratelimit-limit'])
}
}
});
callback.apply(this, arguments);
} else {
grant.dismiss({
backoff: true //Math.min(60, (62 - new Date().getUTCSeconds()))
});
finalFunct.apply(this, arguments);
}
});
})
.catch(quota.OutOfQuotaError, function(err) {
if (onFail) onFail(err);
console.log('OUT OF QUOTA!');
})
.catch(function(err) {
console.error('QUOTA ERROR!', err);
})
;
};
return finalFunct;
};
return {
post: gen('post'),
get: gen('get')
};
};