Send an idempotency key on write requests - #17
Open
JeyKip wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
--retryflag repeats failed requests. It repeats all of them, includingPOST,PUT,PATCHandDELETE.The problem is that when a request fails with 502 or 504, we do not know what happened on the server. The request may have failed before anything was saved. Or it may have been processed correctly and only the response was lost. We cannot tell the difference from the client side.
If we retry in the second case, we create a second record. For an accounting CLI this means a duplicated invoice or journal entry.
The DualEntry API added idempotency keys on 2026-08-12:
This PR uses that header. Now every write request sends a key. All retries of the same request send the same key. The server then replays the first response instead of doing the work again, so we no longer need to know what happened on the server.
What the API promises
From the endpoint docs, for example create recurring request, update recurring request, delete recurring request and partial update of a customer payment:
The last point is important. Each logical request must get its own key. We cannot reuse one key for several requests.
Changes
In
src/dualentry_cli/client.py:_requestnow adds anIdempotency-Keyheader forPOST,PUT,PATCHandDELETE.GETdoes not get the header. It does not change anything on the server, so the header has no meaning there.uuid.uuid4(). It is created once per_requestcall, before the retry loop. This is the important part. If we created a new key for each attempt, the bug would still be there.--retryis off. Something else may repeat the request, for example a proxy. We usesetdefault, so if a caller passes its own key, we keep it.patch()method. The API documentsPATCHfor partial updates, but the client could not send one.Tests
tests/test_client.pyhad no tests for the retry logic at all. A new classTestIdempotencyKeywith 10 cases was added:test_write_methods_send_an_idempotency_keytest_get_does_not_send_an_idempotency_keytest_retry_reuses_the_same_key_across_attemptstest_every_retry_attempt_carries_the_keytest_separate_requests_use_different_keystest_caller_supplied_key_is_not_overwrittentest_key_is_sent_even_when_retry_is_disabledretry=FalseA
no_backofffixture sets_RETRY_DELAYSto zeros. Without it the retry tests would wait 1s, 2s and 4s.Note about the changes
The PR [#16] needs to be merged into
mainfirst, and those changes need to be pulled into this branch for the tests to pass. The changes were originally made and tested on top of the fix/ci-dependency-drift branch, then moved to the current branch, which was created frommain.Test plan
uv run pytest)uv run ruff check .)dualentry <command>(since I do not have a valid API key, I tested in a mocked environment)