Search RU
Tasks / API / Retrying an API request
Statement medium

Retrying an API request

`fetch_user(client, user_id, attempts=3)` fetches a user through a client with a
`get(path)` method. The response has `status_code` and `json()`.

Rules:

- `200` — return `response.json()`;
- `404` — raise `ApiError` immediately, no retries;
- any other status — retry, no more than `attempts` calls in total;
- when the attempts run out — raise `ApiError`.

Write tests with a fake client: no network calls. Cover success, retry, `404`
without a retry, and exhausted attempts.

apimockretrypytest
test_solution.py Python 3.14
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

How many times should `get()` be called in each scenario? Start with the
question: what makes `404` different from `500` for the client?

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts