I have a paged Rest API and want to receive all records. Currently I loop until all records are received synchronously.
Now I want to change the behaviour due to a long time until all records are received. What I want to do, is to start 10 paged requests assigned to a List<Task> and when a request is finished start the next request. So that max 10 requests are running in parallel. My current code looks like:
protected async Task<List<T>> GetAll<T, TListObject>() where TListObject : ApiListModel<T> {
var limit = this.GetRequestLimit();
var token = this.GetBearerToken();
var maxConcurrent = _config.GetValue<int>("Blackduck:MaxConcurrentRequests");
var list = new List<T>();
var tasks = new List<Task<TListObject>>();
//Request first page to get total count
var resp = await this.GetPage<TListObject>(0, limit);
list.AddRange(resp.Items);
var total = resp.TotalCount;
for (int i = 0; i < maxConcurrent; i++)
{
tasks.Add(this.GetPage<TListObject>(0, 0)); // TODO: 0, 0 should be replaced with the offset and limit
}
TListObject result;
while (list.Count < total || (result = Task.WhenAny<TListObject>(tasks)))
{
}
return list;
}
But now I can't use Task.WhenAny<T>()
in a while to start the next run until all records are received. Has anybody an idea how to start the next page?
BR