执行一定的逻辑,直到满足要求

问题描述 投票:0回答:2

我已经编写了 C# 代码来从供应商 API 中提取数据,在这里我只能在每个 API 请求中提取最多 100 条记录。如果 API 存在/返回的记录超过 100 条,则 API 将返回以 Next URL 属性形式附加的

"paging"
信息,如下所示在
JSON
响应数据中;

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

我在这里关心的是我想编写一个递归代码逻辑,它将检查返回的

JSON
响应数据,如果它包含响应中附加的
"paging"
属性,那么它会再次调用 API,直到到达最终请求调用其中
"paging"
属性数据不会在
JSON
响应中附加/返回,否则将为空。

目前我正在使用多个

If
语句来执行此操作,并且仅提取数据直到现在有 300-400 条记录,如下所示;

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 ....
    }                    
}

感谢您的建议!

c# .net recursion windows-services
2个回答
2
投票

使用

while
循环解决这个问题可能看起来像这样

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;
}

1
投票
var recentDealsInfo = await _objReq.Post<Deals>(_limit, dtPastDay, isGetRecentAPI: true);

//store response in a list of deals
List<Deal> lstRecentDeals = recentDealsInfo.results;

var after = recentDealsInfo.paging.next.after;

//handle paging for more than 100 records
while (true)
{
    if (string.IsNullOrEmpty(after))
    {
        break;
    }
    
    //build request url using paging information for next 100 records
    var tmp = await _objReq.Post<Deals>(_limit, dtPastDay, nxtLink: after, isGetRecentAPI: true);
    lstRecentDeals.AddRange(tmp.results);
    
    after = tmp.paging.next.after;
}
© www.soinside.com 2019 - 2024. All rights reserved.