Karta.Sending.Options (Karta v1.0.0)

What a message sent over an API may carry besides its content, checked in one place for every API (each API reads its own syntax and gives the values here):

  • check/2: tags (at most 10, each 1 to 128 bytes of text; the first is the tag when none is given), variables (a map with string keys, at most 16 KiB as JSON), a time to send (a DateTime at most 72 hours ahead; a time in the past means at once) and tracking switches (%{opens: false, clicks: false} switch that tracking off; true or nil leave the server's setting). Karta.Sending runs it on every message.
  • text_variables/2: variables given as text with an API's own limits (Postmark's Metadata).
  • The batch limit: batch_limit/0, the most messages one batch request may make in any API (config :karta, Karta.Sending.Options, batch_limit:, which KARTA_BATCH_MAX_MESSAGES sets through parse_batch_limit!/1; 100 by default, at most 1000), and make_batch/2, which makes a batch request's messages one at a time and counts their content (at most 50 MiB together), so that a request over either is refused before it is made whole.

Summary

Functions

The most messages one batch request may make, in every API that sends batches (one message per recipient or per personalization, or the messages of a batch): config :karta, Karta.Sending.Options, batch_limit:, which KARTA_BATCH_MAX_MESSAGES sets; 100 when not set.

Checks the options of a message and gives them as they are kept: {:ok, %{tag, tags, variables, send_at, track_opens, track_clicks, requiretls}} (requiretls is true only when fields[:require_tls] is true: REQUIRETLS, RFC 8689, as on the submission listeners), or {:error, :invalid_tags | :invalid_variables | :invalid_send_at | :send_at_too_late | :invalid_tracking}. fields has :tag, :tags, :variables, :send_at and :tracking as Karta.Sending takes them; opts may give :now, the time a :send_at is judged against (default: the clock).

Makes the messages of a batch request one at a time, with fun.(item) for each item ({:ok, fields}, or an error, which is returned as it is), and counts their content as they are made: the subject, the text, the HTML, the attachments and the inline parts of every message, at most 50 MiB together. So a request is refused before it is made whole: {:error, :too_many_messages} when it has more items than batch_limit/0 (before any is made), {:error, :batch_too_large} as soon as the content passes 50 MiB. The fields in order otherwise.

The batch limit as KARTA_BATCH_MAX_MESSAGES gives it: a whole number from 1 to 1000, or 100 when the variable is not set. Raises ArgumentError for anything else, so that a release does not start with a limit it cannot read.

Variables kept as text, with an API's limits: at most max of them, names of 1 to name characters without control characters, values of at most value characters. A number or a boolean is kept as its JSON text (3 becomes "3"); nil is no variables. {:ok, variables}, or {:error, :too_many} or {:error, :invalid}.

Functions

batch_limit()

@spec batch_limit() :: pos_integer()

The most messages one batch request may make, in every API that sends batches (one message per recipient or per personalization, or the messages of a batch): config :karta, Karta.Sending.Options, batch_limit:, which KARTA_BATCH_MAX_MESSAGES sets; 100 when not set.

check(fields, opts \\ [])

@spec check(map(), keyword()) :: {:ok, map()} | {:error, atom()}

Checks the options of a message and gives them as they are kept: {:ok, %{tag, tags, variables, send_at, track_opens, track_clicks, requiretls}} (requiretls is true only when fields[:require_tls] is true: REQUIRETLS, RFC 8689, as on the submission listeners), or {:error, :invalid_tags | :invalid_variables | :invalid_send_at | :send_at_too_late | :invalid_tracking}. fields has :tag, :tags, :variables, :send_at and :tracking as Karta.Sending takes them; opts may give :now, the time a :send_at is judged against (default: the clock).

make_batch(items, fun)

@spec make_batch([term()], (term() -> {:ok, map()} | term())) ::
  {:ok, [map()]} | term()

Makes the messages of a batch request one at a time, with fun.(item) for each item ({:ok, fields}, or an error, which is returned as it is), and counts their content as they are made: the subject, the text, the HTML, the attachments and the inline parts of every message, at most 50 MiB together. So a request is refused before it is made whole: {:error, :too_many_messages} when it has more items than batch_limit/0 (before any is made), {:error, :batch_too_large} as soon as the content passes 50 MiB. The fields in order otherwise.

parse_batch_limit!(value)

@spec parse_batch_limit!(String.t() | nil) :: pos_integer()

The batch limit as KARTA_BATCH_MAX_MESSAGES gives it: a whole number from 1 to 1000, or 100 when the variable is not set. Raises ArgumentError for anything else, so that a release does not start with a limit it cannot read.

text_variables(variables, limits)

@spec text_variables(map() | nil, keyword()) ::
  {:ok, map()} | {:error, :too_many | :invalid}

Variables kept as text, with an API's limits: at most max of them, names of 1 to name characters without control characters, values of at most value characters. A number or a boolean is kept as its JSON text (3 becomes "3"); nil is no variables. {:ok, variables}, or {:error, :too_many} or {:error, :invalid}.