Idempotency keys #466
-
I'm trying to figure out the best way to handle idempotency keys, much like Stripe's API and the draft spec (https://tools.ietf.org/id/draft-idempotency-header-01.html). I can add the requirement for an idempotency header to any endpoint, doing something like this: def my_endpoint(
request,
input: InputSchema,
idempotency_key: str = Header(..., alias="Idempotency-Key")
):
... The problem is that the logic needs to be managed outside the endpoint handler, because it needs to work for all status codes, including validation failures. The approximate algorithm should be something like this:
I'm struggling find the best way to inject this behaviour with Django Ninja. My best course of action so far seems to be to override the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Just trying to imagine some simple case - let's say there is a form where user clicks a button and a comment created user can click 2-3 times (because of faulty mouse) - but we want to to create only one record in database if that case is just for few methods/endpoints - I would use decorator (in decorator you have access to request and header and can store for key processing status) if you want some global handling (like for all POST/PUT/PATCH methods) I would do this with django middlewares layer (the only issue is patching generated openapi schema to document required headers) |
Beta Was this translation helpful? Give feedback.
-
some plugin acutally exist already - https://pypi.org/project/django-idempotency-key/ |
Beta Was this translation helpful? Give feedback.
-
Answer extracted from a comment:
|
Beta Was this translation helpful? Give feedback.
Answer extracted from a comment: