# `Alternera.PluginHosts.HttpCall`
[🔗](https://github.com/10e-ab/alternera-camel/blob/v0.9.0/alternera_plugin_hosts/lib/alternera/plugin_hosts/http_call.ex#L1)

A plugin host step's call over a plugin host's HTTP channel: `POST
/ops/<operation>` with the inputs as a JSON object, made as the plugin's
own outbound call (`Plugin.Provisional.Outbound`, with `upstream:`) to the
address `PluginHosts.Endpoint` names, under the suffix
its kind claims (`PluginHosts.Kind`, `Plugin.Provisional.Upstream`).

So the call is an exchange of its own, recorded with the exchange that
made it as its parent and in its trace, and the forward side's upstream
connections carry it. The member's token is one of the upstream's
headers, not the call's: the forward side writes it as the request leaves.
The host's `protocol` picks the scheme and the wire: HTTP/1.1 for `:http1`
and `:https1`, HTTP/2 for `:http2` and `:https2`, where the forward side's
H1→H2 bridge multiplexes many calls on one connection per member (h2c
prior knowledge, or ALPN over TLS). A host speaks only that protocol, so
the tenant's outbound settings and a Configuration step do not apply.

## A body

A step with a `source` sends that body as it is, with its content type,
and the inputs move to one request header, `Alternera-Inputs`: the same JSON
object, every character past ASCII escaped (`\uXXXX`) so the header stays
a valid field value. The host hands the route the body as bytes (or, for
a JSON content type, parsed) and the inputs as headers of the exchange. A
field line cannot pass `Outbound.max_header_bytes/0`, so inputs
that do not fit are refused here as `invalid_input`, before anything is
sent.

## Headers and properties

A Camel step's header and property rows (the step's extras,
`PluginHosts.Step`) go beside the body, with or without a source, in
`Alternera-Headers` and `Alternera-Properties`: JSON objects escaped as
`Alternera-Inputs` is, each sent only when it holds a name and bounded
by the same limit. The host makes them headers and properties of the
exchange.

The member is the pool's choice (`Pool.checkout/2`); the member counts the
call as in flight until `Pool.checkin/1`, or until the caller dies.

## Another node

When this node runs no pool of the host, the call goes whole to a node
whose host serves HTTP and whose entry lets the tenant call it
(`PluginHosts.Cluster`), one with a ready member first, else one that
starts a member within the call's deadline; that node checks its own
entry again and makes the call, so the exchange is recorded there. The
step's context goes without the bodies its requests and responses hold
(the call reads none of them). A body spooled to a file here travels as its bytes, up to
8 MiB; a larger one is refused as `invalid_input`. A forwarded call is
not forwarded again, and a node that runs the host never forwards.

## The answer

A 2xx answer is `{:ok, body, content_type}`: a JSON body decoded, text
as text, anything else as its bytes, and a 204 as nil. Any other status
carries the host's `{"error": {"type", "message"}}` and maps to the
step's error kinds:

| Status | Kind |
|---|---|
| 400, 413 | `invalid_input` |
| 401 | `unavailable` (the token was refused) |
| 504 | `timeout` |
| any other | the operation's failure, `{:op_failed, type, message, details}` as a frame reply's, `details` the rest of the error object (`PluginHosts.Step` makes the step's error of it) |

A failed call that never got an answer keeps the outbound call's kind
(`timeout`, `connection`, `unavailable`).

A Camel host sends the route's answer headers beside the body, in
`Alternera-Answer-Headers`, a JSON object escaped as the inputs are; one
past 8000 bytes it leaves out and names in
`Alternera-Answer-Headers-Omitted`. `answer_headers/3` reads them for a
step that offers them (`PluginHosts.Step.outputs/1`). It sends the
route's trace in `Alternera-Trace` (`trace/3`); a failure carries its
trace in the error object.

A call of a written document (`PluginHosts.Definition`) names its hash in
`Alternera-Definition`. A member that does not hold it answers 404
`unknown_definition`; the document then goes to that member over its
frame channel (`Host.define/3`) and the call is sent again, once, so the
step's summary lists two calls.

# `answer`

```elixir
@spec answer(integer(), String.t() | nil, binary(), String.t(), String.t()) ::
  {:ok, term(), String.t() | nil}
  | {:error, String.t(), String.t()}
  | {:error, {:op_failed, String.t(), String.t(), map()}}
```

The step's result for an answer with `status`, `content_type` and `body`
from host `name`'s operation `op` (see "The answer"): what a call gets,
and what a recorded call replays (`PluginHosts.Step.replay_recorded/3`).

# `answer_headers`

```elixir
@spec answer_headers([{String.t(), String.t()}], String.t(), String.t()) :: map()
```

The answer's headers a Camel host sends in `Alternera-Answer-Headers`, of
the response `headers` (`[{name, value}]`): an object of lower-case names
to text (`header_object/2`), empty when there is none. A host that left
them out for their size (`Alternera-Answer-Headers-Omitted`), or sent
what is not a JSON object, gives none, logged.

# `call`

```elixir
@spec call(
  module(),
  String.t(),
  String.t(),
  term(),
  {binary() | Alternera.Plugin.Body.t(), String.t()} | nil,
  map(),
  pos_integer(),
  keyword()
) ::
  {{:ok, term(), String.t() | nil}
   | {:error, String.t(), String.t()}
   | {:error, {:op_failed, String.t(), String.t(), map()}}, [map()],
   map() | nil}
```

Calls `op` on `kind`'s host `name` with `input`, the input object and the
extras beside it (`{fields, extras}`, extras keyed `"headers"` and
`"properties"`) and, when the step has a source,
`body` (`{content, content_type}`), for the step's `context`, within
`deadline_ms`. Returns the result, the call records for the step's
summary (`calls`, as a Request step's), and the call as sent and
answered (`%{request, response}`; nil when nothing was sent), from which the step builds its `record`.
Option: `:definition`, the `PluginHosts.Definition` `op` is in.

# `header_object`

```elixir
@spec header_object(map(), String.t()) :: %{required(String.t()) =&gt; String.t()}
```

An object of answer headers as a step's `headers` output takes it: each
name lower-cased, a value that is not text left out (logged), and a name
sent in two cases joined with `", "`, as a header sent twice is.

# `trace`

```elixir
@spec trace([{String.t(), String.t()}], String.t(), String.t()) :: map()
```

The trace a host sent beside a 2xx answer, from its `Alternera-Trace`
header (`{"trace": [...], "trace_omitted": n}`): that object, or `%{}`
without one. One that is not that JSON is logged and left out.

---

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