# Calling

> A client_t submits calls without driving the network, and polls to drive them and collect what finished.

`#include <ucall/client.hpp>``#include <ucall/types.hpp>` 1.0.0-rc.1 · b7577426

A `client_t` submits calls without driving the network, and polls to drive them and collect what finished. It holds up to `max_concurrent_connections` connections, reusing them across calls, and carries up to `max_pipelined_calls` calls on each.

```cpp
auto [client, client_status] = uc::client_t::make({.max_concurrent_connections = 4, .max_pipelined_calls = 16});
std::array<std::uint64_t, 64> contexts {};
std::array<char, 64> bodies {};
for (std::size_t index = 0; index != contexts.size(); ++index) {
    contexts[index] = index, bodies[index] = char(index);
    uc::client_call_t const call {.verb = ucall_verb_post_k, .url = "http://127.0.0.1:8545/infer",
                                  .body = {&bodies[index], 1}};
    uc::status_t const submitted = client.submit(call, &contexts[index]);
}
std::array<ucall_completed_call_t, 16> storage {};
for (std::size_t remaining = contexts.size(); remaining != 0;) {
    auto [finished, finished_status] = client.take_completed(storage, std::chrono::seconds(1));
    for (uc::completed_t done : finished) {
        auto [response, response_status] = std::move(done.response);
        std::printf("call %llu answered %u\n", *static_cast<unsigned long long*>(done.context),
                    response_status.failed() ? 0u : unsigned(response.status_code()));
    }
    remaining -= finished.size();
}

auto [health, health_status] = client.get("http://127.0.0.1:8545/health");
auto [cache_control, cache_control_status] = health.header("cache-control");
```

`submit` takes a `client_call_t`, whose URL, header lines and body the caller keeps until the call completes, and tags it with a `void*` context. A call admitted once completes exactly once, and a client already holding its call budget of unretrieved calls refuses more with `status_code_t::contended_k`. `take_completed` drives every admitted call and fills the front of the storage the caller passes with finished ones, waiting up to its timeout for the first, and a zero timeout performs one nonblocking round. It answers a `completed_view_t`, which hands each `completed_t` over once by `take` or by iterating and frees the responses never taken. `get`, `post`, `put`, `delete_`, `patch` and `call` submit one call and wait for it alone, while the library keeps the completions of other calls queued for the next `take_completed`. `client_call_t::options` carries the call’s own `client_call_options_t`: a timeout in place of the client’s, retries, redirect hops, security, and the client and server keys.

Every received response is a `response_t`, whatever its HTTP status, and only a failed exchange is a failed status. A `response_t` owns its received bytes, frees them when destroyed, stays readable after its `client_t` is destroyed and may move to another thread. Destroying the client cancels the calls still in flight and discards completions not yet retrieved.

### Streamed Response Bodies

Download a body larger than the client buffers by pulling it chunk by chunk as it lands:

```cpp
uc::client_call_t const call {.url = "http://127.0.0.1:8545/dataset.bin",
                              .options = {.body_delivery = ucall_body_delivery_streamed_k}};
auto [response, response_status] = client.call(call);
std::size_t received_bytes = 0;
for (;;) {
    auto [chunk, chunk_status] = response.body_chunk();
    if (chunk_status == uc::status_code_t::would_block_k) {
        auto [advanced, advanced_status] = client.take_completed({}, std::chrono::milliseconds(1));
        continue;
    }
    if (chunk_status.failed() || chunk.empty()) break;
    received_bytes += chunk.size();
}
```

A call whose `body_delivery` is `ucall_body_delivery_streamed_k` completes once its head arrived, so a download larger than `max_response_bytes` is pulled through `response_t::body_chunk` as it lands, while `response_t::body` reads empty. A pull never waits: with nothing decoded yet it answers `status_code_t::would_block_k`, and an empty `take_completed` advances the client, moving the next bytes in. Every streamed response is pulled to its end or destroyed, as its connection carries no other call meanwhile; destroying it early closes that connection and fails the calls pipelined behind it.

## API

### Types

#### `client_options_t`

What a client may hold open at once, how long it waits, and who it is under Noise; zero fields take their documented defaults, except the connection budget, which is required.

[client.hpp:27 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L27)

```cpp
struct client_options_t
```

#### Fields

