Canvas API pagination

Canvas API’s pagination is mentioned here but is not really very well-documented. For example, r.links[‘last’] may not exist. Therefore a solution as below suggested here may not work

while r.links['current']['url'] != r.links['last']['url']:
    r = requests.get(r.links['next']['url'], headers=headers)
    raw = r.json()
    for question in raw:
        data_set.append(question)

Instead, I changed it a little bit to first check if ‘last’ exists first.

 

while True:
    if 'last' in r.links:
        if r.links['current']['url'] == r.links['last']['url']:
        break
    r = requests.get(r.links["next"]["url"],headers=headers)
    raw = r.json()
    for question in raw:
        data_set.append(question)

Leave a Reply

Your email address will not be published. Required fields are marked *