View Source LoggerTelegramBackend.HTTPClient behaviour (logger_telegram_backend v3.0.0)

Specifies the API for using a custom HTTP Client.

The default HTTP client is LoggerTelegramBackend.HTTPClient.Finch.

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

config :logger, LoggerTelegramBackend,
  client: MyHTTPClient

Example

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

defmodule MyHTTPClient do
  @behaviour LoggerTelegramBackend.HTTPClient

  @hackney_pool_name :logger_telegram_backend_pool

  @impl true
  def child_spec(opts) do
    :hackney_pool.child_spec(@hackney_pool_name, opts)
  end

  @impl true
  def request(method, url, headers, body, opts) do
    opts = Keyword.merge(opts, pool: @hackney_pool_name) ++ [:with_body]

    case :hackney.request(method, url, headers, body, opts) do
      {:ok, _status, _headers, _body} = result -> result
      {:error, _reason} = error -> error
    end
  end
end

Summary

Types

HTTP request or response body.

HTTP request or response headers.

HTTP request method.

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 or nil.

Should make an HTTP request to url with the given method, headers, body and req_opts.

Types

@type body() :: binary()

HTTP request or response body.

Link to this type

headers()

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

HTTP request or response headers.

Link to this type

method()

View Source (since 3.0.0)
@type method() :: atom()

HTTP request method.

Link to this type

pool_opts()

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

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

Link to this type

req_opts()

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

HTTP request options (set via :client_request_opts).

Link to this type

status()

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

HTTP response status.

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

HTTP request URL.

Callbacks

Link to this callback

child_spec(pool_opts)

View Source (since 3.0.0)
@callback child_spec(pool_opts()) :: Supervisor.child_spec() | nil

Should return a child specification to start the HTTP client or nil.

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

Link to this callback

request(method, url, headers, body, req_opts)

View Source (since 3.0.0)
@callback request(method(), url(), headers(), body(), req_opts()) ::
  {:ok, status(), headers(), body()} | {:error, term()}

Should make an HTTP request to url with the given method, headers, body and req_opts.