Packages
tesla
1.14.0
1.20.0
1.18.3
1.18.2
1.18.1
1.18.0
1.17.0
1.16.0
1.15.3
1.15.2
1.15.1
1.15.0
1.14.3
1.14.2
1.14.1
1.14.0
1.13.2
1.13.1
1.13.0
1.12.3
1.12.2
1.12.1
1.12.0
1.11.2
1.11.1
1.11.0
1.10.3
1.10.2
1.10.1
1.10.0
1.9.0
1.8.1
retired
1.8.0
1.7.0
1.6.1
1.6.0
1.5.1
1.5.0
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.3
1.3.2
1.3.1
1.3.0
1.2.1
1.2.0
1.1.0
1.0.0
1.0.0-beta.1
0.10.0
0.9.0
0.8.0
0.7.2
0.7.1
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.2.2
0.2.1
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
HTTP client library, with support for middleware and multiple adapters.
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/tesla/client.ex
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: Tesla.Env.runtime() | 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.
## Examples
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
if client.adapter, 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.
## Examples
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