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

What sees a response body as it leaves, read-only: the module a holding
step's `leave/3` names in `{:observe_body, {module, state}, summary}` at
its Close (`Plugin.CallAdapter`).

The door calls `open/2` with the upstream's head as it arrived, before
the response steps changed it (its header names in their own case), then
`chunk/2` for each piece the client
is sent, then exactly one of `finish/2`, with the trailers, on a clean
end, or `abort/2`, with why, on any other: a client gone, an upstream
that broke off, an exchange that ended before the door opened the body
(`"abandoned"`). For an answer made on the request side the body is fed
whole, after `open/2` with that answer's head as it reached the Close. Nothing the client gets
depends on an observer: one that raises is logged and dropped.

The end is called at most once, from whichever process ends the
exchange, so an observer that holds something outside its state (a
single flight, a file) gives it back in either.

# `head`

```elixir
@type head() :: %{status: 100..599, headers: [{String.t(), String.t()}]}
```

A response head: its status and its headers, in order.

# `abort`

```elixir
@callback abort(reason :: String.t(), state :: term()) :: term()
```

The body did not end cleanly, for `reason`.

# `chunk`

```elixir
@callback chunk(binary(), state :: term()) :: {:ok, term()}
```

A piece of the body as the client is sent it.

# `finish`

```elixir
@callback finish(trailers :: [{String.t(), String.t()}], state :: term()) :: term()
```

The body ended cleanly; `trailers` are the upstream's.

# `open`
*optional* 

```elixir
@callback open(head(), state :: term()) :: {:ok, term()}
```

The upstream's head as it arrived (or a request-side answer's at the Close), before any piece of the body.

---

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