Skip to content

Commit 3afe965

Browse files
authored
feat(KNO-10406): add support for specifying a branch (#45)
1 parent 7e472a0 commit 3afe965

File tree

5 files changed

+91
-4
lines changed

5 files changed

+91
-4
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ Or you can pass it through when creating a client instance:
4949
knock_client = MyApp.Knock.client(api_key: "sk_12345")
5050
```
5151

52+
To use a branch, set the `branch` option in your configuration or client instance:
53+
54+
```elixir
55+
config :my_app, MyApp.Knock,
56+
api_key: "sk_12345"
57+
branch: "my-feature-branch"
58+
59+
# OR
60+
61+
knock_client = MyApp.Knock.client(api_key: "sk_12345", branch: "my-feature-branch")
62+
```
63+
64+
Alternatively, you can set it as an environment variable:
65+
66+
```bash
67+
KNOCK_BRANCH="my-feature-branch"
68+
```
69+
5270
## Usage
5371

5472
### Identifying users

lib/knock.ex

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,28 @@ defmodule Knock do
4949
json_client: JSX
5050
```
5151
52-
You can read more about the availble adapters in the [Tesla documentation](https://hexdocs.pm/tesla/readme.html#adapters)
52+
You can read more about the available adapters in the [Tesla documentation](https://hexdocs.pm/tesla/readme.html#adapters)
53+
54+
To use a branch, set the `branch` option in your configuration or client instance.
55+
56+
```elixir
57+
# config/runtime.exs
58+
59+
config :my_app, MyApp.KnockClient,
60+
api_key: "sk_12345",
61+
branch: "my-feature-branch"
62+
63+
# OR
64+
65+
knock_client = MyApp.Knock.client(api_key: "sk_12345", branch: "my-feature-branch")
66+
```
5367
"""
5468

5569
defmacro __using__(opts) do
5670
quote do
5771
@app_name Keyword.fetch!(unquote(opts), :otp_app)
5872
@api_key_env_var "KNOCK_API_KEY"
73+
@branch_env_var "KNOCK_BRANCH"
5974

6075
alias Knock.Client
6176

@@ -72,6 +87,7 @@ defmodule Knock do
7287
defp fetch_options(overrides) do
7388
Application.get_env(@app_name, __MODULE__, [])
7489
|> maybe_resolve_api_key()
90+
|> maybe_resolve_branch()
7591
|> Keyword.merge(overrides)
7692
end
7793

@@ -82,6 +98,14 @@ defmodule Knock do
8298
_ -> Keyword.put(opts, :api_key, System.get_env(@api_key_env_var))
8399
end
84100
end
101+
102+
defp maybe_resolve_branch(opts) do
103+
case Keyword.get(opts, :branch) do
104+
branch when is_binary(branch) -> opts
105+
{:system, var_name} -> Keyword.put(opts, :branch, System.get_env(var_name))
106+
_ -> Keyword.put(opts, :branch, System.get_env(@branch_env_var))
107+
end
108+
end
85109
end
86110
end
87111

lib/knock/api.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ defmodule Knock.Api do
9191
[
9292
{"Authorization", "Bearer " <> config.api_key},
9393
{"User-Agent", "knocklabs/knock-elixir@#{library_version()}"}
94-
] ++ maybe_idempotency_key_header(Map.new(opts))}
94+
] ++
95+
maybe_idempotency_key_header(Map.new(opts)) ++
96+
maybe_branch_header(config)}
9597
]
9698

9799
Tesla.client(middleware, config.adapter)
@@ -101,4 +103,9 @@ defmodule Knock.Api do
101103
do: [{"Idempotency-Key", to_string(key)}]
102104

103105
defp maybe_idempotency_key_header(_), do: []
106+
107+
defp maybe_branch_header(%{branch: branch}) when not is_nil(branch),
108+
do: [{"X-Knock-Branch", to_string(branch)}]
109+
110+
defp maybe_branch_header(_), do: []
104111
end

lib/knock/client.ex

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ defmodule Knock.Client do
77
```elixir
88
# Setup a client instance directly
99
client = Knock.Client.new(api_key: "sk_test_12345")
10+
11+
# With optional branch
12+
client = Knock.Client.new(api_key: "sk_test_12345", branch: "my-feature-branch")
1013
```
1114
"""
1215

1316
@enforce_keys [:api_key]
1417
defstruct host: "https://api.knock.app",
1518
api_key: nil,
19+
branch: nil,
1620
adapter: Tesla.Adapter.Hackney,
1721
json_client: Jason
1822

@@ -22,6 +26,7 @@ defmodule Knock.Client do
2226
@type t :: %__MODULE__{
2327
host: String.t(),
2428
api_key: String.t(),
29+
branch: String.t() | nil,
2530
adapter: atom(),
2631
json_client: atom()
2732
}
@@ -38,7 +43,7 @@ defmodule Knock.Client do
3843

3944
opts =
4045
opts
41-
|> Keyword.take([:host, :api_key, :adapter, :json_client])
46+
|> Keyword.take([:host, :api_key, :branch, :adapter, :json_client])
4247
|> Map.new()
4348
|> maybe_set_adapter_default()
4449

test/knock_test.exs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ defmodule KnockTest do
1414
assert knock.adapter == Tesla.Adapter.Hackney
1515
assert knock.json_client == Jason
1616
assert knock.host == "https://api.knock.app"
17+
assert knock.branch == nil
18+
end
19+
20+
test "it allows configuring a branch" do
21+
knock = TestClient.client(api_key: "sk_test_12345", branch: "my-feature-branch")
22+
23+
assert knock.api_key == "sk_test_12345"
24+
assert knock.branch == "my-feature-branch"
1725
end
1826

1927
test "it will default to reading the api key from env vars" do
@@ -24,7 +32,18 @@ defmodule KnockTest do
2432
assert knock.api_key == "sk_test_12345"
2533
end
2634

27-
test "it can read from application config" do
35+
test "it will default to reading the branch from env vars" do
36+
System.put_env("KNOCK_API_KEY", "sk_test_12345")
37+
System.put_env("KNOCK_BRANCH", "test-branch")
38+
39+
knock = TestClient.client()
40+
41+
assert knock.branch == "test-branch"
42+
43+
System.delete_env("KNOCK_BRANCH")
44+
end
45+
46+
test "it can read the api key from application config" do
2847
Application.put_env(:knock, KnockTest.TestClient,
2948
api_key: "sk_test_12345",
3049
foo: "bar"
@@ -35,6 +54,20 @@ defmodule KnockTest do
3554
assert knock.api_key == "sk_test_12345"
3655
end
3756

57+
test "it can read the branch from application config" do
58+
Application.put_env(:knock, KnockTest.TestClient,
59+
api_key: "sk_test_12345",
60+
branch: "config-branch"
61+
)
62+
63+
knock = TestClient.client()
64+
65+
assert knock.api_key == "sk_test_12345"
66+
assert knock.branch == "config-branch"
67+
68+
Application.delete_env(:knock, KnockTest.TestClient)
69+
end
70+
3871
test "if set, will use the Tesla default adapter if one is not provided" do
3972
Application.put_env(:tesla, :adapter, Tesla.Adapter.Hackney)
4073

0 commit comments

Comments
 (0)