QuestionQ352

Using APIs

Retrieve the complete list of Cisco Webex rooms by using pagination and the Webex API. Not every option is used.

Drag & Drop
print(response.json())
while True
except IndexError
response.links
except KeyError
for i in range(10):
import requests
api_key = 'NGMwYWY1YzktYzZiM6a-bf05-067ef73c6836'
headers = {'Authorization': f'Bearer {api_key}', 'Content-Type':
           'application/json'}

url = 'https://webexapis.com/v1/rooms'
params = {'max': 10}

:
    response = requests.get(url, headers=headers, params=params)
    
    try:
        url = ['next']['url']
    :
        break
Explanation

Webex list APIs paginate with a rel="next" URL in the HTTP Link header. Requests parses that header into response.links, so repeatedly requesting the returned next URL retrieves every page. When no next-page relation remains, indexing response.links['next'] raises KeyError, which terminates the loop.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!