Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Tesla middleware to be configured #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions lib/ex_force/client/tesla/tesla.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule ExForce.Client.Tesla do
- `:headers`: set additional headers; default: `[{"user-agent", "#{@default_user_agent}"}]`
- `:api_version`: use the given api_version; default: `"#{@default_api_version}"`
- `:adapter`: use the given adapter with custom opts; default: `nil`, which causes Tesla to use the default adapter or the one set in config.
- `:middleware`: set additional middleware to use in the Tesla Client
"""
@impl ExForce.Client
def build_client(instance_url_or_map, opts \\ [headers: [{"user-agent", @default_user_agent}]])
Expand All @@ -42,16 +43,18 @@ defmodule ExForce.Client.Tesla do
end

def build_client(instance_url, opts) when is_binary(instance_url) do
Tesla.client(
[
{ExForce.Client.Tesla.Middleware,
{instance_url, Keyword.get(opts, :api_version, @default_api_version)}},
{Tesla.Middleware.Compression, format: "gzip"},
{Tesla.Middleware.JSON, engine: Jason},
{Tesla.Middleware.Headers, Keyword.get(opts, :headers, [])}
],
Keyword.get(opts, :adapter)
)
additional_middleware = Keyword.get(opts, :middleware, [])

middleware = [
{ExForce.Client.Tesla.Middleware,
{instance_url, Keyword.get(opts, :api_version, @default_api_version)}},
additional_middleware,
{Tesla.Middleware.Compression, format: "gzip"},
{Tesla.Middleware.JSON, engine: Jason},
{Tesla.Middleware.Headers, Keyword.get(opts, :headers, [])}
] |> List.flatten

Tesla.client(middleware, Keyword.get(opts, :adapter))
end

@doc """
Expand Down
53 changes: 53 additions & 0 deletions test/ex_force/client/tesla_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
defmodule ExForce.Client.TeslaTest do
use ExUnit.Case, async: false
alias ExForce.{Client, Request}
alias Plug.Conn

defmodule TestMiddleware do
@behaviour Tesla.Middleware

@impl Tesla.Middleware
def call(env, next, test_pid: test_pid, id: id) when is_pid(test_pid) do
send(test_pid, {:before, id})
result = Tesla.run(env, next)
send(test_pid, {:after, id})

result
end
end

setup do
%{bypass: Bypass.open()}
end

test "it will use the custom middleware provided to it", %{bypass: bypass} do
Bypass.expect_once(bypass, "GET", "/foo", fn conn ->
conn
|> Conn.put_resp_content_type("application/json")
|> Conn.resp(200, ~w({"hello": "world"}))
end)

client =
ExForce.Client.Tesla.build_client(%{instance_url: bypass_url(bypass), access_token: "foo"},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested modules could be aliased at the top of the invoking module.

middleware: [
{TestMiddleware, test_pid: self(), id: 1},
{TestMiddleware, test_pid: self(), id: 2},
{TestMiddleware, test_pid: self(), id: 3}
]
)

{:ok, _response} = Client.request(client, %Request{url: "/foo", method: :get})

assert {:messages,
[
before: 1,
before: 2,
before: 3,
after: 3,
after: 2,
after: 1
]} = Process.info(self(), :messages)
end

defp bypass_url(bypass), do: "http://127.0.0.1:#{bypass.port}"
end