salemove_http_client v0.2.0 Salemove.HttpClient

Generic HTTP client built on top of Tesla and used to build specific JSON API HTTP clients with ability to configure them during runtime, using, for example, environment variables.

Example

defmodule Github do
  use Salemove.HttpClient, base_url: "https://api.github.com/"

  def user_repos(login, opts \ []) do
    get("/user/" <> login <> "/repos", opts)
  end
end

Github.user_repos("take-five")

Configuration options

There are number of available configuration options:

  • :base_url - Base URL of service (including schema, i.e. https://api.github.com/)
  • :adapter - HTTP Adapter module, defaults to Tesla.Adapter.Hackney
  • :adapter_options - adapter specific options, see documentation for concrete adapter
  • :json - JSON encoding/decoding options. If omitted, default options are used - see Tesla.Middleware.JSON. If set to false, request body is sent as application/x-www-form-urlencoded not JSON.
  • :retry - Retry few times in case of connection refused error. See Tesla.Middleware.Retry.
  • :stats - StatsD instrumenting options. See Tesla.StatsD for more details.
  • :username - along with :password option adds basic authentication to all requests. See Tesla.Middleware.BasicAuth.
  • :password - see :username.
  • :log - Logging options, see Salemove.HttpClient.Middleware.Logger
  • :debug - Turn on/off verbose request/response logging, defaults to false

HTTP client can be configured at runtime and at compile time via configuration files. Note, that you can use {:system, env_name} tuples to configure the client

Configuration via request options

You can pass additional Keyword argument to request functions:

Github.user_repos("take-five", adapter: :mock, base_url: "http://mocked-gh/")

Configuration via config files

In config/config.exs:

config :salemove_http_client,
  adapter: :mock,
  base_url: "http://mocked-gh/"

Link to this section Summary

Types

Client or request-specific options

Link to this section Types

Link to this type options()
options() :: Keyword.t()

Client or request-specific options