Jido.Signal.Dispatch.Http (Jido v1.1.0-rc.2)

View Source

An adapter for dispatching signals via HTTP requests using Erlang's built-in :httpc client.

This adapter implements the Jido.Signal.Dispatch.Adapter behaviour and provides functionality to send signals as HTTP requests to specified endpoints. It uses the built-in :httpc client to avoid external dependencies.

Configuration Options

  • :url - (required) The URL to send the request to
  • :method - (optional) HTTP method to use, one of [:post, :put, :patch], defaults to :post
  • :headers - (optional) List of headers to include in the request
  • :timeout - (optional) Request timeout in milliseconds, defaults to 5000
  • :retry - (optional) Retry configuration map with keys:
    • :max_attempts - Maximum number of retry attempts (default: 3)
    • :base_delay - Base delay between retries in milliseconds (default: 1000)
    • :max_delay - Maximum delay between retries in milliseconds (default: 5000)

Examples

# Basic POST request
config = {:http, [
  url: "https://api.example.com/events",
]}

# Custom configuration
config = {:http, [
  url: "https://api.example.com/events",
  method: :put,
  headers: [{"content-type", "application/json"}, {"x-api-key", "secret"}],
  timeout: 10_000,
  retry: %{
    max_attempts: 5,
    base_delay: 2000,
    max_delay: 10000
  }
]}

Error Handling

The adapter handles these error conditions:

  • :invalid_url - The URL is not valid
  • :connection_error - Failed to establish connection
  • :timeout - Request timed out
  • :retry_failed - All retry attempts failed
  • Other HTTP status codes and errors

Summary

Functions

Delivers a signal via HTTP request.

Validates the HTTP adapter configuration options.

Types

delivery_error()

@type delivery_error() ::
  :invalid_url
  | :connection_error
  | :timeout
  | :retry_failed
  | {:status_error, pos_integer()}
  | term()

delivery_opts()

@type delivery_opts() :: [
  url: String.t(),
  method: http_method(),
  headers: [header()],
  timeout: pos_integer(),
  retry: retry_config()
]

header()

@type header() :: {String.t(), String.t()}

http_method()

@type http_method() :: :post | :put | :patch

retry_config()

@type retry_config() :: %{
  max_attempts: pos_integer(),
  base_delay: pos_integer(),
  max_delay: pos_integer()
}

Functions

deliver(signal, opts)

@spec deliver(Jido.Signal.t(), delivery_opts()) :: :ok | {:error, delivery_error()}

Delivers a signal via HTTP request.

Parameters

  • signal - The signal to deliver
  • opts - Validated options from validate_opts/1

Returns

  • :ok - Signal was delivered successfully
  • {:error, reason} - Delivery failed with reason

Examples

iex> signal = %Jido.Signal{type: "user:created", data: %{id: 123}}
iex> Http.deliver(signal, [url: "https://api.example.com/events"])
:ok

validate_opts(opts)

@spec validate_opts(Keyword.t()) :: {:ok, Keyword.t()} | {:error, term()}

Validates the HTTP adapter configuration options.

Parameters

  • opts - Keyword list of options to validate

Options

  • :url - Must be a valid URL string
  • :method - Must be one of [:post, :put, :patch]
  • :headers - Must be a list of string tuples
  • :timeout - Must be a positive integer
  • :retry - Must be a valid retry configuration map

Returns

  • {:ok, validated_opts} - Options are valid
  • {:error, reason} - Options are invalid with reason