Current section

Files

Jump to
salemove_http_client lib salemove http_client adapter.ex
Raw

lib/salemove/http_client/adapter.ex

defmodule Salemove.HttpClient.Adapter do
@moduledoc """
Custom Tesla adapter which allows to specify adapter in request options.
It also allows configuring adapter at runtime, using, for example, environment variables.
## Example
defmodule Search do
use Salemove.HttpClient, base_url: "http://www.google.com/"
end
Search.get("/", adapter: Tesla.Adapter.Finch)
"""
@doc false
def call(%{opts: opts} = env, _opts) do
adapter = Keyword.fetch!(opts, :__adapter)
adapter_options = Keyword.get(opts, :__adapter_options, [])
env
|> adapter.call(adapter_options)
|> normalize_error()
end
defp normalize_error({:error, %{__struct__: Finch.TransportError, reason: reason}}) do
{:error, reason}
end
defp normalize_error(result), do: result
end