tesla v0.5.2 Tesla.Builder

Summary

Macros

Choose adapter for your API client

Attach middleware to your API client

Macros

adapter(adapter)

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
adapter(adapter, opts \\ nil)
plug(middleware, opts \\ nil)

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