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

Provisional: added for plugins that run processes of their own, and a
change to it needs a changelog line but no new major
(`docs/USER_PLUGINS_DESIGN.md`). The namespace has nothing to do with the
context's `:provisional?` key.

A step's HTTP call out of the proxy, made the way a Request step makes its
own: through the forward side, so it is an exchange of its own, recorded
with the exchange the step runs for as its parent and in its trace, and
everything the forward side does to a client's request happens to it
(forward pipelines, the SSRF guard, the tenant's outbound protocols, the
connection pools, the recording).

A call is made as one of two things:

  * `as: {:proxy_user, id}`, a proxy user of the tenant granted to
    pipelines: the forward side's pipelines, mocks and spies for that user
    apply, as to a Request step's call;
  * `upstream: %{plugin: MyPlugin, protocol: :http1 | :http2, headers: [...]}`,
    the plugin's own processes under a host suffix it claims
    (`Plugin.Provisional.Upstream`): the URL's host must be one of its names.
    The call is dialed where the plugin says, speaks `protocol` whatever the
    tenant's settings say, and carries `headers` (a token, say) written as
    the request leaves, never seen by a pipeline step or kept by the
    recording; a header of the same name a pipeline step set does not
    leave beside it, and a framing or connection field (`host`,
    `content-length`, `transfer-encoding`, `connection`, `keep-alive`, `te`,
    `upgrade`) is refused. `MyPlugin` is the plugin whose step makes the
    call: another plugin's names are refused. It acts as no proxy user.

Other options: `timeout_ms` (default: the step's time left), `cap` (the
most bytes of the response read, default `max_response_bytes/0`,
`:infinity` for none; a larger response fails `response_too_large`), `send_traceparent` (`true`, `false`, or `:tenant`,
the default: the tenant's setting or an earlier Configuration step's),
`redact_credentials?` (the call's recording redacts its credentials) and
`references` (`%{url: text, header_values: [text], source: location |
nil}`: the step's templates the call was built from, so the call's
exchange starts out knowing which of its parts may hold the exchange's
stamped secret references; without it, any part may).

A step inside Throttle or Circuit breaker containers has its calls pass
them, as a Request step's do: a call waits for a turn within its time
(the wait comes off `timeout_ms`), its outcome counts towards the breaker,
and a refused call fails `throttled` or `circuit_open` without being sent.
A call whose time ran out while it waited for a turn fails `timeout`, as
one the upstream did not answer in time does; only the message tells them
apart, and neither was answered. A plugin that retries calls each attempt
through `call/3`, so every attempt passes them.

A failure that says more is `{:error, type, message, details}`, with any
of: `exchange_id`, the exchange the failed call was recorded under (it
reached the forward side); `sent: false`, the call never left (a
container refused it, or its time ran out waiting for a turn), with the
refusal's `status` and `retry_after_ms` when a container refused it; and
`waited_us`, the time it waited for a turn. A response carries
`waited_us` too.

The call may carry the exchange's stamped secret references
(`{{secret.NAME}}` in a template the step resolved) in any part of its
request: they resolve as it leaves.

A response body read past the tenant's spool threshold comes back as a file
(`Plugin.Body`), which the step releases (`Plugin.Body.release/1`) once it is
done with it; a smaller one as a binary.

`recorded_body/2` reads the body a recorded call kept, for a plugin's
`replay_recorded/2`. `unstamp/1` gives text as the recording shows it, a
stamped secret reference as the setting wrote it. `framing_headers/0`
names the headers the proxy frames a call with, which a step may not set,
`max_response_bytes/0` the default cap, and `retry_after_ms/2` reads the
pause a `429` or `503` asks for.

In a plugin's own tests (`Plugin.Test.Host`) nothing leaves the process:
`Plugin.Test.stub_outbound/1` answers the calls.

# `details`

```elixir
@type details() :: %{
  optional(:exchange_id) =&gt; String.t() | nil,
  optional(:sent) =&gt; false,
  optional(:status) =&gt; 100..599,
  optional(:retry_after_ms) =&gt; non_neg_integer() | nil,
  optional(:waited_us) =&gt; non_neg_integer()
}
```

What a failure may say besides its type and message (see the moduledoc).

# `error_type`

```elixir
@type error_type() :: String.t()
```

Why a call failed: `connection`, `timeout`, `tls`, `unavailable` (nothing
was sent), `not_found` (the proxy user is not granted), `invalid_request`
(the call itself is refused: a method, a header, a URL), `depth_exceeded`
(calls nest too deep), `blocked` (the tenant's policy),
`response_too_large` (the response was larger than the call's `cap`), or `throttled` or
`circuit_open` (a Throttle or Circuit breaker container around the step
refused the call, which was not sent).

# `option`

```elixir
@type option() ::
  {:as, {:proxy_user, String.t()}}
  | {:upstream, upstream()}
  | {:timeout_ms, pos_integer()}
  | {:cap, pos_integer() | :infinity}
  | {:send_traceparent, boolean() | :tenant}
  | {:redact_credentials?, boolean()}
  | {:references, references()}
```

# `references`

```elixir
@type references() :: %{
  url: String.t() | nil,
  header_values: [String.t()],
  source: map() | nil
}
```

The step's templates a call was built from (see the moduledoc).

# `request`

```elixir
@type request() :: %{
  :method =&gt; String.t(),
  :url =&gt; String.t(),
  optional(:headers) =&gt; [{String.t(), String.t()}],
  optional(:body) =&gt; binary() | Alternera.Plugin.Body.t() | nil
}
```

What to send. `headers` are `{name, value}`; `body` a binary or a held body.

# `response`

```elixir
@type response() :: %{
  :status =&gt; 100..599,
  :headers =&gt; [{String.t(), String.t()}],
  :body =&gt; binary() | Alternera.Plugin.Body.t(),
  :exchange_id =&gt; String.t(),
  optional(atom()) =&gt; term()
}
```

What came back. `exchange_id` is the call's own recorded exchange.

# `upstream`

```elixir
@type upstream() :: %{
  :plugin =&gt; module(),
  :protocol =&gt; :http1 | :http2,
  optional(:headers) =&gt; [{String.t(), String.t()}]
}
```

A plugin's own processes as the call's target (see the moduledoc).

# `call`

```elixir
@spec call(map(), request(), [option()]) ::
  {:ok, response()}
  | {:error, error_type(), String.t()}
  | {:error, error_type(), String.t(), details()}
```

Makes the call for the step `context` belongs to. Raises `ArgumentError`
when the options name neither or both of `:as` and `:upstream`, or an
`:upstream` whose `:plugin` is not a plugin module. An `:upstream` call
for another plugin than the one whose step runs is refused with
`invalid_request`.

# `framing_headers`

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

The headers the proxy frames a call with (lower case), which a step may not set.

# `max_header_bytes`

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

The most bytes one header field line of a call's request may hold, name,
colon and value: the proxy's own limit on a head it reads, which a forward
pipeline on the call reads the head under. A step that builds a header from
data (a JSON document, a list of values) checks against it before it sends,
and refuses the step itself, rather than have the call fail on the way out.

# `max_response_bytes`

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

The most bytes of a response body a call reads unless it passes its own `:cap`.

# `recorded_body`

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

The `side` body a recorded exchange kept (an exchange as `replay_recorded/2`
gets it): `{:ok, body}`, an empty body when it had none, or `{:error,
message}`, a sentence fragment saying why it cannot be read ("is not
stored", "was recorded only in part").

# `retry_after_ms`

```elixir
@spec retry_after_ms([{String.t(), String.t()}]) :: non_neg_integer() | nil
```

The pause, in ms, that `headers`' `Retry-After` asks for (seconds or an
HTTP date), whatever the status; nil without one that parses.

# `retry_after_ms`

```elixir
@spec retry_after_ms(100..599, [{String.t(), String.t()}]) :: non_neg_integer() | nil
```

The pause, in ms, that a `429` or `503` response asks for in its
`Retry-After` (seconds or an HTTP date); nil for any other status, or
without a header that parses.

# `unstamp`

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

`text` as the recording shows it: a secret reference stamped with the
exchange's token (`{{secret.NAME.TOKEN}}`, raw or percent-encoded) as the
setting wrote it, `{{secret.NAME}}`. Text without one comes back as it is.

---

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