tesla v0.10.0 Tesla.Builder View Source

Link to this section Summary

Functions

Choose adapter for your API client

Attach middleware to your API client

Link to this section Functions

Link to this macro adapter(adapter) View Source (macro)

Choose adapter for your API client

defmodule ExampleApi do
  use Tesla

  # set adapter as module
  adapter Tesla.Adapter.Hackney

  # set adapter as function
  adapter :local_adapter

  # set adapter as anonymous function
  adapter fn env ->
    ...
    env
  end


  # adapter function gets Tesla.Env as parameter and must return Tesla.Env
  def local_adapter(env) do
    ...
    env
  end
end
Link to this macro adapter(adapter, opts \\ nil) View Source (macro)
Link to this macro plug(middleware, opts \\ nil) View Source (macro)

Attach middleware to your API client

defmodule ExampleApi do
  use Tesla

  # plug middleware module with options
  plug Tesla.Middleware.BaseUrl, "http://api.example.com"
  plug Tesla.Middleware.JSON, engine: Poison

  # plug middleware function
  plug :handle_errors

  # middleware function gets two parameters: Tesla.Env and the rest of middleware call stack
  # and must return Tesla.Env
  def handle_errors(env, next) do
    env
    |> modify_env_before_request
    |> Tesla.run(next)            # run the rest of stack
    |> modify_env_after_request
  end
end