简体   繁体   中英

How to Loop HTTPClient request to get all elements

0Good Day,

On my azure function v1 app I wrote a framework to handle HTTPCLIENT request/response and connect to our api service, I have a method to get all transactions(with parameters: pageNumber, pageSize), after calling the meththod with pageSize=100 , Iam able to get the first 100 elements, but the total elements is lets say 1000, how can I loop my request to call the getTransaction again, if the response has more items or elements

here is the example response of getting transaction:

{  
   "_embedded":{  
      "testTransactions":[  ]
   },
   "_links":{  },
   "page":{  
      "size":100,
      "totalElements":1000,
      "totalPages":10,
      "number":0
   }
}    

Thanks

int _pageNumber = 0;
do {
            _response = await TransactionAsync(_pageNumber, 100);
            if(_response != null && _response.saleTransactions != null && _response.page != null) {
                _pageNumber = _response.page.number + 1;

                foreach (var item in _response.saleTransactions) {
                    transactions.Add(item);
                }
            }

        } while (_pageNumber <= _response.page.totalPages && transactions.Count() < _response.page.totalElements);

You have to call the API Service again meaning, create a loop for it. Set a flag or index where your current return for the row ends for the result of your api.

Eg

page:{
"size" : 100,
"totalElements" : 1000,
"currRow" : 10 //the end index of your list
}

And then when you have to request for the api again, you have to pass the current row index that you have for you to fetch another batch of list, so that you wouldn't start at index = 0 again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM