Skip to content

Commit 972c35f

Browse files
authored
chore: default adapter to tesla default (#43)
1 parent 75c562b commit 972c35f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/knock/client.ex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,23 @@ defmodule Knock.Client do
3636
raise Knock.ApiKeyMissingError
3737
end
3838

39-
opts = Keyword.take(opts, [:host, :api_key, :adapter, :json_client])
39+
opts =
40+
opts
41+
|> Keyword.take([:host, :api_key, :adapter, :json_client])
42+
|> Map.new()
43+
|> maybe_set_adapter_default()
44+
4045
struct!(__MODULE__, opts)
4146
end
47+
48+
defp maybe_set_adapter_default(%{adapter: adapter} = opts) when not is_nil(adapter),
49+
do: opts
50+
51+
defp maybe_set_adapter_default(opts) do
52+
# Use the default adapter if one is not provided (if set using Tesla)
53+
case Application.get_env(:tesla, :adapter) do
54+
default when not is_nil(default) -> Map.put(opts, :adapter, default)
55+
_ -> opts
56+
end
57+
end
4258
end

test/knock_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,19 @@ defmodule KnockTest do
3434

3535
assert knock.api_key == "sk_test_12345"
3636
end
37+
38+
test "if set, will use the Tesla default adapter if one is not provided" do
39+
Application.put_env(:tesla, :adapter, Tesla.Adapter.Hackney)
40+
41+
knock = TestClient.client()
42+
assert knock.adapter == Tesla.Adapter.Hackney
43+
44+
Application.delete_env(:tesla, :adapter)
45+
end
46+
47+
test "it can set the adapter to a custom one" do
48+
knock = TestClient.client(adapter: Tesla.Adapter.Mint)
49+
assert knock.adapter == Tesla.Adapter.Mint
50+
end
3751
end
3852
end

0 commit comments

Comments
 (0)