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

The template language of a step's settings: `${…}` placeholders and
tenant variable references resolved against the data of an exchange, and
the request facts they read. What only the running pipelines know comes
through `Plugin.Host`.

# `request_fact`

```elixir
@type request_fact() ::
  :path
  | :method
  | :request_id
  | :timestamp
  | :client_ip
  | :client_cert
  | :caller
```

A request fact `request_fact/2` reads.

# `caller_key`

```elixir
@spec caller_key(map() | nil, String.t() | tuple() | nil) :: String.t() | nil
```

The key that names one caller, for what is counted per caller (a usage
plan) and for `${caller.key}`: `user:<proxy_user_id>` for a proxy user,
whichever credential it signed in with; `subject:<trusted_issuer_id>:<subject>`
for a token caller mapped to no proxy user, a subject over
500 bytes replaced by `sha256:<hex>` of it; otherwise
`address:<client_ip>`, from `client_ip` (text or an address tuple), or
`nil` without one. An internal replay (`method: "internal"`) has no key.

# `generate_request_id`

Generates a unique request ID with timestamp for tracking.

# `request_fact`

```elixir
@spec request_fact(request_fact(), map() | nil) ::
  String.t() | integer() | map() | nil
```

The request facts `${client_ip}`, `${request_id}`, `${timestamp}`,
`${method}` and `${path}` resolve to, read from `request` (a request
map, or the data `resolve/2` gets): a Script's `req` reads the
same (`Plugins.Script`). `:path` is the request's `path`, without the
query, as the exchange holds it at that point (after an earlier
Rewrite of it); `:timestamp` the current Unix time in seconds, an
integer. A request without one reads `"unknown"`, a new
`generate_request_id/0`, `"GET"` or `"/"`. `:client_cert` is the
verified client certificate's identity a reverse site asked for, a map
of `subject`, `issuer`, `common_name`, `sans`, `serial`, `fingerprint`
and `not_after`, or `nil` without one. `:caller` is the caller the
request signed in as, read from its `auth_info`, or `nil` for an
anonymous one: a map of `id` (the proxy user), `name`, `method` (how it
signed in: `api_key`, `jwt`, `client_certificate`, or the forward door's
method), `subject` and `issuer` (the token's, for a token caller), and
`groups` and `scopes` (lists), each left out when the caller has none,
and `key`, the caller's `caller_key/2`. A caller has signed in when it is a proxy user, at either door, or a
token caller mapped to none, as for a pipeline's access block
(`Pipelines.Access`).

# `request_facts`

```elixir
@spec request_facts() :: [request_fact()]
```

The request facts, in the order a script's `req` lists them (`Plugins.Script`).

# `resolve`

Resolves variable placeholders in strings with request data:
`${client_ip}`, `${request_id}`, `${timestamp}`, `${method}` and
`${path}`; `${client_cert.<field>}` and `${caller.<field>}`, a field of
the verified client certificate or of the signed-in caller
(`request_fact/2`); `${path.<name>}`, the value the pipeline's path template
matched for `{name}` (`data[:path_params]`, `Pipelines.PathParams`),
percent-decoded, nothing for a name it did not match;
`${allowed_methods}`, the methods of the pipelines that
match the request but for their method condition, as an `Allow` header
lists them (`"GET, POST"`, empty when none;
`Pipelines.AllowedMethods`), computed only for a value that names it;
`${var.<name>}`, the exchange variable `name` from
`data[:pipeline_vars]` (`Pipelines.Runner`): a string as it is, any
other JSON value as JSON text, nothing for a variable no step set;
`${var.<name>.<path>}`, the value at a JSON path inside it
(`Plugin.JsonPath.dig/2`: text is read as JSON first, a plain map is
walked by key), read the same way and nothing when the path leads
nowhere; and
`${error.<name>}` (`type`, `message`, `step_id`, `status`, `attempt`),
the error a catch or On error handles, from `data[:pipeline_error]`:
nothing outside one; `${error.outputs.<name>}`, a value the failed
step's To rows selected (`Runner.Error`'s `outputs`, by the variable a
row writes, else its output, `headers.etag`), as a variable is read;
`${error.step}`, `${error.request}` and `${error.response}`, the failed
step and its call (`Runner.Error`), and `${error.cause}`, the error an
On error was handling when it failed itself, each with an optional path
inside it
(`${error.response.body.error.message}`), read as a variable with a
path is; and `${steps.<id>.<output>}`, an earlier step's output in the
scope `data[:pipeline_steps]` holds (`Plugin.StepOutputs`), with an
optional key or JSON path after it
(`${steps.langs.headers.etag}`, `${steps.part.body.value.0.No}`), read
as a variable is; and `${input.<name>}`, an input of the call the
pipeline runs in or a wrapper's inner parameter, from
`data[:pipeline_input]` (`Pipelines.CallInputs`), with an optional path
after it, read the same way, and `${context.<name>}`, a context value
the pipeline sees (spec 063), from `data[:pipeline_context]`, likewise;
these substituted last, so no other
placeholder's value is read as one. A value without `${` or `{{` is returned as it
came: most rewritten header values are literals, and the replacement
passes cost more than the rest of a header rewrite together.

`{{NAME}}` and `{{secret.NAME}}` are tenant variables
(`Pipelines.TenantVars`), checked against `data[:tenant_vars]`: a plain
one resolves to its value, a secret one to the reference stamped with
the exchange's token (the value is put in where the request leaves the
proxy, `Pipelines.WireSecrets`);
`data[:allow_secret?]` false refuses a secret reference. The `${…}`
placeholders apply to the text around the references only, so a
variable's value is never read as a template. A reference that does not
resolve raises `Plugin.UnresolvedVariableError`.

---

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