tesla v1.2.1 Tesla View Source

A HTTP toolkit for building API clients using middlewares

Building API client

use Tesla macro will generate basic http functions (e.g. get, post) inside your module. It supports following options:

  • :only - builder will generate only functions included in list given in this option
  • :except - builder won’t generate functions included in list given in this option
  • :docs - when set to false builder will won’t add documentation to generated functions

Example

defmodule ExampleApi do
  use Tesla, only: [:get], docs: false

  plug Tesla.Middleware.BaseUrl, "http://api.example.com"
  plug Tesla.Middleware.JSON

  def fetch_data do
    get("/data")
  end
end

In example above ExampleApi.fetch_data/0 is equivalent of ExampleApi.get("/data")

Direct usage

It is also possible to do request directly with Tesla module.

Tesla.get("https://example.com")

Common pitfalls

Direct usage won’t include any middlewares.

In following example:

defmodule ExampleApi do
  use Tesla, only: [:get], docs: false

  plug Tesla.Middleware.BaseUrl, "http://api.example.com"
  plug Tesla.Middleware.JSON

  def fetch_data do
    Tesla.get("/data")
  end
end

call to ExampleApi.fetch_data/0 will fail, because request will be missing base url.

Default adapter

By default Tesla is using Tesla.Adapter.Httpc, because :httpc is included in Erlang/OTP and doen not require installation of any additional dependency. It can be changed globally with config

config :tesla, :adapter, Tesla.Adapter.Hackney

or by Tesla.Builder.adapter/2 macro for given API client module

defmodule ExampleApi do
  use Tesla

  adapter Tesla.Adapter.Hackney

  ...
end

Link to this section Summary

Functions

build_adapter(fun) deprecated

Dynamically build client from list of middlewares and/or adapter

Perform a DELETE request

Perform a DELETE request

Perform a GET request

Perform a GET request

Returns value of header specified by key from :headers field in Tesla.Env

Perform a HEAD request

Perform a HEAD request

Perform a OPTIONS request

Perform a OPTIONS request

Perform a PATCH request

Perform a PATCH request

Perform a POST request

Perform a POST request

Perform a PUT request

Perform a PUT request

Adds given key/value pair to :opts field in Tesla.Env

Perform request and raise in case of error

Perform a TRACE request

Perform a TRACE request

Link to this section Types

Link to this type option() View Source
option() ::
  {:method, Tesla.Env.method()}
  | {:url, Tesla.Env.url()}
  | {:query, Tesla.Env.query()}
  | {:headers, Tesla.Env.headers()}
  | {:body, Tesla.Env.body()}
  | {:opts, Tesla.Env.opts()}

Link to this section Functions

This function is deprecated. Use client/1 or client/2 instead.
Link to this function build_client(pre, post \\ []) View Source
This function is deprecated. Use client/1 or client/2 instead.

Dynamically build client from list of middlewares and/or adapter.

# add dynamic middleware
client = Tesla.client([{Tesla.Middleware.Headers, [{"authorization", token}]}])
Tesla.get(client, "/path")

# configure adapter in runtime
client = Tesla.client([], Tesla.Adapter.Hackney)
client = Tesla.client([], {Tesla.Adapter.Hackney, pool: :my_pool})
Tesla.get(client, "/path")

# complete module example
defmodule MyApi do
  # note there is no need for `use Tesla`

  @middleware [
    {Tesla.Middleware.BaseUrl, "https://example.com"},
    Tesla.Middleware.JSON,
    Tesla.Middleware.Logger
  ]

  @adapter Tesla.Adapter.Hackney

  def new(opts) do
    # do any middleware manipulation you need
    middleware = [
      {Tesla.Middleware.BasicAuth, username: opts[:username], password: opts[:password]}
    ] ++ @middleware

    # allow configuring adapter in runtime
    adapter = opts[:adapter] || @adapter

    # use Tesla.client/2 to put it all together
    Tesla.client(middleware, adapter)
  end

  def get_something(client, id) do
    # pass client directly to Tesla.get/2
    Tesla.get(client, "/something/#{id}")
    # ...
  end
end

client = MyApi.new(username: "admin", password: "secret")
MyApi.get_something(client, 42)

Perform a DELETE request.

See request!/1 or request!/2 for options definition.

delete!("/users")
delete!("/users", query: [scope: "admin"])
delete!(client, "/users")
delete!(client, "/users", query: [scope: "admin"])

Perform a DELETE request.

See request/1 or request/2 for options definition.

delete("/users")
delete("/users", query: [scope: "admin"])
delete(client, "/users")
delete(client, "/users", query: [scope: "admin"])
Link to this function delete_header(env, key) View Source
delete_header(Tesla.Env.t(), binary()) :: Tesla.Env.t()

Perform a GET request.

See request!/1 or request!/2 for options definition.

get!("/users")
get!("/users", query: [scope: "admin"])
get!(client, "/users")
get!(client, "/users", query: [scope: "admin"])

Perform a GET request.

See request/1 or request/2 for options definition.

get("/users")
get("/users", query: [scope: "admin"])
get(client, "/users")
get(client, "/users", query: [scope: "admin"])
Link to this function get_header(env, key) View Source
get_header(Tesla.Env.t(), binary()) :: binary() | nil

Returns value of header specified by key from :headers field in Tesla.Env

Examples

# non existing header
iex> env = %Tesla.Env{headers: [{"server", "Cowboy"}]}
iex> Tesla.get_header(env, "some-key")
nil

