# `Alternera.Plugin.Host`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_plugin_sdk/lib/alternera/plugin/host.ex#L1)

What the plugin SDK asks of the application around it. Resolving a
template needs two things only the running pipelines know: the tenant's
variables and the methods the tenant's pipelines allow for a request.
Reading a body needs the application's in-memory limit, and work on a
body loaded whole a claim on the application's memory. A call out of the
proxy (`Plugin.Provisional.Outbound`) goes through the application's forward
side, and a saved output's fingerprint (`Plugin.Provisional.SavedOutput`) is
keyed with the application's secret. A rate (`Plugin.Provisional.Rate`)
is counted by the application's counter, and a stamped secret reference
is shown unstamped by the application's rule (`Outbound.unstamp/1`). A
response cache step's lookup, single flight and fill
(`Plugin.Provisional.ResponseCache`) are the application's response
cache. The credentials a request is
sent with (`Plugin.Provisional.Credentials`) and the signed-in caller an
upstream is told of (`Plugin.Provisional.Identity`) are the
application's. A step's settings checked against the
flow (`Plugin.Provisional.Checks`) are checked with the application's own
conditions and cache vocabulary. A file a user uploads into a step
(`Plugin.Provisional.Files`) is kept in the application's storage. A map is written as YAML
(`write_yaml/1`) by the application's writer. A resource record is read by
its lookup key (`resource_get/3`) and a resource's action is run
(`resource_run/5`), both optional callbacks, by the application's
resources. The SDK asks
through this behaviour, so it names no module of the application. The
application names the implementation under `:host`:

    config :alternera_plugin_sdk, :host, MyApp.PluginHost

Without one the SDK uses `Alternera.Plugin.Test.Host`, the stand-in a
plugin's own tests run on.

# `allowed_method_list`

```elixir
@callback allowed_method_list(map() | nil) :: [String.t()]
```

# `allowed_methods`

```elixir
@callback allowed_methods(map()) :: String.t()
```

# `authorization`

```elixir
@callback authorization(map(), String.t() | nil, String.t() | nil) ::
  {:ok,
   %{
     value: String.t(),
     profile: String.t(),
     directives: map(),
     outbound_us: non_neg_integer()
   }}
  | {:token_request, String.t()}
  | {:error, String.t()}
  | {:error, String.t(), non_neg_integer()}
```

# `body_inspect_max_bytes`

```elixir
@callback body_inspect_max_bytes(String.t() | nil) :: pos_integer() | nil
```

# `cache_get`

```elixir
@callback cache_get(String.t(), term()) :: {:ok, term()} | :miss
```

# `cache_key_reads_body?`

```elixir
@callback cache_key_reads_body?(term()) :: boolean()
```

# `cache_put`

```elixir
@callback cache_put(String.t(), term(), term(), pos_integer()) :: :ok
```

# `cache_settings_errors`

```elixir
@callback cache_settings_errors(map()) :: [String.t()]
```

# `call_authorization`

```elixir
@callback call_authorization(map(), String.t(), keyword()) ::
  {:ok,
   %{value: String.t(), profile: String.t(), outbound_us: non_neg_integer()}}
  | {:error, String.t()}
  | {:error, String.t(), non_neg_integer()}
```

# `caller_claims`

```elixir
@callback caller_claims(map(), String.t(), pos_integer()) :: {:ok, map()} | :anonymous
```

# `claim_held_bytes`

```elixir
@callback claim_held_bytes(non_neg_integer(), pos_integer() | nil) ::
  {:ok, term()} | {:error, :busy | :size, String.t()}
```

# `claim_refusal`

```elixir
@callback claim_refusal(:busy | :size, atom()) :: %{
  :type =&gt; String.t(),
  :status =&gt; pos_integer(),
  optional(:retry_after_ms) =&gt; pos_integer()
}
```

# `close_body_spool`

```elixir
@callback close_body_spool(term()) ::
  {:ok, Alternera.Plugin.Body.t()} | {:error, :spool_failed, String.t()}
```

# `condition_errors`

```elixir
@callback condition_errors([term()], :request | :response) :: [String.t()]
```

# `discard_body_spool`

```elixir
@callback discard_body_spool(term()) :: :ok
```

# `error_map`

```elixir
@callback error_map(term()) :: %{required(String.t()) =&gt; term()}
```

# `fingerprint_key`

```elixir
@callback fingerprint_key() :: binary()
```

# `in_memory_hold_max_bytes`

```elixir
@callback in_memory_hold_max_bytes() :: pos_integer()
```

# `inline_threshold_bytes`

```elixir
@callback inline_threshold_bytes() :: non_neg_integer()
```

# `max_header_size_bytes`

```elixir
@callback max_header_size_bytes() :: pos_integer()
```

# `open_body_spool`

```elixir
@callback open_body_spool(term(), String.t()) :: {:ok, term()} | {:error, String.t()}
```

# `outbound_call`

```elixir
@callback outbound_call(map(), map(), keyword()) ::
  {:ok, map()}
  | {:error, String.t(), String.t()}
  | {:error, String.t(), String.t(), map()}
```

# `plugin_file_get`

```elixir
@callback plugin_file_get(String.t() | nil, String.t(), String.t(), :info | :bytes) ::
  {:ok, map() | binary()} | :not_found | {:error, String.t()}
```

# `plugin_file_put`

```elixir
@callback plugin_file_put(String.t(), String.t(), binary(), map(), keyword()) ::
  {:ok, %{digest: String.t(), size: non_neg_integer(), new?: boolean()}}
  | {:error, :quota, %{used: non_neg_integer(), limit: non_neg_integer()}}
  | {:error, String.t()}
```

# `plugin_files_list`

```elixir
@callback plugin_files_list(String.t(), String.t()) ::
  {:ok, [map()]} | {:error, String.t()}
```

# `plugin_files_reclaim`

```elixir
@callback plugin_files_reclaim(String.t()) ::
  {:ok, %{removed: non_neg_integer(), bytes: non_neg_integer()}}
  | {:error, String.t()}
```

# `plugin_files_used`

```elixir
@callback plugin_files_used(String.t(), String.t()) ::
  {:ok, non_neg_integer()} | {:error, String.t()}
```

# `rate_check`

```elixir
@callback rate_check(String.t() | nil, String.t(), String.t(), map()) ::
  :ok | {:refuse, pos_integer()}
```

# `read_tenant_vars`

```elixir
@callback read_tenant_vars(String.t(), %{plain: [String.t()], secret: [String.t()]}) ::
  {:ok, %{required(String.t()) =&gt; String.t()}}
  | {:error, [String.t()]}
  | :unreadable
```

# `read_timeout_ms`

```elixir
@callback read_timeout_ms(String.t() | nil) :: pos_integer()
```

# `recorded_body`

```elixir
@callback recorded_body(map(), :request | :response) ::
  {:ok, binary()} | {:error, :none} | {:error, String.t()}
```

# `release_held_bytes`

```elixir
@callback release_held_bytes(term()) :: :ok
```

# `remove_body_spool`

```elixir
@callback remove_body_spool(term(), String.t()) :: :ok
```

# `resolve_tenant_vars!`

```elixir
@callback resolve_tenant_vars!(String.t(), map() | nil, keyword(), (String.t() -&gt;
                                                            String.t())) ::
  String.t()
```

# `resource_get`
*optional* 

```elixir
@callback resource_get(String.t(), String.t(), term()) ::
  {:ok, map()} | :not_found | {:error, term()}
```

# `resource_run`
*optional* 

```elixir
@callback resource_run(String.t(), String.t(), String.t(), map(), keyword()) ::
  {:ok, term()} | {:error, term()}
```

# `response_cache_give_up`

```elixir
@callback response_cache_give_up(term()) :: :ok
```

# `response_cache_keep`

```elixir
@callback response_cache_keep(term(), map(), {non_neg_integer() | nil, term()}) ::
  {:keep, {module(), term()}, map()} | {:bypass, String.t(), map()}
```

# `response_cache_key_template`

```elixir
@callback response_cache_key_template(String.t()) :: term()
```

# `response_cache_look_up`

```elixir
@callback response_cache_look_up(map(), map(), map()) ::
  {:hit, map(), map()}
  | {:fill, term(), map()}
  | {:miss, String.t(), map()}
  | {:bypass, String.t(), map()}
```

# `reveal_secret`

```elixir
@callback reveal_secret(String.t(), map()) :: {:ok, String.t()} | {:error, String.t()}
```

# `saved_body_max_bytes`

```elixir
@callback saved_body_max_bytes() :: pos_integer()
```

# `script_cache_max_entries`

```elixir
@callback script_cache_max_entries(String.t() | nil) :: pos_integer()
```

# `sign_caller`

```elixir
@callback sign_caller(map(), map()) :: {:ok, String.t()} | {:error, String.t()}
```

# `store_delete`

```elixir
@callback store_delete(String.t(), String.t(), String.t(), keyword()) ::
  {:ok, boolean()} | {:error, atom()}
```

# `store_entries`

```elixir
@callback store_entries(String.t(), String.t(), keyword()) ::
  {:ok, [map()], %{truncated: boolean()}} | {:error, atom()}
```

# `store_get`

```elixir
@callback store_get(String.t(), String.t(), String.t()) ::
  {:ok, map()} | :miss | {:error, atom()}
```

# `store_invalidate`

```elixir
@callback store_invalidate(
  map(),
  term(),
  %{required(String.t()) =&gt; String.t()},
  keyword()
) ::
  {:ok, %{deleted: boolean(), key: String.t()}} | :unresolved | {:error, atom()}
```

