Last active
March 5, 2018 19:47
-
-
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
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
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