PropertyDamage.Export.HTTPSpec (PropertyDamage v0.2.0)

View Source

Describes an HTTP call for export purposes.

This struct is used to map PropertyDamage commands to their HTTP representations, enabling the export of failure reports to standalone scripts (curl, Python, Elixir) that can reproduce the failure without the PropertyDamage framework.

Usage

Adapters can optionally implement http_spec/2 to provide HTTP specifications:

def http_spec(%CreateAccount{currency: currency}, _context) do
  %HTTPSpec{
    method: :post,
    path: "/api/accounts",
    body: %{currency: currency}
  }
end

def http_spec(%CreditAccount{account_ref: id, amount: amount}, _context) do
  %HTTPSpec{
    method: :post,
    path: "/api/accounts/:account_id/credit",
    path_params: %{account_id: id},
    body: %{amount: amount}
  }
end

Path Parameters

Use :param_name syntax in paths, with corresponding keys in path_params:

%HTTPSpec{
  path: "/api/accounts/:account_id/transactions/:tx_id",
  path_params: %{account_id: "acc_123", tx_id: "tx_456"}
}

This renders as: /api/accounts/acc_123/transactions/tx_456

Summary

Functions

Builds the full URL with resolved path and query parameters.

Checks if the spec has a request body.

Returns the HTTP method as an uppercase string.

Creates a new HTTPSpec struct.

Resolves path parameters in the path string.

Types

method()

@type method() :: :get | :post | :put | :patch | :delete | :head | :options

t()

@type t() :: %PropertyDamage.Export.HTTPSpec{
  body: map() | nil,
  headers: [{String.t(), String.t()}],
  method: method(),
  path: String.t(),
  path_params: map(),
  query_params: map()
}

Functions

build_url(spec, base_url)

@spec build_url(t(), String.t()) :: String.t()

Builds the full URL with resolved path and query parameters.

Examples

iex> spec = %HTTPSpec{path: "/accounts", query_params: %{page: 1, limit: 10}}
iex> HTTPSpec.build_url(spec, "http://localhost:4000")
"http://localhost:4000/accounts?limit=10&page=1"

has_body?(http_spec)

@spec has_body?(t()) :: boolean()

Checks if the spec has a request body.

method_string(http_spec)

@spec method_string(t()) :: String.t()

Returns the HTTP method as an uppercase string.

new(opts)

@spec new(keyword()) :: t()

Creates a new HTTPSpec struct.

Options

  • :method - HTTP method (required)
  • :path - URL path with optional :param placeholders (required)
  • :body - Request body as a map (optional)
  • :headers - Additional headers as keyword list or list of tuples (optional)
  • :path_params - Map of path parameter values (optional)
  • :query_params - Map of query string parameters (optional)

resolve_path(http_spec)

@spec resolve_path(t()) :: String.t()

Resolves path parameters in the path string.

Examples

iex> spec = %HTTPSpec{path: "/accounts/:id", path_params: %{id: "123"}}
iex> HTTPSpec.resolve_path(spec)
"/accounts/123"

iex> spec = %HTTPSpec{path: "/accounts/:id/tx/:tx_id", path_params: %{id: "a", tx_id: "b"}}
iex> HTTPSpec.resolve_path(spec)
"/accounts/a/tx/b"