The case is that I call an API once to get a list of tenants, then for each tenant I must call again to get a list of usages for the tenant. Unfortunately there is no way to get usages for all tenants in a single call.
Now I wish to try to save time by making these calls concurrent. Then put them all together after the last one arrives. Here is what my attempt looks like so far:
public async Task<List<Usage>> GetUsagesAsync2(Task<List<Tenant>> tenants)
{
List<Usage> usages = new List<Usage>();
foreach (var tenant in await tenants)
{
//Generate request
RestRequest request = new RestRequest("tenants/{tenantID}/usages", Method.Get);
request.AddParameter("tenantID", tenant.id, ParameterType.UrlSegment);
request.AddHeader("Authorization", $"Bearer {Token.access_token}");
//Get response
RestResponse response = await _client.ExecuteAsync(request)
.ConfigureAwait(false);
//Validate response
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception("Failed at Getting usages for a tenant: ");
//Add to list
var t_usage = JsonConvert.DeserializeObject<Wrapper<Usage>>(response.Content);
usages.AddRange(t_usage.items);
}
return usages;
}
The code runs and does what it is supposed to, but I am not sure that it actually runs asynchronously. Takes about 7-8 seconds to run, which I find a bit long to wait on a webpage.