Intermediate
Retry only what can succeed
send(attempt) returns (status, headers, text) for the given attempt number (starting at 0). Write call_with_retries(send, max_attempts=3) that returns (text, waits) on status 200.
Retry only statuses 408, 429 and 500 to 599. After each failed attempt that will be retried, append a wait in seconds to waits: the integer in the retry-after header if present, otherwise 2 ** attempt. Raise ApiError(status) immediately for any other status, and also when the last attempt fails.
What Check tests
- Succeeds after a 429 (with Retry-After) and a 503
- A 401 is not retried: exactly one call
- Gives up after
max_attemptsand raises the last status - Immediate success returns no waits
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.