Skip to content

Commit 559e5d8

Browse files
committed
Add ability to modify middleware via a configured callback.
1 parent 6eb7f01 commit 559e5d8

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

lib/knock.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ defmodule Knock do
4646
```elixir
4747
config :my_app, Knock,
4848
adapter: Tesla.Adapter.Finch,
49-
json_client: JSX
49+
json_client: JSX,
50+
middleware_callback: &MyApp.Tesla.middleware_callback/1
5051
```
5152
5253
You can read more about the availble adapters in the [Tesla documentation](https://hexdocs.pm/tesla/readme.html#adapters)

lib/knock/api.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ defmodule Knock.Api do
8888
] ++ maybe_idempotency_key_header(Map.new(opts))}
8989
]
9090

91+
middleware = (config.middleware_callback || & &1).(middleware)
92+
9193
Tesla.client(middleware, config.adapter)
9294
end
9395

lib/knock/client.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ defmodule Knock.Client do
1414
defstruct host: "https://api.knock.app",
1515
api_key: nil,
1616
adapter: Tesla.Adapter.Hackney,
17-
json_client: Jason
17+
json_client: Jason,
18+
middleware_callback: nil
1819

1920
@typedoc """
2021
Describes a Knock client
@@ -23,7 +24,8 @@ defmodule Knock.Client do
2324
host: String.t(),
2425
api_key: String.t(),
2526
adapter: atom(),
26-
json_client: atom()
27+
json_client: atom(),
28+
middleware_callback: ([atom()] -> [atom()])
2729
}
2830

2931
@doc """
@@ -36,7 +38,7 @@ defmodule Knock.Client do
3638
raise Knock.ApiKeyMissingError
3739
end
3840

39-
opts = Keyword.take(opts, [:host, :api_key, :adapter, :json_client])
41+
opts = Keyword.take(opts, [:host, :api_key, :adapter, :json_client, :middleware_callback])
4042
struct!(__MODULE__, opts)
4143
end
4244
end

0 commit comments

Comments
 (0)