# `store_plain?`

```elixir
@callback store_plain?(term()) :: boolean()
```

# `store_put`

```elixir
@callback store_put(String.t(), String.t(), String.t(), term(), keyword()) ::
  :ok | {:error, atom()}
```

# `store_secret?`

```elixir
@callback store_secret?(String.t(), String.t()) :: boolean()
```

# `store_update`

```elixir
@callback store_update(String.t(), String.t(), String.t(), term(), keyword()) ::
  {:ok, term(), %{created: boolean()}} | {:error, atom()}
```

# `store_valid_name?`

```elixir
@callback store_valid_name?(term()) :: boolean()
```

# `tenant_id_for_slug`

```elixir
@callback tenant_id_for_slug(String.t()) ::
  {:ok, String.t()} | {:error, :not_found | :disabled | :not_started | term()}
```

# `tenant_var_names`

```elixir
@callback tenant_var_names(String.t() | nil) :: %{
  plain: [String.t()],
  secret: [String.t()]
}
```

# `tenant_var_references?`

```elixir
@callback tenant_var_references?(String.t()) :: boolean()
```

# `tenant_var_value`

```elixir
@callback tenant_var_value(:plain | :secret, String.t(), map() | nil, keyword()) ::
  {:ok, String.t()} | {:error, String.t()}
```

# `tenant_var_values`

```elixir
@callback tenant_var_values(String.t() | nil, %{plain: [String.t()], secret: [String.t()]}) ::
  {%{required(String.t()) =&gt; String.t()}, [String.t()]}
```

# `unstamp`

```elixir
@callback unstamp(String.t()) :: String.t()
```

# `write_body_spool`

```elixir
@callback write_body_spool(term(), iodata()) ::
  {:ok, term()} | {:error, :disk_budget | :spool_failed, String.t()}
```

# `write_yaml`

```elixir
@callback write_yaml(map()) :: {:ok, String.t()} | {:error, String.t()}
```

# `allowed_methods`

```elixir
@spec allowed_methods(map()) :: String.t()
```

The methods the tenant's pipelines allow for `request`, as an `Allow` header lists them.

# `body_inspect_max_bytes`

```elixir
@spec body_inspect_max_bytes(String.t() | nil) :: pos_integer() | nil
```

The most bytes of a body the tenant's pipelines inspect
(`body_inspect_max_bytes`), or nil for the application's default. What a
step keeps of a body for an error (`Plugin.Error.max_body_bytes/1`) is
bounded by it.

# `claim_held_bytes`

```elixir
@spec claim_held_bytes(non_neg_integer(), pos_integer() | nil) ::
  {:ok, term()} | {:error, :busy | :size, String.t()}
```

Claims room in the application's memory, in the calling process, for
work on `input_bytes` of bodies and values loaded whole: a JSON path
read of a spooled body (`Plugin.Location.read/2`), a plugin that runs in
a heap of its own (the Script sandbox). The application claims `factor`
bytes per byte (`Plugin.memory_factor/1`; nil for its default) and waits
a moment for room. `{:error, :busy, message}`
when it has none now, `{:error, :size, message}` when the claim could
never fit; the claim goes back with `release_held_bytes/1`.

# `claim_refusal`

```elixir
@spec claim_refusal(:busy | :size, atom()) :: %{
  :type =&gt; String.t(),
  :status =&gt; pos_integer(),
  optional(:retry_after_ms) =&gt; pos_integer()
}
```

The step error a refused claim of `kind` (`claim_held_bytes/2`) is in
`phase`, as the application answers its own refusals: its `type`
(`memory_limit` for the application's load, `body_too_large` for a
claim that could never fit), its `status` and, for a request the
application turned away for its load, when to try again
(`retry_after_ms`).

# `close_body_spool`

```elixir
@spec close_body_spool(term()) ::
  {:ok, Alternera.Plugin.Body.t()} | {:error, :spool_failed, String.t()}
```

The body a spool of `open_body_spool/2` holds, its file closed: inline or a file.

# `discard_body_spool`

```elixir
@spec discard_body_spool(term()) :: :ok
```

Gives a spool of `open_body_spool/2` up: its file and its disk claim go.

# `in_memory_hold_max_bytes`

```elixir
@spec in_memory_hold_max_bytes() :: pos_integer()
```

The most bytes of a body the application holds in memory at once.

# `inline_threshold_bytes`

```elixir
@spec inline_threshold_bytes() :: non_neg_integer()
```

The size over which the application holds a body on disk rather than in
memory, and over which a step on it runs in a process of its own under
a heap limit and is metered (`Provisional.HeapPeak`).

# `open_body_spool`

```elixir
@spec open_body_spool(term(), String.t()) :: {:ok, term()} | {:error, String.t()}
```

