tesla v1.2.1 Tesla.Adapter behaviour View Source
The adapter specification
Adapter is a module that denormalize request data stored in Tesla.Env
in order to make
request with lower level http client (e.g. :httpc
or :hackney
) and normalize response data
in order to store it back to Tesla.Env
. It has to export call/2
function.
Writing custom adapter
call/2
params:
- env -
Tesla.Env
struct that stores request/response data - options - middleware options provided by user
Example
defmodule MyProject.CustomAdapter do
alias Tesla.Multipart
@behaviour Tesla.Adapter
@override_defaults [follow_redirect: false]
@impl true
def call(env, opts) do
opts = Tesla.Adapter.opts(@override_defaults, env, opts)
with {:ok, {status, headers, body}} <- request(env.method, env.body, env.headers, opts) do
{:ok, normalize_response(env, status, headers, body)}
end
end
defp request(_method, %Stream{}, _headers, _opts) do
{:error, "stream not supported by adapter"}
end
defp request(_method, %Multipart{}, _headers, _opts) do
{:error, "multipart not supported by adapter"}
end
defp request(method, body, headers, opts) do
:lower_level_http.request(method, body, denormalize_headers(headers), opts)
end
defp denormalize_headers(headers), do: ...
defp normalize_response(env, status, headers, body), do: %Tesla.Env{env | ...}
end
Link to this section Summary
Functions
Helper function that merges all adapter options
Link to this section Functions
Link to this function
opts(defaults \\ [], env, opts)
View Source
opts(Keyword.t(), Tesla.Env.t(), Keyword.t()) :: Keyword.t()
Helper function that merges all adapter options
Params:
defaults
(optional) - useful to override lower level http client default configurationenv
-Tesla.Env
structopts
- options provided toTesla.Builder.adapter/2
macro
Precedence rules:
- config from
opts
overrides config fromdefaults
when same key is encountered - config from
env
overrides config from bothdefaults
andopts
when same key is encountered
Link to this section Callbacks
Link to this callback
call(env, options)
View Source
call(env :: Tesla.Env.t(), options :: any()) :: Tesla.Env.result()