Kantoor.Karta.Client behaviour (Kantoor v1.0.0)

Behaviour and dispatcher for Kantoor's calls to Karta (/kantoor/v1).

The shapes follow Karta's own description of the interface (its guide guides/operations/kantoor.md); docs/karta-contract.md lists them and what was checked.

Operations:

  • fetch_raw/2 GET /kantoor/v1/messages/:id/raw (optional Range)
  • delete_delivery/1 DELETE /kantoor/v1/deliveries/:id
  • list_domains/0 GET /kantoor/v1/domains
  • send_raw/2 POST /kantoor/v1/messages/raw (data, retention, source=kantoor; auto_reply: true for the null envelope sender)
  • message/1 GET /kantoor/v1/messages/:id (metadata)
  • health/0 GET /kantoor/v1/health
  • attach_domain/2 POST /kantoor/v1/endpoints then POST /kantoor/v1/routes (one HTTP endpoint and one * route for the domain; both idempotent)
  • set_route_mode/2 POST /kantoor/v1/routes with takeover: true (mode: endpoint | hold); Karta's PATCH on a route changes retention only
  • domain_routes/1 GET /kantoor/v1/domains: the domain's routes as Karta lists them (address, mode, endpoint), with the domain's own key
  • put_route/4 POST /kantoor/v1/routes for one address: creates it, or takes an existing route at that address over when it already points at the same endpoint

A refusal with a code in Karta's error body is {:error, {:refused, status, code, detail}}; {:http, status} is an answer with no code, or not_found.

The implementation module is Kantoor.Karta.ReqClient unless config :kantoor, Kantoor.Karta, client: Mod says otherwise. Credentials come from Kantoor.Karta.Settings; the API key is never logged or inspected.

Summary

Functions

Creates the Karta endpoint (this instance's inbound URL, option :url) and the * route that delivers the domain to it. domain is the listing entry ("id", "name"). Registering again changes nothing, so a failed attach can be repeated.

Deletes this Kantoor's delivery (idempotent). Option :domain for per-domain keys.

The routes Karta lists at domain, with the domain's own key. Each route is a map with at least "local_part", "mode" and "endpoint_id".

Fetches the raw RFC822 bytes. Option :range is {first, last} (inclusive); :domain names the mail domain when the domain has its own Karta key.

Karta health/contract version.

Lists the domains Kantoor may attach to.

One message's metadata (size, subject, addresses, deliveries).

Creates the route local_part@domain to the endpoint in opts[:endpoint_id], or takes an existing route at that address over when it already points there (opts[:takeover], default true). Options: :endpoint_id, :takeover, :spam_mode (default "mark"), :spam_threshold, :dmarc_reject, :fail_closed, :plus_addressing.

Sends a raw MIME message. Options: :mail_from, :rcpt_to (list), :retention ("keep" or days, default "keep").

Switches a domain's route to :endpoint (deliver) or :hold (keep in Karta, do not push). route is %{domain: name, endpoint_id: id}.

Types

error()

@type error() ::
  :not_configured
  | {:http, pos_integer()}
  | {:refused, pos_integer(), String.t(), String.t() | nil}
  | {:transport, Exception.t()}
  | term()

Callbacks

attach_domain(domain, opts)

@callback attach_domain(domain :: map(), opts :: keyword()) ::
  {:ok,
   %{
     :route_id => String.t() | nil,
     :endpoint_id => String.t() | nil,
     optional(:secret) => String.t() | nil
   }}
  | {:error, error()}

delete_delivery(delivery_id, opts)

@callback delete_delivery(delivery_id :: String.t(), opts :: keyword()) ::
  :ok | {:error, error()}

domain_routes(domain)

@callback domain_routes(domain :: String.t()) :: {:ok, [map()]} | {:error, error()}

fetch_raw(message_id, opts)

@callback fetch_raw(message_id :: String.t(), opts :: keyword()) ::
  {:ok, binary()} | {:error, error()}

health()

@callback health() :: {:ok, map()} | {:error, error()}

list_domains()

@callback list_domains() :: {:ok, [map()]} | {:error, error()}

message(message_id)

@callback message(message_id :: String.t()) :: {:ok, map()} | {:error, error()}

put_route(domain, local_part, mode, opts)

@callback put_route(
  domain :: String.t(),
  local_part :: String.t(),
  mode :: atom(),
  opts :: keyword()
) ::
  {:ok, String.t(), :created | :existing} | {:error, error()}

send_raw(raw, opts)

@callback send_raw(raw :: binary(), opts :: keyword()) :: {:ok, map()} | {:error, error()}

set_route_mode(route, mode)

@callback set_route_mode(route :: Kantoor.MailSource.route(), mode :: :endpoint | :hold) ::
  :ok | {:error, error()}

Functions

attach_domain(domain, opts \\ [])

Creates the Karta endpoint (this instance's inbound URL, option :url) and the * route that delivers the domain to it. domain is the listing entry ("id", "name"). Registering again changes nothing, so a failed attach can be repeated.

delete_delivery(delivery_id, opts \\ [])

Deletes this Kantoor's delivery (idempotent). Option :domain for per-domain keys.

domain_routes(domain)

The routes Karta lists at domain, with the domain's own key. Each route is a map with at least "local_part", "mode" and "endpoint_id".

fetch_raw(message_id, opts \\ [])

Fetches the raw RFC822 bytes. Option :range is {first, last} (inclusive); :domain names the mail domain when the domain has its own Karta key.

health()

Karta health/contract version.

list_domains()

Lists the domains Kantoor may attach to.

message(message_id)

One message's metadata (size, subject, addresses, deliveries).

put_route(domain, local_part, mode, opts \\ [])

Creates the route local_part@domain to the endpoint in opts[:endpoint_id], or takes an existing route at that address over when it already points there (opts[:takeover], default true). Options: :endpoint_id, :takeover, :spam_mode (default "mark"), :spam_threshold, :dmarc_reject, :fail_closed, :plus_addressing.

Returns {:ok, route_id, :created | :existing} (:existing covers both a route that already matched and one taken over).

send_raw(raw, opts \\ [])

Sends a raw MIME message. Options: :mail_from, :rcpt_to (list), :retention ("keep" or days, default "keep").

set_route_mode(route, mode)

Switches a domain's route to :endpoint (deliver) or :hold (keep in Karta, do not push). route is %{domain: name, endpoint_id: id}.