In Shopify a product can belong to a collection.
I have written a method which accepts an organisation. It would then get a list of collections that this organisation has from Shopify. Finally, it has to loop through each collection to get the products that belong to this collection.
I don't want to overload Shopify server by sending too many requests, so I have added 75 ms wait between each request. But since this method is async, I am not quite sure if the responses are handled correctly? Do I need any sort of mutext or locking mechanism to ensure each response is being handled correctly?
public async Task<List<ShopifyCollection>> GetShopifyCollections(Organisation organisation)
{
int waitInMilliSeconds = 50;
var shopifyCollections = new List<ShopifyCollection>();
var customCollections = await GetAllCustomCollections(organisation);
foreach (var cc in customCollections)
{
shopifyCollections.AddCollection(cc.Id, cc.Title);
}
foreach (var curCollection in shopifyCollections)
{
var products = await GetCollectionProducts(organisation, curCollection.CollectionId);
curCollection.ProductIds = products.Where(p => p.Id != null && p.Id > 0).Select(p => p.Id.Value).ToList();
await Task.Delay(waitInMilliSeconds); // <-- do not overload Shopify
}
return shopifyCollections;
}
Task.Delayinstead ofThread.Sleep\$\endgroup\$Delayin anasyncmethod and just wanted to double check if this is correct. \$\endgroup\$