FritzApi.HTTPClient behaviour (fritz_api v3.1.0)

Copy Markdown View Source

Specifies the API for using a custom HTTP Client.

The default HTTP client is FritzApi.HTTPClient.Finch.

To configure a different HTTP client, implement the FritzApi.HTTPClient behaviour and change the :client configuration:

config :fritz_api, :client, MyHTTPClient

Example

A client implementation based on :hackney could look like this:

defmodule MyHTTPClient do
  @behaviour FritzApi.HTTPClient

  @pool_name :my_http_client_pool

  @impl true
  def child_spec(pool_opts) do
    :hackney_pool.child_spec(@pool_name, pool_opts)
  end

  @impl true
  def get(url, req_opts) do
    opts = [:with_body, pool: @pool_name] ++ req_opts

    case :hackney.get(url, [], "", opts) do
      {:ok, _status, _headers, _body} = result -> result
      {:error, _reason} = error -> error
    end
  end
end

Summary

Types

HTTP response body.

HTTP response headers.

Options to configure the pool (set via :client_pool_opts).

HTTP request options (set via :client_request_opts).

HTTP response status.

HTTP request URL.

Callbacks

Should return a child specification to start the HTTP client, a list of them, or nil if the client needs no supervised process.

Should make an HTTP request to url/0 with the given req_opts/0.

Types

body()

(since 3.0.0)
@type body() :: binary()

HTTP response body.

headers()

(since 3.0.0)
@type headers() :: [{String.t(), String.t()}]

HTTP response headers.

pool_opts()

(since 3.0.0)
@type pool_opts() :: Keyword.t()

Options to configure the pool (set via :client_pool_opts).

req_opts()

(since 3.0.0)
@type req_opts() :: Keyword.t()

HTTP request options (set via :client_request_opts).

status()

(since 3.0.0)
@type status() :: 100..599

HTTP response status.

url()

(since 3.0.0)
@type url() :: String.t()

HTTP request URL.

Callbacks

child_spec(pool_opts)

(since 3.0.0)
@callback child_spec(pool_opts()) ::
  Supervisor.child_spec() | [Supervisor.child_spec()] | nil

Should return a child specification to start the HTTP client, a list of them, or nil if the client needs no supervised process.

For example, this can start a pool of HTTP connections dedicated to FritzApi.

get(url, req_opts)

(since 3.0.0)
@callback get(url(), req_opts()) :: {:ok, status(), headers(), body()} | {:error, term()}

Should make an HTTP request to url/0 with the given req_opts/0.