Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
- "7"
- "8"
- "9"
sudo: false
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Usage:
mws.Orders.ListOrders({
MarketplaceId: 'lel',
MaxResultsPerPage: 10,
CreatedAfter: new Date(1,1,2015),
CreatedBefore: new Date(1,2,2015)
CreatedAfter: new Date(2015, 1, 1),
CreatedBefore: new Date(2015, 1, 2)
})
.then(({ result, metadata }) => {
// result
Expand Down
56 changes: 22 additions & 34 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ class AmazonMwsClient {
* @return Promise
*/
call(req, q, meta) {

const doRetry = (err) => {
if (!meta.retry) {
throw err;
}

const attempt = meta.attempt + 1;

if (attempt > meta.max_attempts) {
throw err;
}

// simple exponential backoff for dealing with throttling
const backoffDuration = Math.min(100 * Math.pow(2, attempt), meta.max_backoff);

return Promise.delay(backoffDuration)
.then(() => this.call(req, q, _.assign({}, meta, { attempt })));
};

return this.request(req, q, meta).then((response) => {
const body = response.body;

Expand Down Expand Up @@ -166,40 +185,9 @@ class AmazonMwsClient {
metadata: _.concat(metadata, nextData.metadata)
}));
})
.catch({ code: 'RequestThrottled' }, (err) => {
if (!meta.retry) {
throw err;
}

const attempt = meta.attempt + 1;

if (attempt > meta.max_attempts) {
throw err;
}

// simple exponential backoff for dealing with throttling
const backoffDuration = Math.min(100 * Math.pow(2, attempt), meta.max_backoff);

return Promise.delay(backoffDuration)
.then(() => this.call(req, q, _.assign({}, meta, { attempt })));
})
.catch({ code: 'QuotaExceeded' }, (err) => {
if (!meta.retry) {
throw err;
}

const attempt = meta.attempt + 1;

if (attempt > meta.max_attempts) {
throw err;
}

// simple exponential backoff for dealing with throttling
const backoffDuration = Math.min(100 * Math.pow(2, attempt), meta.max_backoff);

return Promise.delay(backoffDuration)
.then(() => this.call(req, q, _.assign({}, meta, { attempt })));
});
.catch({ code: 'InternalError' }, doRetry)
.catch({ code: 'RequestThrottled' }, doRetry)
.catch({ code: 'QuotaExceeded' }, doRetry);
});
}

Expand Down