350-901 · Question #31
Refer to the exhibit. A developer is creating a python script to use the Webex Teams REST API to list joined spaces, retry after the server-specified amount of time if a "Too many requests" response…
This question tests knowledge of Python REST API scripting with the Webex Teams API, specifically handling HTTP rate limiting (429 Too Many Requests) by reading the server-specified Retry-After header and implementing a wait-and-retry loop.
Question
import requests
token = "BEARER_TOKEN_HERE"
url = 'https://api.ciscopark.com/v1/rooms'
headers = {'content-type': 'application/yang-data+json',
'accept': 'application/yang-data+json',
'Authorization':f'Bearer {token}'}
while True:
response = requests.get(url, headers=headers, verify=False)
status = <item 1>
if (status == 200):
print("Success")
break
elif(status == <item 2>):
sleep_time = int(<item 3>)
print(f'Too Many requests. Sleeping for {sleep_time}, <item 4>.)
time.sleep(sleep_time)
else:
print("Error code" + str(status) + "detected.")
break
Available Snippets from Answer Area: 429, RETRIES, backoff, backoff, iExplanation
This question tests knowledge of Python REST API scripting with the Webex Teams API, specifically handling HTTP rate limiting (429 Too Many Requests) by reading the server-specified Retry-After header and implementing a wait-and-retry loop.
Approach. Item 1 must be 'response.status_code' - the requests library exposes the HTTP status code via this attribute. Item 2 must be '429', the standard HTTP status code for Too Many Requests (rate limiting). Item 3 must be response.headers['Retry-After'] - when a 429 is returned, the server includes a Retry-After header specifying exactly how many seconds the client must wait before retrying, and converting it to int() makes it usable by time.sleep(). Item 4 completes the f-string log message, and 'backoff' is the correct snippet from the provided list, describing the strategy (exponential or server-directed backoff) being applied - the while True loop then calls time.sleep(sleep_time) and retries, satisfying the requirement to retry after the server-specified delay rather than a fixed arbitrary pause.
Concept tested. HTTP rate limiting handling in Python REST API clients: recognizing HTTP 429 as the Too Many Requests status code, extracting the Retry-After response header to honor the server-directed retry delay, and structuring a retry loop using the requests library - core skills for Cisco DevNet DEVASC exam objectives around REST API consumption and error handling.
Reference. RFC 6585 (HTTP 429 Too Many Requests); Webex REST API developer documentation; Cisco DevNet DEVASC 200-901 exam topic: Construct a Python script that calls a REST API using the requests library
Topics
Community Discussion
No community discussion yet for this question.