-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, []) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure - I can change that name and also only scope it to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure - will update. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}]) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure - I'll move this back. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved back. |
||
@doc """ | ||
Returns a `Tesla` client for `ExForce.OAuth` functions | ||
|
||
|
@@ -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}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to change this order? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 """ | ||
|
@@ -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) | ||
|
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to: