-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
757f3b1
commit e56fb5f
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
var Manager = require('../manager.js'); | ||
var _ = require('lodash'); | ||
|
||
/** | ||
* Quota Preset for Pinterest's API | ||
* | ||
* Quota rules based on: https://developers.pinterest.com/docs/api/overview/ | ||
* Pinterest's API docs: https://developers.pinterest.com/docs/getting-started/introduction/ | ||
* | ||
* In a cluster environment a local Server can be used if all requests made on | ||
* behalf of a particular user are only made by a single node.js instance. | ||
* | ||
* @param options | ||
* @returns {Manager} | ||
*/ | ||
module.exports = function (options) { | ||
|
||
var manager = new Manager({ | ||
backoff: 'timeout' | ||
}); | ||
|
||
function addRule(resource) { | ||
|
||
manager.addRule({ | ||
name: resource, | ||
limit: 1000, | ||
window: 60*60*1000, | ||
throttling: 'window-sliding', | ||
queueing: 'fifo', | ||
scope: 'userId', | ||
resource: resource | ||
}); | ||
|
||
} | ||
|
||
// Each unique client ID and user ID pair is limited to approximately 1000 calls per endpoint per hour. | ||
addRule('v1/boards'); | ||
addRule('v1/boards/<board_id>'); | ||
addRule('v1/boards/<board_id>/pins'); | ||
addRule('v1/me'); | ||
addRule('v1/me/boards'); | ||
addRule('v1/me/followers'); | ||
addRule('v1/me/following/boards'); | ||
addRule('v1/me/following/boards/<board_id>'); | ||
addRule('v1/me/following/interests'); | ||
addRule('v1/me/following/interests/<interest_id>'); | ||
addRule('v1/me/following/users'); | ||
addRule('v1/me/following/users/<user_id>'); | ||
addRule('v1/me/likes'); | ||
addRule('v1/me/pins'); | ||
addRule('v1/me/search/boards'); | ||
addRule('v1/me/search/pins'); | ||
addRule('v1/pins'); | ||
addRule('v1/pins/<pin_id>'); | ||
addRule('v1/users/<username_or_id>'); | ||
|
||
return manager; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict'; | ||
|
||
var quota = require('../../../lib/index.js'); | ||
|
||
var _ = require('lodash'); | ||
var BPromise = require('bluebird'); | ||
|
||
|
||
describe('Preset Pinterest', function () { | ||
|
||
it('should grant 1000 requests per endpoint', function () { | ||
|
||
var quotaServer = new quota.Server(); | ||
quotaServer.addManager('pinterest'); | ||
|
||
var quotaClient = new quota.Client(quotaServer); | ||
|
||
return BPromise.resolve() | ||
.then(function () { | ||
|
||
return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me': 1000 }); | ||
|
||
}) | ||
.then(function () { | ||
|
||
return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me': 1 }, { maxWait: 0 }) | ||
.then(function () { | ||
throw new Error('Expected OutOfQuotaError'); | ||
}) | ||
.catch(quota.OutOfQuotaError, function (err) { | ||
return; // Expected | ||
}); | ||
|
||
}) | ||
.then(function () { | ||
|
||
return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me/boards': 1000 }); | ||
|
||
}) | ||
.then(function () { | ||
|
||
return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me/boards': 1 }, { maxWait: 0 }) | ||
.then(function () { | ||
throw new Error('Expected OutOfQuotaError'); | ||
}) | ||
.catch(quota.OutOfQuotaError, function (err) { | ||
return; // Expected | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
it('should allow updating the limit', function () { | ||
|
||
var quotaServer = new quota.Server(); | ||
quotaServer.addManager('pinterest'); | ||
|
||
var quotaClient = new quota.Client(quotaServer); | ||
|
||
return BPromise.resolve() | ||
.then(function () { | ||
|
||
return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me': 1 }) | ||
.then(function (grant) { | ||
grant.dismiss({ | ||
forRule: { | ||
'v1/me': { | ||
limit: 2 | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
}) | ||
.then(function () { | ||
|
||
return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me': 1 }); | ||
|
||
}) | ||
.then(function () { | ||
|
||
return quotaClient.requestQuota('pinterest', { userId: 1 }, { 'v1/me': 1 }, { maxWait: 0 }) | ||
.then(function () { | ||
throw new Error('Expected OutOfQuotaError'); | ||
}) | ||
.catch(quota.OutOfQuotaError, function (err) { | ||
return; // Expected | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |