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

The plugin model: one callback, `call(inputs, opts, context)`, inputs in
and writes out, and `leave/3` for a plugin whose step holds something for
the rest of the exchange; and what a host asks of a plugin. The host
runs a step through `run/6`, which calls `head/5` or `body/5`, gives
back what a step held through `release/4`, and runs a streaming step
through `stream_init/3`, `stream_chunk/2`, `stream_finish/1` and
`stream_abort/2`; their results are the runner's own vocabulary
(`{:cont, message, extras}` for a head, `{:ok, body, summary[, extras]}`
for a body, a hold, `{:skip_to_after, …}`, `{:answer_below_close, …}`).

The step's opts are the plugin's own. What the model needs besides is the
host's, kept beside them on the step: `extras/2`, from the stored
settings, gives `%{signature: …}`, with `on_refusal: …` for a plugin that
declares `refuses:`, `targets: rows` for one whose schema has a To list
(`Plugin.Targets`). The host passes those extras back to `run/6`,
`requirements/3` and `flag/4`, and `init/2` or the plugin's `load/1`
checks settings with the model's own keys taken off. `holds?/1` and
`streams?/1` answer from the plugin's declaration.

`call/3` answers:

  * `{:ok, writes}` or `{:ok, writes, summary}`: `writes` are `{location,
    value}` pairs the runner writes, held to the tenant's limits; the step
    never returns a changed message;
  * `{:ok, inputs}` or `{:ok, inputs, summary}`: the step wrote into its
    inputs (`Inputs.with_write/3`) and hands them back. Its writes are
    the scratch's, in order, checked as returned writes are, and the
    runner takes the scratch's view as the exchange after the step
    (`scratch` in the extras) after the checks a write gets besides the
    write itself (the phase, the direction, the URL flag), instead of
    writing them a second time. The scratch held variables to the
    tenant's cap (`Inputs`). A step given no view has its writes
    written by the runner;
  * `{:respond, answer, writes}` or `{:respond, answer, writes, summary}`:
    the step answers the exchange. A holding plugin's answer (a cache hit)
    lands below its Close, as a stack unwinds, and writes nothing;
  * `{:hold, state}` or `{:hold, state, summary}`, for a plugin that
    defines `leave/3`: the rest of the exchange runs inside the step, and
    `leave/3` gets `state` back exactly once with how the exchange ended
    (`t:Provisional.Hold.outcome/0`), at the step's Close in After;
  * `{:refuse, type, message, retry_in_ms}`, for a plugin that declares
    `refuses: %{type => status}`: the step refuses the exchange with that
    status;
  * `{:skip, reason}`, `{:return, value[, summary]}`, a failure.

