One and Done
View SourceOne and Done is the easiest way to make HTTP requests idempotent in Elixir applications.
One and Done supports the following frameworks:
Plug(includingPhoenix)
Usage
One and Done depends on having a pre-existing cache like Nebulex. This guide assumes Nebulex is already configured under MyApp.Cache.
- Add
one_and_doneto yourmix.exsdependencies:
def deps do
[
{:one_and_done, "~> 0.1.5"}
]
end- Add
OneAndDoneto yourPlugpipeline:
defmodule MyAppWeb.Router do
use MyAppWeb, :router
pipeline :api do
# Configuration options for OneAndDone are in the docs
plug OneAndDone.Plug, cache: MyApp.Cache
end
# By default, all POST and PUT requests piped through :api
# that have an Idempotency-Key header set will be cached for 24 hours.
scope "/api", MyAppWeb do
pipe_through :api
resources "/users", UserController
end
end- Make your requests idempotent by adding the
Idempotency-Keyheader:
curl -X POST \
http://localhost:4000/api/users \
-H 'Content-Type: application/json' \
-H 'Idempotency-Key: 123' \
-d '{
"email": "hello@example.com",
"password": "password"
}'
Repeat the request with the same Idempotency-Key header and you will get the same response
without the request being processed again.