Skip to content

Commit 0e280f0

Browse files
committed
Make Mailtrap client dynamic
1 parent 28a4071 commit 0e280f0

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/mailtrap.ex

+15-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,19 @@ defmodule Mailtrap do
55

66
use Tesla
77

8-
plug(Mailtrap.DirectResponse)
9-
plug(Tesla.Middleware.JSON)
10-
plug(Tesla.Middleware.PathParams)
11-
plug(Tesla.Middleware.BaseUrl, "https://mailtrap.io/api")
12-
plug(Tesla.Middleware.BearerAuth, token: Application.get_env(:mailtrap, :api_token))
8+
@doc """
9+
Generates client
10+
"""
11+
@spec client(String.t()) :: Tesla.Client.t()
12+
def client(token) do
13+
middleware = [
14+
Mailtrap.DirectResponse,
15+
Tesla.Middleware.JSON,
16+
Tesla.Middleware.PathParams,
17+
{Tesla.Middleware.BaseUrl, "https://mailtrap.io/api"},
18+
{Tesla.Middleware.BearerAuth, token: token}
19+
]
20+
21+
Tesla.client(middleware)
22+
end
1323
end

test/mailtrap_test.exs

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ defmodule Mailtrap.APITest do
88
json(%{error: "Incorrect API token"}, status: 401)
99
end)
1010

11-
assert {:error, %Tesla.Env{status: 401, body: body}} = Mailtrap.get("accounts")
11+
client = Mailtrap.client("")
12+
assert {:error, %Tesla.Env{status: 401, body: body}} = Mailtrap.get(client, "accounts")
1213
assert %{"error" => "Incorrect API token"} == body
1314
end
1415

@@ -18,7 +19,9 @@ defmodule Mailtrap.APITest do
1819
json([%{"access_levels" => [100], "id" => 11_111, "name" => "John Doe"}])
1920
end)
2021

22+
client = Mailtrap.client("API_TOKEN")
23+
2124
assert {:ok, [%{"name" => "John Doe", "id" => 11_111, "access_levels" => [100]}]} =
22-
Mailtrap.get("accounts")
25+
Mailtrap.get(client, "accounts")
2326
end
2427
end

0 commit comments

Comments
 (0)