I am requesting the data from some kind of Products API, but the thing is that I am getting it 20 by 20. So the endpoint looks like this:
https://www.someDummyAPI.com/Api/Products?offset=0&count=20
Note: I can't change the count, it will always be 20.
I.e. The data from this endpoint will contain 20 record, from 0 to 20 and after that I have to increase offset by 20 to get next 20 record and so on (totally it's about 1500 record so I have to make approximately 700 request ).
After getting all the data I am inserting it into the SQL database using stored procedure (this is different process).
So my question is, how can I speed up the fetching process, I thought about running tasks in parallel but I need to get results from the response.
For now this process looks like this :
protected async void FSL_Sync_btn_Click(object sender, EventArgs e)
{
int offset = 0;
int total= 0;
bool isFirst = true;
DataTable resTbl = CreateDt();
while (offset < total || offset == 0)
{
try
{
var data = await GetFSLData(offset.ToString(),"Products");
JObject Jresult = JObject.Parse(data);
if (isFirst)
{
Int32.TryParse(Jresult.SelectToken("total").ToString(),out total);
isFirst = false;
}
// Function to chain up data in DataTable
resTbl = WriteInDataTable(resTbl, Jresult);
offset += 20;
}
catch(Exception ex)
{
var msg = ex.Message;
}
}
}
So the process flow I am taking is:
- Get data from API (let's say first 20 record).
- Add it two existing
DataTable
usingWriteInDataTable
function. - Insert data into SQL Database from this
resTbl
Datatable
(completely different process, not shown in this screenshot).
I haven't used parallel tasks yet (don't even know if it's a correct solution for it), so would appreciate any help.