A new spool for a body step that writes the body it leaves a piece at a
time (the Script step run per record), in the calling process: in
memory up to the exchange's spool threshold or 64 KiB, whichever is
lower (what a step keeps there is its own claim's to count), past it in a file of the
step's own beside the held body's, which the application's disk budget
for held bodies claims as it grows. `spool` is the step's
`context.body_spool` (nil outside a body phase that spools: there is
none to open, `{:error, message}`), `name` a tag unique to the step's
run, which `remove_body_spool/2` takes too. The spool is written
(`write_body_spool/2`) and closed (`close_body_spool/1`) in the process
that opened it; the body the close gives is the step's to return as
its body, and the application releases it once the exchange no longer
needs it. A spool given up is `discard_body_spool/1`ed.

# `release_held_bytes`

```elixir
@spec release_held_bytes(term()) :: :ok
```

Gives back a claim of `claim_held_bytes/2`.

# `remove_body_spool`

```elixir
@spec remove_body_spool(term(), String.t()) :: :ok
```

Removes what is left of a spool `open_body_spool(spool, name)` opened in
a process that ended before it closed or discarded it (a call killed at
its time or memory limit), with its disk claim.

# `resolve_tenant_vars!`

```elixir
@spec resolve_tenant_vars!(String.t(), map() | nil, keyword(), (String.t() -&gt;
                                                            String.t())) ::
  String.t()
```

`text` with its tenant variable references resolved against `scope`,
`literal` applied to the text around them. Raises on a reference that does
not resolve.

# `resource_get`

```elixir
@spec resource_get(String.t(), String.t(), term()) ::
  {:ok, map()} | :not_found | {:error, term()}
```

The record of resource `name` (`"<package>.<resource>"`, spec 069) whose
lookup key is `key`, in `tenant_id`: `{:ok, record}`, the record's public
attributes as a map with string keys and JSON-ready values; `:not_found`
when the tenant has no such record; or `{:error, reason}`:
`:unknown_resource` (no such resource in this build), `:no_lookup_key`
(the resource has no lookup key), `:unlicensed` (the licence does not
include resources) or the reason a read failed, which the application
logs. An optional callback: a host without it answers `{:error,
:unsupported}`.

# `resource_run`

```elixir
@spec resource_run(String.t(), String.t(), String.t(), map(), keyword()) ::
  {:ok, term()} | {:error, term()}
```

Runs the generic action `action` of resource `name` (`"<package>.<resource>"`)
in `tenant_id` with `input` (argument name to value) on the pipeline's
behalf: `{:ok, result}`, the result JSON-ready (string keys, decimals and
dates as text), or `{:error, reason}`: `:unknown_resource`,
`{:unknown_action, open}` (no such action open to the API; `open` lists
the ones that are), `:unlicensed`, `{:invalid, message}` (the action
refused the input or failed, the message says why) or the reason a run
failed, which the application logs. Only the generic actions the
resource's `expose api:` lists run here. With `dry_run?: true` (a
preview, a dry run) an action that may write, one not in the resource's
`read_only_actions`, is not run: `{:ok, :not_run}`. An optional
callback: a host without it answers `{:error, :unsupported}`.

# `tenant_id_for_slug`

```elixir
@spec tenant_id_for_slug(String.t()) ::
  {:ok, String.t()} | {:error, :not_found | :disabled | :not_started | term()}
```

The id of the tenant whose slug is `slug`: `{:ok, id}`; `{:error,
:not_found}` when no tenant has it; `{:error, :disabled}` when the tenant
is disabled (its hosts do not start); `{:error, :not_started}` while the
application cannot read its tenants yet (it starts after the plugin
packages), no error; or `{:error, reason}` when the read failed, which the
application logs. A plugin host entry names its tenant by slug
(`Alternera.PluginHosts`).

# `tenant_var_references?`

```elixir
@spec tenant_var_references?(String.t()) :: boolean()
```

Whether `text` holds a tenant variable reference, `{{NAME}}` or `{{secret.NAME}}`.

# `write_body_spool`

```elixir
@spec write_body_spool(term(), iodata()) ::
  {:ok, term()} | {:error, :disk_budget | :spool_failed, String.t()}
```

Appends `bytes` to a spool of `open_body_spool/2`: `{:error, :disk_budget,
message}` when the disk budget has no room for them, `{:error,
:spool_failed, message}` when the file could not be written; either way
the spool is spent and its file gone.

# `write_yaml`

```elixir
@spec write_yaml(map()) :: {:ok, String.t()} | {:error, String.t()}
```

`map` as YAML text, as the application writes YAML: `{:ok, text}`, or
`{:error, reason}` for a map it cannot write. A `Jason.OrderedObject`
anywhere in `map` is written with its keys in the order it has.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
