Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kno-3079): idempotency keys for workflow triggers #17

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## v0.4.7

* Add support for `idempotency_key` on workflow triggers
2 changes: 1 addition & 1 deletion lib/knock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ defmodule Knock do
@doc """
Issues a notify call, triggering a workflow with the given key.
"""
defdelegate notify(client, key, properties), to: Knock.Workflows, as: :trigger
defdelegate notify(client, key, properties, options), to: Knock.Workflows, as: :trigger
end
13 changes: 9 additions & 4 deletions lib/knock/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Knock.Api do
@spec put(Client.t(), String.t(), map(), options()) :: response()
def put(client, path, body, opts \\ []) do
client
|> http_client()
|> http_client(opts)
|> Tesla.put(path, body, opts)
|> handle_response()
end
Expand All @@ -45,7 +45,7 @@ defmodule Knock.Api do
@spec post(Client.t(), String.t(), map(), options()) :: response()
def post(client, path, body, opts \\ []) do
client
|> http_client()
|> http_client(opts)
|> Tesla.post(path, body, opts)
|> handle_response()
end
Expand Down Expand Up @@ -77,17 +77,22 @@ defmodule Knock.Api do
"""
def library_version, do: @lib_version

defp http_client(config) do
defp http_client(config, opts \\ []) do
middleware = [
{Tesla.Middleware.BaseUrl, config.host <> "/v1"},
{Tesla.Middleware.JSON, engine: config.json_client},
{Tesla.Middleware.Headers,
[
{"Authorization", "Bearer " <> config.api_key},
{"User-Agent", "knocklabs/knock-elixir@#{library_version()}"}
]}
] ++ maybe_idempotency_key_header(Map.new(opts))}
]

Tesla.client(middleware, config.adapter)
end

defp maybe_idempotency_key_header(%{idempotency_key: key}) when is_binary(key),
do: [{"Idempotency-Key", key}]

defp maybe_idempotency_key_header(_), do: []
end
9 changes: 6 additions & 3 deletions lib/knock/resources/workflows.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ defmodule Knock.Workflows do
Executes a notify call for the workflow with the given key.

Note: properties must contain at least `recipents` for the call to be valid.

Options can include:
* `idempotency_key`: A unique key to prevent duplicate requests
"""
@spec trigger(Knock.Client.t(), String.t(), map()) :: Api.response()
def trigger(client, key, properties) do
Api.post(client, "/workflows/#{key}/trigger", properties)
@spec trigger(Knock.Client.t(), String.t(), map(), keyword()) :: Api.response()
def trigger(client, key, properties, options \\ []) do
Api.post(client, "/workflows/#{key}/trigger", properties, options)
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Knock.MixProject do
use Mix.Project

@source_url "https://github.com/knocklabs/knock-elixir"
@version "0.4.6"
@version "0.4.7"

def project do
[
Expand Down