Search RU
Tasks / API / Idempotent order creation
Statement hard

Idempotent order creation

`create_order(client, payload, key)` creates an order idempotently.

Client: `post("/orders", json=payload, headers=headers)` → a response with
`status_code` and `json()`.

Rules:

- `201` — return the response body;
- `409` — return `{"duplicate": True}`, no retries;
- `5xx` — retry, at most three attempts in total;
- `4xx` (except 409) — `ApiError` immediately;
- every request must carry an `Idempotency-Key` header with the given key.

Write tests with a fake client: success, retry after a 500, 409, 400, and
exhausted attempts. Check both the responses and the headers.

apiidempotencyretry
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

What makes 409 different from 500 when it comes to retries? One status is a
result, the other is a transient failure.

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