Restlax.Client (Restlax v0.1.0) View Source
Rest Client builder
Options
:adapter
-module()
One of the Tesla Adapters or your own customized adapter:adapter_opts
-keyword()
options for the adapter:logger_opts
-keyword()
options forTesla.Middleware.Logger
:base_url
-String.t()
Base URL, e.g. "https://api.cloudflare.com/client/v4":encoding
-encoding()
:json
or:form_urlencoded
:encoding_opts
-keyword()
options forTesla.Middleware.JSON
orTesla.Middle.FormUrlencoded
:headers
-[{String.t(), String.t()}]
Default headers, can be overridden per request
Example
defmodule MyClient do
use Restlax.Client,
base_url: "https://my-awesome.app/api/v1"
adapter: Tesla.Adapter.Hackney
end
Note: You may pick an adapter directly like in the above code. However, it's preferred to not pick one if your API client is a library. Leaving it out allows the users of your library to choose one for themselves.
For example, if your users already use Mint in their code base, they can use this configuration
config :tesla, Cloudflare.Client, adapter: Tesla.Adapter.Mint
to make the Cloudflare API client use the Mint adapter of Tesla and avoid adding another dependency
Customization
Feel free to add more middlewares like so
defmodule MyApp.Auth do
@behaviour Tesla.Middleware
@impl Tesla.Middleware
def call(env, next, _) do
auth_token = env.opts[:auth_token] || Application.get_env(:my_app, :auth_token)
headers = auth_token && [{"authorization", "Bearer #{auth_token}"}]) || []
Tesla.run(%{env | headers: headers ++ env.headers}, next)
end
end
defmodule MyApp.MyClient do
use Restlax.Client,
base_url: "https://my-awesome.app/api/v1"
adapter: Tesla.Adapter.Hackney
plug MyApp.Auth
end
Link to this section Summary
Link to this section Types
Specs
encoding() :: :json | :form_url_encoded | :raw