Created
July 20, 2016 18:38
-
-
Save nozzlegear/a078c2b49c9a31996ca0cfb9a07d8e5f to your computer and use it in GitHub Desktop.
A quick example for handling Shopify's API request limit with ShopifySharp.
This file contains hidden or 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
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