Search code examples
c#.netrecursionwindows-services

Execute certain logic till it meets the requirement


I have written C# code to pull the data from vendor API, Here I can pull only up to 100 records in per API request. If there are more than 100 records present/returned from the API then the API will return "paging" information appended it in the form of Next URL attribute like below in the JSON response data;

 ],
    "paging": {
        "next": {
            "after": "2459708154",
            "link": "https://api.someapi.com/objects?archived=false&limit=100&after=2459708154
        },
        "prev": null
    }

My concern here is that I want to write a recursive code logic which will check the returned JSON response data and if it contains the "paging" attribute appended in the response then it call the API again till it reaches to the final request call where the "paging" attribute data will not appended/returned in the JSON response or it will be null.

Currently I am doing this using multiple If statements and pulling data only till the 300-400 records for now like below;

var recentDealsInfo = await _objReq.Post<Deals>(_limit, dtPastDay, isGetRecentAPI: true);
//store response in a list of deals
List<Deal> lstRecentDeals = recentDealsInfo.results;
//handle paging for more than 100 records
if (!string.IsNullOrEmpty(recentDealsInfo.paging.next.after))
{
    //build request url using paging information for next 100 records
    var nxtdeals = await _objReq.Post<Deals>(_limit, dtPastDay, nxtLink: recentDealsInfo.paging.next.after, isGetRecentAPI: true);
    //add records to the list
    lstRecentDeals.AddRange(nxtdeals.results);
    if (!string.IsNullOrEmpty(nxtdeals.paging.next.after))
    {
        //build request url using paging information for next 100 records
        var nextdeals = await _objReq.Post<Deals>(_limit, dtPastDay, nxtLink: nxtdeals.paging.next.after, isGetRecentAPI: true);
        //add records to the list
        lstRecentDeals.AddRange(nextdeals.results);
        if (!string.IsNullOrEmpty(nextdeals.paging.next.after))
        {
            //build request url using paging information for next 100 records
            var nextdeal = await _objReq.Post<Deals>(_limit, dtPastDay, nxtLink: nextdeals.paging.next.after, isGetRecentAPI: true);
            //add records to the list
            lstRecentDeals.AddRange(nextdeal.results);
        }
        // and so on ....
    }                    
}

Appreciate your suggestions!


Solution

  • Solving this using a while loop could look like this

    var response = await _objReq.Post<Deals>(_limit, dtPastDay, isGetRecentAPI: true);
    List<Deal> recentDeals = response.results;
    string nextPage = response.paging.next.after;
    
    while (!string.IsNullOrEmpty(nextPage))
    {
        response = await _objReq.Post<Deals>(_limit, dtPastDay, nxtLink: nextPage, isGetRecentAPI: true);
        recentDeals.AddRange(response.results);
        nextPage = response.paging.next.after;
    }