What the adapter does so that the plugin does not:

  * the phase follows from the signature (`Plugin.Signature`), and a
    holding plugin runs in the request head, on the request side;
  * a plugin with `refuses:` takes an `on_refusal` setting, popped before
    its `init/1` sees the settings (`init/2`): `"fail"` (the
    default) fails the step with the refusal, `"skip_to_after"` answers
    with it and skips to After below the step's Close; `raises/1` and
    `responses/1` follow;
  * with `previews: :skip` (a holding plugin's default), a dry run or a
    pipeline test does not call `call/3`: a holding step's traffic is not
    the partner's, so it holds nothing and `leave/3` is not called, and
    any other step (a rate limit, whose counts a preview must not spend)
    passes, writing nothing. With `previews: :enter` (the default for a
    plugin that does not hold, and a cache's, which reads in a preview
    but writes nothing), `call/3` runs and reads `context.dry_run?` and
    `context.test_run?` itself. Either way a preview of an exchange the
    step refused refuses it again from the recording, when the host put
    that refusal in the context (`Hold.recorded_refusal/1`; the proxy
    does for a holding step's);
  * a holding plugin's context carries `owner` (`Hold.owner/1`) and
    `time_left_ms`.

`enter_all/2` and `leave_all/3` are the container form: the same holding
plugins, entered around each outbound call.

# `extras`

```elixir
@type extras() ::
  nil
  | %{
      :signature =&gt; Alternera.Plugin.Signature.t(),
      optional(:on_refusal) =&gt; String.t(),
      optional(:targets) =&gt; [Alternera.Plugin.Targets.row()],
      optional(:reveal_keys) =&gt; [String.t()]
    }
```

The model's data beside a step's opts; nil where a host has none.

# `declared_statuses`

```elixir
@spec declared_statuses(module(), map()) :: %{required(String.t()) =&gt; [200..599]}
```

The statuses `module` declares for its error types under `settings`:
its refusals' (`refuses:`, whatever `on_refusal` says, since a refusal
skipped to After is answered with its error answer too) and the
`{type, status}` entries of its `raises/1`, each type with its
statuses, more than one when the type is listed with several. The
error answer keeps a failure's status only when it is one declared for
its type.

# `directive_keys`

```elixir
@spec directive_keys() :: [atom()]
```

The directives a step may set (`{:ok, writes, summary, %{directives: map}}`),
each an atom key; the SDK checks the keys, the runner the values:

  * Configuration's overrides, last step wins: `retry_policy`,
    `timeout_ms`, `protocol_preference`, `spool_threshold_bytes`,
    `large_body_recording`, `record_exchange`, `record_execution`,
    `response_decompression`, `send_server_timing`,
    `pipeline_calls_send_traceparent` and `error_detail`;
  * the recording's redactions, a list of names unioned over every
    step: `redact_headers`, `redact_query_params`, `redact_request_body`
    and `redact_response_body`;
  * `authorization_renewal`, what the door renews an `Authorization` it
    sent by when the upstream answers `401` (the host's
    `Plugin.Provisional.Credentials` gives it).

# `enter_all`

```elixir
@spec enter_all([{module(), term()}], map()) ::
  {:ok, [{module(), term()}]}
  | {:error, String.t(), String.t(),
     %{status: 100..599, retry_after_ms: pos_integer() | nil}}
```

The container form, per outbound call: calls every holding `{module,
opts}` around a call, ordered by its `exchange_opener/0` rank (a breaker
before a throttle, so an open circuit refuses before a turn is spent), in
the time left, with `per_call?: true` in the context. `{:ok, entered}`,
which `leave_all/3` must follow, or the first refusal with its answer
(`%{status, retry_after_ms}`, the status the refusing plugin's `refuses:`
gives its type), after the ones entered before it left with `:not_sent`,
innermost first, as a stack unwinds. A plugin that answers `{:ok, _}`
holds nothing for the call.

# `extras`

```elixir
@spec extras(module(), term()) :: extras()
```

The model's data for a step with these stored settings.

# `failure_keys`

```elixir
@spec failure_keys() :: [atom()]
```

The keys a `call/3` failure's map may hold (`t:Alternera.Plugin.call_failure/0`):
`status`, the HTTP status of the error answer; `outputs`, the step's
outputs as `{:ok, writes, summary, %{outputs: values}}` gives them,
which the step's To rows pick from for the error's `outputs`
(`Plugin.Targets.selected/2`); `target`, what in the request the error
is about; `on_purpose: true`, an error the pipeline's author raised; and
`outbound_us`, the time the step waited on a service outside the proxy
before it failed (a token fetch), as a summary's `outbound_us` is. A step
that calls out (Request) says what of the call failed: `request`, the
call as the step sent it, a map; `response`, the response that failed it,
a map; `calls`, every attempt's record, a list; `retry_after_ms`, the
pause a refusal asked for; and `header_vars`, the variables its rows
would have written from headers, marked as those headers for the
recording (`summary.header_vars`). `summary`, a map, holds entries for
the failed step's own summary, as a success's summary does (a plugin
host's trace of the operation that failed); the error does not carry
them.

# `flag`

```elixir
@spec flag(module(), atom(), term(), extras()) :: boolean()
```

A boolean answer of the step (`exchange?`, `neutral?`, `follows?`,
`keeps_request_body?`, `any_phase?`, `stoppable?`, …). The signature
answers `neutral?`, `follows?` and `keeps_request_body?`, which a plugin
cannot override; its trait options answer `any_phase?`, `body_required?`,
`runs_bodiless?`, `stoppable?`, `run_in_task?`, `body_handles?`,
`own_body_handle?`, `writes_url?` and `tenant_vars?` (`use Alternera.Plugin, stoppable:
true`); and `exchange?` is the signature's or the `exchange: true` option.
Any other name is false.

# `holds?`

```elixir
@spec holds?(module()) :: boolean()
```

Whether the plugin's steps hold something for the rest of the exchange (`leave/3`).

# `init`

```elixir
@spec init(module(), term()) :: {:ok, term()} | {:error, [String.t()]}
```

The plugin's `init/1` over the settings without the model's own keys,
those checked here: what a host that validates stored settings calls in
place of `init/1`. A key the schema declares `configurable: :only` is
refused: only a Configuration step sets it (`Plugin.configurable_fields/1`);
so is a value a field lists in `skips` (`Plugin.skip_errors/2`).

# `init_filled`

```elixir
@spec init_filled(module(), term()) :: {:ok, term()} | {:error, [String.t()]}
```

`init/2` over settings the runner filled from Configuration steps, which
may hold the keys only a Configuration step sets.

# `leave_all`

```elixir
@spec leave_all(
  [{module(), term()}],
  Alternera.Plugin.Provisional.Hold.outcome(),
  map()
) :: :ok
```

Gives back what `enter_all/2` entered, innermost first, with the call's outcome.

# `load`

```elixir
@spec load(module(), term()) :: {:ok, term(), [map()]} | {:error, [String.t()]}
```

A refusing plugin's `load/1`: `init/2`, in `load/1`'s form.

# `memory_factor`

```elixir
@spec memory_factor(module(), map()) :: pos_integer() | nil
```

The plugin's own `memory_factor/1` for `settings`, a positive integer,
or `nil` when it defines none. Anything else it answers raises.

# `on_refusal_values`

```elixir
@spec on_refusal_values() :: [String.t()]
```

What a step that refuses does with a refusal: fail, or skip to After.

# `release`

```elixir
@spec release(module(), term(), term(), map()) :: :ok | {:ok, map()} | tuple()
```

Gives back what a step held (`Plugin.Provisional.Hold`), through its `leave/3`.

# `requirements`

```elixir
@spec requirements(module(), term(), extras()) :: [atom()]
```

The phases the step needs, from its signature (that of empty settings
when `extras` is nil, as `run/6` takes it): the request head for a step
that holds; its side's head and body for a body step of a plugin that
checks the head (`checks_head: true`), head first.

# `run`

```elixir
@spec run(module(), atom(), term(), term(), extras(), map()) :: term()
```

Runs the step in `phase`: a head or a body, its opts, its extras, its context.

# `streams?`

```elixir
@spec streams?(module()) :: boolean()
```

Whether the plugin is declared `streams: true` (some of its steps may stream).

---

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