| `max_concurrent_connections` | `std::size_t` | Connections open at once; zero is refused. |
| - | - | - |
| `max_pipelined_calls` | `std::size_t` | Calls one connection may carry at once; zero means one. |
| `connect_timeout` | `std::chrono::microseconds` | Time budget for establishing a connection; zero means 5 seconds. |
| `call_timeout` | `std::chrono::microseconds` | Time budget for a full exchange, queueing and redirects included; zero means 30 seconds. |
| `noise_private_key` | `ucall_noise_key_t const *` | The client identity for Noise handshakes. |
| `peer_authorizer` | `peer_authorizer_t` | Vets each server's authenticated key, needed by Noise XX calls. |
| `log` | `log_t` | Where log lines go; empty keeps the client silent. |
| `max_concurrent_calls` | `std::size_t` | Calls admitted at once, unretrieved completions included; zero means connections times pipelined calls. |
| `backend` | `ucall_io_backend_t` | The requested I/O backend. |
| `cpu_class_ceiling` | `ucall_cpu_class_t` | A ceiling on the CPU class; unknown sets none. |
| `max_response_bytes` | `std::size_t` | The largest response a call buffers; zero takes the largest receive buffer. |
| `server` | `server_t const *` | A server whose event loop drives this client, polled on that loop's thread only. |
| `event_loop` | `std::size_t` | Which of `server's` event loops. |

#### `client_call_options_t`

One call's time budget, retries, redirects, security and body delivery; zero fields take their defaults.

[client.hpp:72 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L72)

```cpp
struct client_call_options_t
```

#### Fields

| `timeout` | `std::chrono::microseconds` | Time budget including queueing and redirects; zero takes the client's. |
| - | - | - |
| `max_retries` | `std::size_t` | Additional connection attempts; zero means 3. |
| `max_redirects` | `std::size_t` | Redirect hops followed; zero means 5. |
| `security` | `ucall_security_t` | Transport security for this call. |
| `noise_private_key` | `ucall_noise_key_t const *` | The client's identity, needed by Noise KK. |
| `noise_peer_key` | `ucall_noise_key_t const *` | The server's expected key, needed by Noise IK and KK. |
| `noise_pre_shared_key` | `ucall_noise_key_t const *` | The secret the server's interface also holds, mixed first into the handshake; refused on plaintext. |
| `key_exchange` | `ucall_key_exchange_t` | The key exchange of the handshake, which the server must match. |
| `body_delivery` | `ucall_body_delivery_t` | Whether the call completes once the body is buffered, or once the head arrived. |

#### `client_call_t`

One call to submit, whose URL, header lines and body the caller keeps until it completes.

[client.hpp:104 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L104)

```cpp
struct client_call_t
```

#### Fields

| `verb` | `ucall_verb_t` | The HTTP method. |
| - | - | - |
| `url` | `std::string_view` | The full URL, with an `http` or `http+noise` scheme. |
| `headers` | `std::span<ucall_bytes_view_t const>` | Full header lines; the host, length and user agent lines are generated. |
| `body` | `std::string_view` | The request body; empty sends none. |
| `options` | `client_call_options_t` | The call's own budget, security and body delivery. |

#### `completed_t`

One finished call: the context it was submitted with, and its response or why none arrived.

[client.hpp:204 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L204)

```cpp
struct completed_t
```

#### Fields

| `context` | `void *` | The context passed to `client_t::submit`. |
| - | - | - |
| `response` | `expected<response_t>` | The received response, whatever its HTTP status, or the failed exchange's status. |

#### `completed_view_t`

The calls one `client_t::take_completed` finished, in the caller's storage and the order they finished, each handed over once by `take` or by iterating; the responses never taken are freed with the view.

[client.hpp:230 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L230)

```cpp
class completed_view_t
```

#### `outputs`

Where a call writes what `value_type_` is made of.

[types.hpp:85 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L85)

```cpp
template <typename value_type_>
struct outputs
```

#### Fields

| `value` | `value_type_` | The answer, which the call writes in place. |
| - | - | - |

#### `outputs< void >`

A call that outputs nothing but its status.

[types.hpp:95 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L95)

```cpp
template <>
struct outputs< void >
```

#### Fields

| `value` | `void` | The answer, which the call writes in place. |
| - | - | - |

#### `awaiter`

