Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F113895
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/tesla/client.ex b/lib/tesla/client.ex
index 61cad21..598056f 100644
--- a/lib/tesla/client.ex
+++ b/lib/tesla/client.ex
@@ -1,14 +1,50 @@
defmodule Tesla.Client do
@type adapter :: module | {module, any} | (Tesla.Env.t() -> Tesla.Env.result())
@type middleware :: module | {module, any}
@type t :: %__MODULE__{
pre: Tesla.Env.stack(),
post: Tesla.Env.stack(),
adapter: adapter | nil
}
defstruct fun: nil,
pre: [],
post: [],
adapter: nil
+
+ @doc ~S"""
+ Returns the client's adapter in the same form it was provided.
+ This can be used to copy an adapter from one client to another.
+
+ ## Example
+
+ iex> client = Tesla.client([], {Tesla.Adapter.Hackney, [recv_timeout: 30_000]})
+ iex> Tesla.Client.adapter(client)
+ {Tesla.Adapter.Hackney, [recv_timeout: 30_000]}
+ """
+ @spec adapter(t) :: adapter
+ def adapter(client) do
+ unruntime(client.adapter)
+ end
+
+ @doc ~S"""
+ Returns the client's middleware in the same form it was provided.
+ This can be used to copy middleware from one client to another.
+
+ ## Example
+
+ iex> middleware = [Tesla.Middleware.JSON, {Tesla.Middleware.BaseUrl, "https://api.github.com"}]
+ iex> client = Tesla.client(middleware)
+ iex> Tesla.Client.middleware(client)
+ [Tesla.Middleware.JSON, {Tesla.Middleware.BaseUrl, "https://api.github.com"}]
+ """
+ @spec middleware(t) :: [middleware]
+ def middleware(client) do
+ unruntime(client.pre)
+ end
+
+ defp unruntime(list) when is_list(list), do: Enum.map(list, &unruntime/1)
+ defp unruntime({module, :call, [[]]}) when is_atom(module), do: module
+ defp unruntime({module, :call, [opts]}) when is_atom(module), do: {module, opts}
+ defp unruntime({:fn, fun}) when is_function(fun), do: fun
end
diff --git a/test/tesla/client_test.exs b/test/tesla/client_test.exs
index 8bfda1a..fc2ba5f 100644
--- a/test/tesla/client_test.exs
+++ b/test/tesla/client_test.exs
@@ -1,4 +1,46 @@
defmodule Tesla.ClientTest do
use ExUnit.Case
doctest Tesla.Client
+
+ describe "Tesla.Client.adapter/1" do
+ test "converts atom adapter properly" do
+ adapter = Tesla.Adapter.Httpc
+
+ client = Tesla.client([], adapter)
+
+ assert adapter == Tesla.Client.adapter(client)
+ end
+
+ test "converts tuple adapter properly" do
+ adapter = {Tesla.Adapter.Hackney, [recv_timeout: 30_000]}
+
+ client = Tesla.client([], adapter)
+
+ assert adapter == Tesla.Client.adapter(client)
+ end
+
+ test "converts function adapter properly" do
+ adapter = fn env ->
+ {:ok, %{env | body: "new"}}
+ end
+
+ client = Tesla.client([], adapter)
+
+ assert adapter == Tesla.Client.adapter(client)
+ end
+ end
+
+ describe "Tesla.Client.middleware/1" do
+ test "converts middleware properly" do
+ middlewares = [
+ FirstMiddleware,
+ {SecondMiddleware, options: :are, fun: 1},
+ fn env, _next -> env end
+ ]
+
+ client = Tesla.client(middlewares)
+
+ assert middlewares == Tesla.Client.middleware(client)
+ end
+ end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 25, 3:16 PM (1 d, 5 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39945
Default Alt Text
(3 KB)
Attached To
Mode
R28 tesla
Attached
Detach File
Event Timeline
Log In to Comment