350-901 · Question #40
Refer to the exhibit. This snippet of a script has recently started exiting abnormally with an exception stating "Unexpected HTTP Response code: 429". response = requests.get(url) if…
The correct answer is D. response = requests.get(url) if response.status_code == 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) response = requests.get(url) elif response.status_code != 200: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json(). The script needs to be modified to gracefully handle an HTTP 429 (Too Many Requests) status code, which indicates rate limiting by the API.
Question
Exhibit
Options
- Aresponse = requests.get(url) if response.status_code == 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) elif response.status_code != 200: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()
- Bresponse = requests.get(url) if response.status_code != 200 and response.status_code != 429: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()
- Cresponse = requests.get(url) if response.status_code != 200 and response.status_code != 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()
- Dresponse = requests.get(url) if response.status_code == 429: backoff_seconds = int(response.headers['Retry-After']) sleep(backoff_seconds) response = requests.get(url) elif response.status_code != 200: error_message = "Unexpected HTTP Response code: {}".format(response.status_code) raise Exception(error_message) data = response.json()
How the community answered
(39 responses)- A8% (3)
- B3% (1)
- C5% (2)
- D85% (33)
Why each option
The script needs to be modified to gracefully handle an HTTP 429 (Too Many Requests) status code, which indicates rate limiting by the API.
This option sleeps after receiving a 429 but does not re-issue the 'requests.get(url)' call, meaning 'data = response.json()' would still attempt to parse the original 429 error response.
This option modifies the exception condition to ignore 429 errors but does not implement any backoff or retry logic, leading to the 'data = response.json()' call failing or processing an empty/error response.
This option incorrectly applies the sleep logic only if the status code is neither 200 nor 429, and then still raises an exception without retrying the request.
This solution correctly identifies an HTTP 429 status code, pauses execution for the duration specified in the 'Retry-After' header, and then re-attempts the 'requests.get(url)' call to retrieve the data. This implements an effective rate-limiting retry mechanism, allowing the script to eventually succeed after waiting.
Concept tested: API rate limiting handling with retry logic
Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429
Topics
Community Discussion
No community discussion yet for this question.