A [`deferred`](https://www.unum.cloud/docs/ucall/cpp/requests.md#deferred) being awaited, living in the awaiting coroutine's frame: each poll reads the status word, and one not yet final posts itself again.

[types.hpp:174 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L174)

```cpp
template <typename value_type_,
          a_scheduler scheduler_type_>
class awaiter
```

#### `answer_value`

What a final `answer_type_` carries beside its status, `void` for a bare `status_t`.

[types.hpp:203 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/types.hpp#L203)

```cpp
template <typename answer_type_>
struct answer_value
```

#### Class `client_t`

An HTTP client, made, submitted to, polled and destroyed by one owner at a time.8

[client.hpp:287 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L287)

```cpp
class client_t
```

It owns its handle alone: completions land in storage the caller passes, and a blocking call takes its own completion out of the library, leaving the others queued there.

##### `make`

Starts a client, which holds no connection until its first call is polled.

[client.hpp:292 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L292)

```cpp
expected<client_t> make(client_options_t const & options) noexcept;
```

##### `submit`

Admits [`call`](#call) without driving the network; its one completion carries `context`.

[client.hpp:322 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L322)

```cpp
status_t submit(client_call_t const & call, void *context) noexcept;
```

A full client refuses with [`status_code_t::contended_k`](https://www.unum.cloud/docs/ucall/cpp/errors.md#contended_k) until completions are taken.

##### `take_completed`

Drives every admitted call on this thread and fills the front of `storage` with finished ones, waiting up to `timeout` for the first; empty `storage` only advances the network.

[client.hpp:349 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L349)

```cpp
expected<completed_view_t> take_completed(
    std::span<ucall_completed_call_t> storage,
    std::chrono::microseconds timeout) noexcept;
```

##### `call`

Submits [`call`](#call) and polls until it finishes, leaving the completions of other calls queued for [`take_completed`](#take_completed).

[client.hpp:367 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L367)

```cpp
expected<response_t> call(client_call_t const & call) noexcept;
```

The response arrives whatever its HTTP status.

##### `get`

Fetches `url` and polls until the response arrives.

[client.hpp:383 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L383)

```cpp
expected<response_t> get(std::string_view url) noexcept;
```

##### `post`

Posts [`body`](#body) to `url` and polls until the response arrives.

[client.hpp:386 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L386)

```cpp
expected<response_t> post(
    std::string_view url,
    std::string_view body) noexcept;
```

##### `put`

Puts [`body`](#body) at `url` and polls until the response arrives.

[client.hpp:391 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L391)

```cpp
expected<response_t> put(
    std::string_view url,
    std::string_view body) noexcept;
```

##### `patch`

Patches `url` with [`body`](#body) and polls until the response arrives.

[client.hpp:402 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L402)

```cpp
expected<response_t> patch(
    std::string_view url,
    std::string_view body) noexcept;
```

#### Class `response_t`

A received HTTP response, whatever its status, freed on destruction and readable after its client is gone.7

[client.hpp:124 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L124)

```cpp
class response_t
```

##### `response_t`

[client.hpp:129 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L129)

```cpp
response_t() noexcept=default;
```

```cpp
response_t(
    ucall_client_t client,
    ucall_received_http_response_t response) noexcept;
```

##### `status_code`

The status code the server sent.

[client.hpp:137 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L137)

```cpp
ucall_http_status_t status_code() const noexcept;
```

##### `body`

The body, decoded from chunked framing if it arrived that way; a streamed call refuses it, as [`body_chunk`](#body_chunk) pulls its body.

[client.hpp:141 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L141)

```cpp
expected<std::string_view> body() const noexcept;
```

##### `header`

The value of header `name`, matched case-insensitively.

[client.hpp:153 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L153)

```cpp
expected<std::string_view> header(std::string_view name) const noexcept;
```

##### `fields_count`

Header field lines, repeated names apart.

[client.hpp:167 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L167)

```cpp
std::size_t fields_count() const noexcept;
```

##### `field`

Header field line `index` in arrival order.

[client.hpp:170 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L170)

```cpp
expected<field_t> field(std::size_t index) const noexcept;
```

##### `body_chunk`

Pulls the body bytes of a streamed call decoded since the last pull, empty once the body ended, valid until the next pull.

[client.hpp:191 ↗](https://github.com/unum-cloud/UCall/blob/b75774266a108d8a1b1a6db1489f8a7141c556df/cpp/ucall/client.hpp#L191)

```cpp
expected<std::string_view> body_chunk() noexcept;
```

Pulls run on the client's thread and never wait: with nothing decoded yet the pull answers [`status_code_t::would_block_k`](https://www.unum.cloud/docs/ucall/cpp/errors.md#would_block_k), and polling the client moves the next bytes in.
