Tesla.Adapter behaviour (tesla v1.4.0) 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 implement Tesla.Adapter.call/2.
Writing custom adapter
Create a module implementing Tesla.Adapter.call/2.
See Tesla.Adapter.call/2 for details.
Example
defmodule MyProject.CustomAdapter do
  alias Tesla.Multipart
  @behaviour Tesla.Adapter
  @override_defaults [follow_redirect: false]
  @impl Tesla.Adapter
  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 | ...}
endLink to this section Summary
Functions
Helper function that merges all adapter options.
Callbacks
Invoked when a request runs.
Link to this section Functions
Specs
opts(Keyword.t(), Tesla.Env.t(), Keyword.t()) :: Keyword.t()
Helper function that merges all adapter options.
Arguments
- defaults(optional) - useful to override lower level http client default configuration
- env-- Tesla.Envstruct
- opts- options provided to- Tesla.Builder.adapter/2macro
Precedence rules
- config from optsoverrides config fromdefaultswhen same key is encountered
- config from envoverrides config from bothdefaultsandoptswhen same key is encountered
Link to this section Callbacks
Specs
call(env :: Tesla.Env.t(), options :: any()) :: Tesla.Env.result()
Invoked when a request runs.
Arguments
- env-- Tesla.Envstruct that stores request/response data
- options- middleware options provided by user