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

Add default config in config.exs + ability to configure Tesla middleware #71

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions lib/ex_force.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ defmodule ExForce do
end
```

Configure default settings in config/config.exs (optional).

```elixir
# config/config.exs

config :ex_force, :api_version, "43.0"
config :ex_force, :adapter, {Tesla.Adapter.Hackney, [recv_timeout: 1_000]}
config :ex_force, :middleware, [
Tesla.Middleware.Telemetry,
{Tesla.Middleware.Timeout, timeout: :timer.seconds(1)}
]
```

Copy link
Owner

Choose a reason for hiding this comment

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

I'd like to avoid adding package level config like this. This can be done by having own application config and wrapper around ExForce.

Copy link
Author

Choose a reason for hiding this comment

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

Many Elixir libraries support application-level config. This is optional and can be overridden for each client if need be.

Copy link
Author

Choose a reason for hiding this comment

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

Updated to:

# config/config.exs

config :ex_force, ExForce.Client.Tesla,
  api_version: "43.0",
  adapter: {Tesla.Adapter.Hackney, [recv_timeout: 1_000]},
  append_middleware: [
    Tesla.Middleware.Telemetry,
    {Tesla.Middleware.Timeout, timeout: :timer.seconds(1)}
  ]

Check out [Choosing a Tesla Adapter](https://github.com/chulkilee/ex_force/wiki/Choosing-a-Tesla-Adapter).

## Usage
Expand Down
58 changes: 36 additions & 22 deletions lib/ex_force/client/tesla/tesla.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,24 @@ 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, get_headers(opts)}
],
Keyword.get(opts, :adapter)
)
custom_middleware = get_from_opts_or_config(opts, :middleware, [])
Copy link
Owner

Choose a reason for hiding this comment

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

This middleware is always appended - so it would be better to make that explicit. What would be the better option name? append_middleware?

Copy link
Author

Choose a reason for hiding this comment

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

Sure - I can change that name and also only scope it to ExForce.Client.Tesla.

Copy link
Author

Choose a reason for hiding this comment

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

Updated!


adapter = get_from_opts_or_config(opts, :adapter)

headers = get_headers(opts)

[
{ExForce.Client.Tesla.Middleware,
{instance_url, get_from_opts_or_config(opts, :api_version, @default_api_version)}},
{Tesla.Middleware.Compression, format: "gzip"},
{Tesla.Middleware.JSON, engine: Jason},
{Tesla.Middleware.Headers, headers},
custom_middleware
]
|> List.flatten()
|> Tesla.client(adapter)
Copy link
Owner

Choose a reason for hiding this comment

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

Let's avoid using pipe here as we're not transforming a data here.

We can just ++ by enforcing passing list

  [
    {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, headers},
  ] ++ Keyword.get(opts, :append_middlewares, [])

Copy link
Author

Choose a reason for hiding this comment

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

Sure - will update.

Copy link
Author

Choose a reason for hiding this comment

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

Updated.

end

defp get_headers(opts), do: Keyword.get(opts, :headers, [{"user-agent", @default_user_agent}])

Copy link
Owner

Choose a reason for hiding this comment

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

Please refrain from moving this function around - as they're defined next to where its first use (not grouped by private funcs).

Copy link
Author

Choose a reason for hiding this comment

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

Sure - I'll move this back.

Copy link
Author

Choose a reason for hiding this comment

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

Moved back.

@doc """
Returns a `Tesla` client for `ExForce.OAuth` functions

Expand All @@ -65,16 +69,20 @@ defmodule ExForce.Client.Tesla do
"""
@impl ExForce.Client
def build_oauth_client(instance_url, opts \\ []) do
Tesla.client(
[
{Tesla.Middleware.BaseUrl, instance_url},
{Tesla.Middleware.Compression, format: "gzip"},
Tesla.Middleware.FormUrlencoded,
{Tesla.Middleware.DecodeJson, engine: Jason},
{Tesla.Middleware.Headers, get_headers(opts)}
],
Keyword.get(opts, :adapter)
)
custom_middleware = get_from_opts_or_config(opts, :middleware, [])

adapter = get_from_opts_or_config(opts, :adapter)

[
{Tesla.Middleware.DecodeJson, engine: Jason},
Copy link
Owner

Choose a reason for hiding this comment

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

Do we need to change this order?

Copy link
Author

Choose a reason for hiding this comment

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

Changing this order will fix #70

{Tesla.Middleware.BaseUrl, instance_url},
{Tesla.Middleware.Compression, format: "gzip"},
Tesla.Middleware.FormUrlencoded,
{Tesla.Middleware.Headers, get_headers(opts)},
custom_middleware
]
|> List.flatten()
|> Tesla.client(adapter)
end

@doc """
Expand All @@ -87,6 +95,12 @@ defmodule ExForce.Client.Tesla do
|> cast_response()
end

defp get_from_opts_or_config(opts, key, default \\ nil),
do: Keyword.get(opts, key) || Application.get_env(:ex_force, key) || default

defp get_headers(opts),
do: get_from_opts_or_config(opts, :headers, [{"user-agent", @default_user_agent}])

defp cast_tesla_request(%Request{} = request) do
request
|> convert_struct(Tesla.Env)
Expand Down
63 changes: 63 additions & 0 deletions test/config_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
defmodule ExForce.ConfigTest do
# Must be set to async: false since this is manipulating global application config
use ExUnit.Case, async: false

alias ExForce.{
Client,
Request
}

alias Plug.Conn

test "build_client/2 - custom config" do
bypass = Bypass.open()
bypass_url = "http://127.0.0.1:#{bypass.port}"

api_version = "456.0"

custom_middleware = [
Tesla.Middleware.Telemetry,
{Tesla.Middleware.Timeout, timeout: :timer.seconds(1)}
]

custom_middleware_representation = [
{Tesla.Middleware.Telemetry, :call, [[]]},
{Tesla.Middleware.Timeout, :call, [[timeout: 1000]]}
]

custom_opts = [
api_version: api_version,
middleware: custom_middleware
]

Application.put_all_env(ex_force: custom_opts)

on_exit(fn ->
Enum.each(custom_opts, fn {key, _} -> Application.delete_env(:ex_force, key) end)
end)

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.build_client(bypass_url)

assert %Tesla.Client{
adapter: nil,
fun: nil,
post: [],
pre:
[
{ExForce.Client.Tesla.Middleware, :call, [{^bypass_url, ^api_version}]},
{Tesla.Middleware.Compression, :call, [[format: "gzip"]]},
{Tesla.Middleware.JSON, :call, [[engine: Jason]]},
{Tesla.Middleware.Headers, :call, [[{"user-agent", "ex_force"}]]}
] ++ ^custom_middleware_representation
} = client

assert {:ok, %{status: 200, body: %{"hello" => "world"}}} =
Client.request(client, %Request{url: "/foo", method: :get})
end
end
2 changes: 1 addition & 1 deletion test/ex_force/oauth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ defmodule ExForce.OAuthTest do
fun: nil,
post: [],
pre: [
{Tesla.Middleware.DecodeJson, :call, [[engine: Jason]]},
{Tesla.Middleware.BaseUrl, :call, ["http://257.0.0.0:0"]},
{Tesla.Middleware.Compression, :call, [[format: "gzip"]]},
{Tesla.Middleware.FormUrlencoded, :call, [[]]},
{Tesla.Middleware.DecodeJson, :call, [[engine: Jason]]},
{Tesla.Middleware.Headers, :call, [[{"user-agent", "agent"}]]}
]
}
Expand Down
4 changes: 2 additions & 2 deletions test/ex_force_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ defmodule ExForceTest do

defp map_sobject_id(enum), do: Enum.map(enum, fn %SObject{id: id} -> id end)

defp get(client, url), do: Client.request(client, %Request{url: url, method: :get})

test "build_client/2 - map", %{bypass: bypass} do
Bypass.expect_once(bypass, "GET", "/", fn conn ->
conn
Expand Down Expand Up @@ -1201,6 +1203,4 @@ defmodule ExForceTest do
"account6"
]
end

defp get(client, url), do: Client.request(client, %Request{url: url, method: :get})
end