Skip to content

Instantly share code, notes, and snippets.

@nozzlegear
Created July 20, 2016 18:38
Show Gist options
  • Save nozzlegear/a078c2b49c9a31996ca0cfb9a07d8e5f to your computer and use it in GitHub Desktop.
Save nozzlegear/a078c2b49c9a31996ca0cfb9a07d8e5f to your computer and use it in GitHub Desktop.
A quick example for handling Shopify's API request limit with ShopifySharp.
public async Task MyMethod(List<ShopifyOrder> orders)
{
foreach (var order in listOfOrders)
{
var index = listOfOrders.IndexOf(order);
// If this order is not the first, wait for .5 seconds (an average of 2 calls per second).
if (index > 0)
{
await Task.Delay(500);
}
try {
// Fulfill order here
}
catch (ShopifyException e) when (e.Message.ToLower().Contains("exceeded 2 calls per second for api client") || (int) e.HttpStatusCode == 429 /* Too many requests */)
{
// todo: wait for a longer period (e.g. 10 seconds) and try again, throwing if the retry fails too
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment