Assent.HTTPAdapter behaviour (Assent v0.4.0)

Copy Markdown View Source

HTTP adapter helper module.

You can configure which HTTP adapter Assent uses by setting the configuration:

http_adapter: Assent.HTTPAdapter.Httpc

Default options can be set by passing a list of options:

http_adapter: {Assent.HTTPAdapter.Httpc, [...]}

You can also set global application config:

config :assent, :http_adapter, Assent.HTTPAdapter.Httpc

Usage

defmodule MyApp.MyHTTPAdapter do
  @behaviour Assent.HTTPAdapter

  @impl true
  def request(method, url, body, headers, opts) do
    # Implement your HTTP request logic here using your preferred
    # HTTP client library. Return {:ok, response} or {:error, reason}.
  end
end

Summary

Callbacks

Sends an HTTP request using the adapter's client.

Functions

Decodes request response body.

Formats an HTTP request for an error message.

Formats an HTTP response for an error message.

Makes an HTTP request.

Sets a user agent header.

Types

body()

@type body() :: binary() | nil

headers()

@type headers() :: [{binary(), binary()}]

method()

@type method() :: :get | :post

Callbacks

request(method, binary, body, headers, t)

@callback request(method(), binary(), body(), headers(), Keyword.t()) ::
  {:ok, Assent.HTTPAdapter.HTTPResponse.t()} | {:error, term()}

Sends an HTTP request using the adapter's client.

Functions

decode_response(response, opts)

Decodes request response body.

Options

format_http_request(request)

@spec format_http_request(Assent.HTTPAdapter.HTTPRequest.t()) :: binary()

Formats an HTTP request for an error message.

format_http_response(response)

@spec format_http_response(Assent.HTTPAdapter.HTTPResponse.t()) :: binary()

Formats an HTTP response for an error message.

request(request, opts)

Makes an HTTP request.

Options

  • :http_adapter - HTTP adapter configuration. Defaults to {Assent.HTTPAdapter.Req, []}, {Assent.HTTPAdapter.Httpc, []} depending on which HTTP client is available in the project dependencies.

  • :json_library - the JSON library to use, see Assent.json_library/1

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

This function is deprecated. Use `request/2` instead. This will be removed in v1.0.0..

user_agent_header()

@spec user_agent_header() :: {binary(), binary()}

Sets a user agent header.

The header value will be Assent-VERSION with VERSION being the :vsn of :assent app.