代码之家  ›  专栏  ›  技术社区  ›  Rahul Hendawe

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

  •  0
  • Rahul Hendawe  · 技术社区  · 4 年前

    我写了一个 C# 从供应商API获取数据的代码,在这里,每个API请求最多只能获取100条记录。如果API存在/返回的记录超过100条,则API将返回 "paging" 以Next URL属性的形式附加的信息,如下所示 JSON 响应数据;

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

    我在这里关心的是,我想编写一个递归代码逻辑,用于检查返回的 JSON 响应数据,如果它包含 “分页” 属性,然后它再次调用API,直到到达最后的请求调用 “分页” 属性数据将不会附加/返回到 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 ....
        }                    
    }
    

    感谢您的建议!

    2 回复  |  直到 4 年前
        1
  •  2
  •   Biesi    4 年前

    使用a解决此问题 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;
    }
    
        2
  •  1
  •   VietDD    4 年前
    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;
    }