# existing header
iex> env = %Tesla.Env{headers: [{"server", "Cowboy"}]}
iex> Tesla.get_header(env, "server")
"Cowboy"

# first of multiple headers with the same name
iex> env = %Tesla.Env{headers: [{"cookie", "chocolate"}, {"cookie", "biscuits"}]}
iex> Tesla.get_header(env, "cookie")
"chocolate"
Link to this function get_headers(env, key) View Source
get_headers(Tesla.Env.t(), binary()) :: [binary()]

Perform a HEAD request.

See request!/1 or request!/2 for options definition.

head!("/users")
head!("/users", query: [scope: "admin"])
head!(client, "/users")
head!(client, "/users", query: [scope: "admin"])

Perform a HEAD request.

See request/1 or request/2 for options definition.

head("/users")
head("/users", query: [scope: "admin"])
head(client, "/users")
head(client, "/users", query: [scope: "admin"])
Link to this function options!(client, url, opts) View Source

Perform a OPTIONS request.

See request!/1 or request!/2 for options definition.

options!("/users")
options!("/users", query: [scope: "admin"])
options!(client, "/users")
options!(client, "/users", query: [scope: "admin"])

Perform a OPTIONS request.

See request/1 or request/2 for options definition.

options("/users")
options("/users", query: [scope: "admin"])
options(client, "/users")
options(client, "/users", query: [scope: "admin"])

Perform a PATCH request.

See request!/1 or request!/2 for options definition.

patch!("/users", %{name: "Jon"})
patch!("/users", %{name: "Jon"}, query: [scope: "admin"])
patch!(client, "/users", %{name: "Jon"})
patch!(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

Perform a PATCH request.

See request/1 or request/2 for options definition.

patch("/users", %{name: "Jon"})
patch("/users", %{name: "Jon"}, query: [scope: "admin"])
patch(client, "/users", %{name: "Jon"})
patch(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

Perform a POST request.

See request!/1 or request!/2 for options definition.

post!("/users", %{name: "Jon"})
post!("/users", %{name: "Jon"}, query: [scope: "admin"])
post!(client, "/users", %{name: "Jon"})
post!(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

Perform a POST request.

See request/1 or request/2 for options definition.

post("/users", %{name: "Jon"})
post("/users", %{name: "Jon"}, query: [scope: "admin"])
post(client, "/users", %{name: "Jon"})
post(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

Perform a PUT request.

See request!/1 or request!/2 for options definition.

put!("/users", %{name: "Jon"})
put!("/users", %{name: "Jon"}, query: [scope: "admin"])
put!(client, "/users", %{name: "Jon"})
put!(client, "/users", %{name: "Jon"}, query: [scope: "admin"])

Perform a PUT request.

See request/1 or request/2 for options definition.

put("/users", %{name: "Jon"})
put("/users", %{name: "Jon"}, query: [scope: "admin"])
put(client, "/users", %{name: "Jon"})
put(client, "/users", %{name: "Jon"}, query: [scope: "admin"])
Link to this function put_header(env, key, value) View Source
put_header(Tesla.Env.t(), binary(), binary()) :: Tesla.Env.t()
Link to this function put_headers(env, list) View Source
put_headers(Tesla.Env.t(), [{binary(), binary()}]) :: Tesla.Env.t()
Link to this function put_opt(env, key, value) View Source
put_opt(Tesla.Env.t(), atom(), any()) :: Tesla.Env.t()

Adds given key/value pair to :opts field in Tesla.Env

Useful when there’s need to store additional middleware data in Tesla.Env

Example

iex> %Tesla.Env{opts: []} |> Tesla.put_opt(:option, "value")
%Tesla.Env{opts: [option: "value"]}
Link to this function request!(client \\ %Tesla.Client{}, options) View Source
request!(Tesla.Env.client(), [option()]) :: Tesla.Env.t() | no_return()

Perform request and raise in case of error.

This is similar to request/2 behaviour from Tesla 0.x

See request/2 for list of available options.

Link to this function request(client \\ %Tesla.Client{}, options) View Source

Perform a request

Options:

  • :method - the request method, one of [:head, :get, :delete, :trace, :options, :post, :put, :patch]
  • :url - either full url e.g. “http://example.com/some/path” or just “/some/path” if using Tesla.Middleware.BaseUrl
  • :query - a keyword list of query params, e.g. [page: 1, per_page: 100]
  • :headers - a keyworld list of headers, e.g. [{"content-type", "text/plain"}]
  • :body - depends on used middleware:

    • by default it can be a binary
    • if using e.g. JSON encoding middleware it can be a nested map
    • if adapter supports it it can be a Stream with any of the above
  • :opts - custom, per-request middleware or adapter options

Examples:

ExampleApi.request(method: :get, url: "/users/path")

You can also use shortcut methods like:

ExampleApi.get("/users/1")

or

ExampleApi.post(client, "/users", %{name: "Jon"})
Link to this function run_default_adapter(env, opts \\ []) View Source

Perform a TRACE request.

See request!/1 or request!/2 for options definition.

trace!("/users")
trace!("/users", query: [scope: "admin"])
trace!(client, "/users")
trace!(client, "/users", query: [scope: "admin"])

Perform a TRACE request.

See request/1 or request/2 for options definition.

trace("/users")
trace("/users", query: [scope: "admin"])
trace(client, "/users")
trace(client, "/users", query: [scope: "admin"])