Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dallashuggins/a12bde3f1c8d7264de3dc5691bebbba4 to your computer and use it in GitHub Desktop.
Save dallashuggins/a12bde3f1c8d7264de3dc5691bebbba4 to your computer and use it in GitHub Desktop.
Shopify/Bedrock API Call Limit script - check for API limit in headers returned from response
var api_call_limit = response.headers['http_x_shopify_shop_api_call_limit'];
var current_limit = api_call_limit.split('/')[0];
console.log("Current API Call Limit", current_limit);
var future_limit = api_call_limit.split('/')[1];
console.log("Future API Call Limit", future_limit);
var results, items, cursor;
items = response.body.customers;
// when we reach the api limit, set timeout
if (future_limit > current_limit) {
console.log("No timeout needed");
if (items.length !== 0) { // if there are results
results = _.map(items, function(item) {
return item;
});
cursor = {
_meta: {
cursor: {
page: page + 1
}
}
};
} else { // no more results, so return final cursor
results = [];
cursor = {
_meta: {
finalCursor: {}
}
};
}
results.push(cursor);
console.log("Final results", results);
return results;
} else {
setTimeout(function(){
console.log("Finished Timeout");
if (items.length !== 0) { // if there are results
results = _.map(items, function(item) {
return item;
});
cursor = {
_meta: {
cursor: {
page: page + 1
}
}
};
} else { // no more results, so return final cursor
results = [];
cursor = {
_meta: {
finalCursor: {}
}
};
}
results.push(cursor);
console.log("Final results", results);
return results;
}, 10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment