0
0
Rest APIprogramming~10 mins

Retry and failure handling in Rest API - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to retry the API call once if it fails.

Rest API
response = requests.get(url)
if response.status_code != 200:
    response = requests.[1](url)
Drag options to blanks, or click blank then click option'
Aget
Bdelete
Cpost
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different HTTP method like post or put for retry.
Not retrying the request at all.
2fill in blank
medium

Complete the code to catch exceptions during the API call and retry once.

Rest API
try:
    response = requests.get(url)
except [1]:
    response = requests.get(url)
Drag options to blanks, or click blank then click option'
AValueError
BTypeError
CKeyError
Drequests.exceptions.RequestException
Attempts:
3 left
💡 Hint
Common Mistakes
Catching unrelated exceptions like ValueError or KeyError.
Not catching exceptions at all.
3fill in blank
hard

Fix the error in the retry logic to limit retries to 3 attempts.

Rest API
max_retries = 3
attempt = 0
while attempt < max_retries:
    try:
        response = requests.get(url)
        if response.status_code == 200:
            break
    except requests.exceptions.RequestException:
        pass
    [1] += 1
Drag options to blanks, or click blank then click option'
Aurl
Battempt
Cresponse
Dmax_retries
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing the wrong variable.
Not incrementing any variable causing infinite loop.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that retries only for status codes 500 or 502.

Rest API
retry_status = {code: True for code in [[1], [2]]}
Drag options to blanks, or click blank then click option'
A500
B404
C502
D403
Attempts:
3 left
💡 Hint
Common Mistakes
Including client error codes like 404 or 403.
Using the same code twice.
5fill in blank
hard

Fill all three blanks to build a retry loop that waits 2 seconds between attempts and stops after 5 tries.

Rest API
import time
max_attempts = [1]
attempt = 0
while attempt < max_attempts:
    try:
        response = requests.get(url)
        if response.status_code == 200:
            break
    except requests.exceptions.RequestException:
        pass
    time.sleep([2])
    attempt [3] 1
Drag options to blanks, or click blank then click option'
A5
B2
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using too few or too many retries.
Forgetting to increment the attempt counter.
Using wrong operator for increment.