Get all pages using python request

I am using api to get orders from website. The problem is she only takes 20 orders. I figured out that I needed to use a pagination iterator, but I don't know how to use it. How to get all orders at the same time.

My code:

def search_orders(self):
    headers = {'Authorization':'Bearer %s' % self.token,'Content-Type':'application/json',}
    url = "https://api.flipkart.net/sellers/orders/search"
    filter = {"filter": {"states": ["APPROVED","PACKED"],},}
    return requests.post(url, data=json.dumps(filter), headers=headers)

      

Here is a link to the documentation.

Documentation

+3


source to share


1 answer


You need to do what the documentation suggests -

The first call to the search API returns a finite number of results based on the pageSize value. Calling the URL returned in the nextPageURL field of the response retrieves subsequent pages of the search result.

nextPageUrl - String - The GET call on this URL will output the results of the next page. Not on the last page

(Emphasis mine)



You can use response.json()

to get the json of the response. Then you can check the flag - hasMore

- to see if there is more, if use requests.get()

to get the response for the next page, and keep doing this until hasMore

it becomes false. Example -

def search_orders(self):
    headers = {'Authorization':'Bearer %s' % self.token,'Content-Type':'application/json',}
    url = "https://api.flipkart.net/sellers/orders/search"
    filter = {"filter": {"states": ["APPROVED","PACKED"],},}
    s = requests.Session()
    response = s.post(url, data=json.dumps(filter), headers=headers)
    orderList = []
    resp_json = response.json()
    orderList.append(resp_json["orderItems"])
    while resp_json.get('hasMore') == True:
        response = s.get('"https://api.flipkart.net/sellers{0}'.format(resp_json['nextPageUrl']))
        resp_json = response.json()
        orderList.append(resp_json["orderItems"])
    return orderList

      

The above code should return a complete list of orders.

+5


source







All Articles