-
Notifications
You must be signed in to change notification settings - Fork 350
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
Introduce Tesla.client/1 & Tesla.client/2 #244
Changes from 3 commits
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 |
---|---|---|
|
@@ -64,13 +64,18 @@ defmodule Tesla.Env do | |
end | ||
|
||
defmodule Tesla.Client do | ||
@type adapter :: atom | {atom, Tesla.Env.opts()} | ||
@type middleware :: atom | {atom, Tesla.Env.opts()} | ||
|
||
@type t :: %__MODULE__{ | ||
pre: Tesla.Env.stack(), | ||
post: Tesla.Env.stack() | ||
post: Tesla.Env.stack(), | ||
adapter: adapter | nil | ||
} | ||
defstruct fun: nil, | ||
pre: [], | ||
post: [] | ||
post: [], | ||
adapter: nil | ||
end | ||
|
||
defmodule Tesla.Middleware do | ||
|
@@ -86,9 +91,9 @@ defmodule Tesla.Middleware do | |
|
||
plug Tesla.Middleware.BaseUrl, "https://example.com" | ||
|
||
or inside tuple in case of dynamic middleware (`Tesla.build_client/2`) | ||
or inside tuple in case of dynamic middleware (`Tesla.client/1`) | ||
|
||
Tesla.build_client([{Tesla.Middleware.BaseUrl, "https://example.com"}]) | ||
Tesla.client([{Tesla.Middleware.BaseUrl, "https://example.com"}]) | ||
|
||
## Writing custom middleware | ||
|
||
|
@@ -298,13 +303,14 @@ defmodule Tesla do | |
|
||
defp prepare(module, %{pre: pre, post: post} = client, options) do | ||
env = struct(Env, options ++ [__module__: module, __client__: client]) | ||
stack = pre ++ module.__middleware__ ++ post ++ [effective_adapter(module)] | ||
stack = pre ++ module.__middleware__ ++ post ++ [effective_adapter(module, client)] | ||
{env, stack} | ||
end | ||
|
||
@doc false | ||
def effective_adapter(module) do | ||
with nil <- adapter_per_module_from_config(module), | ||
def effective_adapter(module, client \\ %Tesla.Client{}) do | ||
with nil <- client.adapter, | ||
nil <- adapter_per_module_from_config(module), | ||
nil <- adapter_per_module(module), | ||
nil <- adapter_from_config() do | ||
adapter_default() | ||
|
@@ -421,26 +427,63 @@ defmodule Tesla do | |
def put_body(%Env{} = env, body), do: %{env | body: body} | ||
|
||
@doc """ | ||
Dynamically build client from list of middlewares. | ||
Dynamically build client from list of middlewares and/or adapter. | ||
|
||
``` | ||
defmodule ExampleAPI do | ||
use Tesla | ||
# add dynamic middleware | ||
client = Tesla.client([{Tesla.Middleware.Headers, [{"authorization", token}]}]) | ||
Tesla.get(client, "/path") | ||
|
||
# configure adapter in runtime | ||
client = Tesla.client([], Tesla.Adapter.Hackney) | ||
client = Tesla.client([], {Tesla.Adapter.Hackney, pool: :my_pool) | ||
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. missing |
||
Tesla.get(client, "/path") | ||
|
||
# complete module example | ||
defmodule MyApi do | ||
# note there is no need for `use Tesla` | ||
|
||
@middleware [ | ||
{Tesla.Middleware.BaseUrl, "https://example.com"}, | ||
Tesla.Middleware.JSON, | ||
Tesla.Middleware.Logger | ||
] | ||
|
||
@adapter Tesla.Adapter.Hackney | ||
|
||
def new(opts) do | ||
# do any middleware manipulation you need | ||
middleware = [ | ||
{Tesla.Middleware.BasicAuth, username: opts[:username], password: opts[:password]} | ||
] ++ @middleware | ||
|
||
# allow configuring adapter in runtime | ||
adapter = opts[:adapter] || @adapter | ||
|
||
# use Tesla.client/2 to put it all together | ||
Tesla.client(middleware, adapter) | ||
end | ||
|
||
def new(token) do | ||
Tesla.build_client([ | ||
{Tesla.Middleware.Headers, [{"authorization", token}] | ||
]) | ||
def get_something(client, id) do | ||
# pass client directly to Tesla.get/2 | ||
Tesla.get(client, "/something/\#{id}") | ||
# ... | ||
end | ||
end | ||
|
||
client = ExampleAPI.new(token: "abc") | ||
client |> ExampleAPI.get("/me") | ||
client = MyApi.new(username: "admin", password: "secret") | ||
MyApi.get_something(client, 42) | ||
``` | ||
""" | ||
@since "1.2.0" | ||
@spec client([Tesla.Client.middleware()], Tesla.Client.adapter()) :: Tesla.Client.t() | ||
def client(middleware, adapter \\ nil), do: Tesla.Builder.client(middleware, [], adapter) | ||
|
||
@deprecated "Use client/1 or client/2 instead" | ||
def build_client(pre, post \\ []), do: Tesla.Builder.client(pre, post) | ||
|
||
def build_adapter(fun), do: Tesla.Builder.client([], [fn env, _next -> fun.(env) end]) | ||
@deprecated "Use client/1 or client/2 instead" | ||
def build_adapter(fun), do: Tesla.Builder.client([], [], fun) | ||
|
||
def build_url(url, []), do: url | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,7 +169,9 @@ defmodule Tesla.Builder do | |
end | ||
end | ||
|
||
def client(pre, post), do: %Tesla.Client{pre: runtime(pre), post: runtime(post)} | ||
def client(pre, post, adapter \\ nil) do | ||
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. I'd change it to something like: def client(pre, post) do
%Tesla.Client{pre: runtime(pre), post: runtime(post)}
end
def client(pre, post, adapter) do
%Tesla.Client{pre: runtime(pre), post: runtime(post), adapter: runtime(adapter)}
end and remove this line: https://github.com/teamon/tesla/pull/244/files#diff-a2c274aba9c954a50446941dc12ebd88R207 Otherwise |
||
%Tesla.Client{pre: runtime(pre), post: runtime(post), adapter: runtime(adapter)} | ||
end | ||
|
||
@default_opts [] | ||
|
||
|
@@ -202,6 +204,7 @@ defmodule Tesla.Builder do | |
Tesla.Migration.breaking_alias!(kind, name, caller) | ||
end | ||
|
||
defp runtime(nil), do: nil | ||
defp runtime(list) when is_list(list), do: Enum.map(list, &runtime/1) | ||
defp runtime({module, opts}) when is_atom(module), do: {module, :call, [opts]} | ||
defp runtime(fun) when is_function(fun), do: {:fn, fun} | ||
|
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.
Maybe
module
(instead ofatom
) is more descriptive here.