We're having to call data from a hideously limited Web API provided to us by means beyond our control. The logic flow is as follows:
For each product represented by a list of ID values
Get each batch of sub-categories of type FOO (100 records max. per call)
Keep calling the above until no records remain
Get each batch of sub-categories of type BAR (100 records max. per call)
Keep calling the above until no records remain
This at present is generating nearly 100 web API calls (we've asked the provider, and there's no improving the web API to mitigate this).
I'm worried that performance will suffer drastically because of this, so am trying to get my head around the asynchronous alternative, in the hope that this will help. One major issue is that the data can only be called once. After that, it becomes locked and isn't resent, which is a major limitation to our testing.
I've read here, here and here, but am struggling to adapt to my code as I think I need two await calls rather than one, and am worried things will get messed up.
Can anyone please apply the await and async logic into this pseudo code so that I can then read up and try follow the flow of what's happening?
public class DefaultController : Controller
{
public ActionResult Index()
{
var idlist = new List<String>() {"123", "massive list of strings....", "789"};
var xdoc = new XDocument();
xdoc.Declaration = new XDeclaration("1.0", Encoding.Unicode.WebName, "yes");
var xroot = new XElement("records");
xdoc.Add(xroot);
foreach (string id in idlist)
{
// Get types FOO -----------------------------------
Boolean keepGoingFOO = true;
while (keepGoingFOO)
{
// 100 records max per call
var w = new WebServiceClient();
request.enumType = enumType.FOO;
var response = w.response();
foreach (ResultItem cr in response.ResultList)
{
var xe = new XElement("r");
// create XML
xroot.Add(xe);
}
keepGoingFOO = response.moreRecordsExist;
}
// Get types BAR -----------------------------------
Boolean keepGoingBAR = true;
while (keepGoingBAR)
{
// 100 records max per call
var w = new WebServiceClient();
request.enumType = enumType.BAR;
var response = w.response();
foreach (ResultItem cr in response.ResultList)
{
var xe = new XElement("r");
// create XML
xroot.Add(xe);
}
keepGoingBAR = response.moreRecordsExist;
}
}
return View(xdoc);
}
}