Skip to content

Commit

Permalink
Add ability to modify middleware via a configured callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
aglassman committed Sep 5, 2024
1 parent 6eb7f01 commit 9d3eebe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/knock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ defmodule Knock do
```elixir
config :my_app, Knock,
adapter: Tesla.Adapter.Finch,
json_client: JSX
json_client: JSX,
middleware_callback: & [ Tesla.Middleware.OpenTelemetry | &1]
```
You can read more about the availble adapters in the [Tesla documentation](https://hexdocs.pm/tesla/readme.html#adapters)
Expand Down
2 changes: 2 additions & 0 deletions lib/knock/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ defmodule Knock.Api do
] ++ maybe_idempotency_key_header(Map.new(opts))}
]

middleware = (config.middleware_callback || & &1).(middleware)

Tesla.client(middleware, config.adapter)
end

Expand Down
8 changes: 5 additions & 3 deletions lib/knock/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ defmodule Knock.Client do
defstruct host: "https://api.knock.app",
api_key: nil,
adapter: Tesla.Adapter.Hackney,
json_client: Jason
json_client: Jason,
middleware_callback: nil

@typedoc """
Describes a Knock client
Expand All @@ -23,7 +24,8 @@ defmodule Knock.Client do
host: String.t(),
api_key: String.t(),
adapter: atom(),
json_client: atom()
json_client: atom(),
middleware_callback: ([atom()] -> [atom()])
}

@doc """
Expand All @@ -36,7 +38,7 @@ defmodule Knock.Client do
raise Knock.ApiKeyMissingError
end

opts = Keyword.take(opts, [:host, :api_key, :adapter, :json_client])
opts = Keyword.take(opts, [:host, :api_key, :adapter, :json_client, :middleware_callback])
struct!(__MODULE__, opts)
end
end

0 comments on commit 9d3eebe

Please sign in to comment.