scopedb-client 0.5.0

The official Rust client for ScopeDB
Documentation
# HTTP protocol

All requests identify the client through `X-ScopeDB-Client: scopedb-client`. An optional API key is sent as a Bearer token.

## Declarative Catalog

```text
GET, PUT         /v1/catalog
GET, PUT, DELETE /v1/catalog/databases/{database}
GET, PUT, DELETE /v1/catalog/databases/{database}/schemas/{schema}
GET, PUT, DELETE /v1/catalog/databases/{database}/schemas/{schema}/tables/{table}
GET, PUT, DELETE /v1/catalog/databases/{database}/schemas/{schema}/views/{view}
```

Names occupy individual percent-encoded path segments. Empty names and dot segments are rejected before sending. `client.catalog()` selects the root; its `database`, `schema`, `table`, and `view` methods select subtrees. GET returns the complete definition and its opaque `ETag`, without pagination.

PUT replaces the selected subtree, including all its children. Omitted collections are empty and omitted optional fields are cleared. The definition's `name` must match the path. DELETE removes the selected resource. A mutation returns `before`, `after`, and `destructive`, with absent resources represented by JSON null, plus an `ETag` header.

`CatalogSnapshot::version` and `CatalogResult::version` contain the opaque tenant catalog version from the `ETag` response header. `CatalogOptions::expected_version` sends that token unchanged, including its quotes, in `If-Match`. The version covers the entire tenant catalog, so a change outside the selected resource can invalidate it. Data writes do not advance the catalog version.

`CatalogOptions` maps `dry_run` and `allow_destructive` to query parameters. A dry run validates and rolls back the changes; its version identifies the base catalog state. A committed mutation returns the resulting version, which stays the same when no catalog changes occur. The server checks `expected_version` atomically with the mutation. A stale condition returns HTTP 412 with code `precondition_failed`, exposed as `ErrorKind::Conflict`; read the resource again and revise the change. Omitting `expected_version` applies the change without a version condition. Potentially destructive changes require explicit authorization.

Definitions include databases, schemas, tables, views, comments, computed-column `expression`, explicit column `indexes`, partitioning, clustering, `distinct`, and `retention_days`. Storage is accepted on creation and omitted from exports. Typed definitions reject unknown fields at every modeled level. `raw()` reads and applies `serde_json::Value` to retain unknown fields and arbitrary-precision JSON numbers.

Mutations use the client's `write_timeout` and never retry automatically. `Error::catalog_outcome()` distinguishes a known `CatalogMutationOutcome::Rejected` from `Unknown`, which requires reading the current definition before replaying. Transport interruptions, malformed responses, and gateway or timeout responses leave the outcome unknown, even when an intermediary supplies an error code. Original server codes and diagnostics remain available; client-side uncertainty does not synthesize a server code. Reads, dry runs, and local validation failures have no mutation outcome.

## Table ingest

```text
POST /v1/catalog/databases/{database}/schemas/{schema}/tables/{table}/ingest
Content-Type: application/x-ndjson
Content-Encoding: zstd
```

Each non-empty line is one row object matching an existing table. The SDK limits each uncompressed request to 8 MiB and 200,000 rows. The request is committed as a whole. A successful HTTP 200 response is:

```json
{ "num_rows_inserted": 2 }
```

The SDK verifies that `num_rows_inserted` matches the number of submitted rows and returns `AppendRowsResult` with `status: AppendOutcome::Succeeded`. An invalid response leaves the write outcome ambiguous.

HTTP 4xx and 5xx responses contain an `error` object with a string `code`, a diagnostic `message`, and optional `details`. For example, an HTTP 429 response is:

```json
{
  "error": {
    "code": "too_many_requests",
    "message": "ingest admission capacity exceeded"
  }
}
```

For a valid response, the HTTP status and `error.code` have the following correspondence. The SDK exposes them separately through `Error::http_status()` and `Error::code()`; `Error::kind()` provides the SDK failure category.

| HTTP status | `error.code`            | SDK `ErrorKind` | SDK `AppendOutcome` | Default writer/stream retry  |
| ----------- | ----------------------- | --------------- | ------------------- | ---------------------------- |
| 400         | `bad_request`           | `InvalidInput`  | `Failed`            | No                           |
| 404         | `not_found`             | `NotFound`      | `Failed`            | No                           |
| 405         | `method_not_allowed`    | `InvalidInput`  | `Failed`            | No                           |
| 408         | `request_timeout`       | `Timeout`       | `Failed`            | No                           |
| 409         | `conflict`              | `Conflict`      | `Failed`            | No                           |
| 412         | `precondition_failed`   | `Conflict`      | `Failed`            | No                           |
| 429         | `too_many_requests`     | `RateLimited`   | `Failed`            | Yes, within the retry budget |
| 500         | `internal_server_error` | `Internal`      | `Ambiguous`         | No                           |
| 503         | `service_unavailable`   | `Unavailable`   | `Ambiguous`         | No                           |

`Failed` means no rows in the request took effect or can take effect later; `Ambiguous` means they may have taken effect or may still take effect later. These are SDK recovery states, not response-body fields. HTTP 4xx establishes a definite failure and HTTP 5xx leaves the outcome ambiguous. In particular, the string `too_many_requests` alone does not establish a safe retry: it must accompany HTTP 429 in a valid error response. A known code paired with the wrong status is an invalid response. Unrecognized codes are preserved and follow the HTTP status guarantee when the response is otherwise valid. Codes defined only for statement execution results are not valid ingest HTTP error codes.

The ingest API does not return per-row results. A diagnostic `message` has no stable machine-readable format; handle a failed batch as a whole. Interrupted requests, malformed responses, and client-side timeouts have an ambiguous outcome. A received HTTP 408 error establishes a definite failure, whereas a client-side timeout does not.

Direct `Table::append` makes one attempt. `Table::append_writer` and `Table::append_stream` batch rows up to the SDK request limits and automatically retry validated HTTP 429 refusals. Opting into `.retry_ambiguous(true)` also retries transient ambiguous outcomes and can duplicate rows; client timeouts are never replayed automatically. A later definite failure leaves an earlier ambiguous attempt unresolved. See [Write rows][write-rows] and the [error-handling guide][error-handling] for finite-write reports, stream admission, and barrier settlement.

NDJSON input is limited to signed 64-bit integers, even for `uint` columns and nested JSON values. This input restriction does not apply to query results, whose `uint` cells support the full unsigned 64-bit range.

## Statements

```text
POST /v1/statements
GET  /v1/statements/{statement_id}?format=json
POST /v1/statements/{statement_id}/cancel?format=json
```

A submission request is:

```json
{
  "statement": "SELECT 1",
  "statement_id": "019a3998-0000-7000-8000-000000000000",
  "max_elapsed_seconds": 30,
  "max_estimated_seconds": 5,
  "format": "json"
}
```

The HTTP API accepts a `statement_id`; the SDK always supplies one, allocating a UUIDv7 unless `with_statement_id()` overrides it. Submission and execution errors retain this identifier through `Error::statement_id()` so callers can inspect or resume waiting on the execution with `Client::statement_handle()`. Duration limits must be positive and finite; omitted values use server defaults. `with_max_elapsed(Duration)` includes queueing time, while `with_max_estimated(Duration)` limits estimated execution duration.

All three endpoints return the same status family: `pending`, `running`, `finished`, `failed`, or `cancelled`. Each includes `statement_id`, `created_at`, and `progress` with `compute_seconds`. Finished responses contain `result_set`; failed responses contain `error: { code, message, details? }`.

`snapshot()` returns the cached status, `refresh()` fetches an active statement once, and `wait()` polls to a terminal state. `cancel()` requests cancellation and caches the returned status; if the status is still `pending` or `running`, call `wait()` to observe termination (`ErrorKind::Cancelled`, or the finished result / execution failure if the statement completed first). Terminal states are cached locally and do not trigger another request. Dropping a waiting future stops local polling without cancelling server execution.

The JSON result format encodes cells as strings or null, with field metadata providing their types. `raw_rows()` preserves those wire cells. Use value or object conversions for typed values; duplicate field names are rejected by object conversion.

## Errors

All asynchronous SDK operations return the concrete `Error` type. Its `kind()` classifies the failure and `operation()` identifies the public operation. HTTP errors use `{ "error": { "code": "...", "message": "...", "details": ... } }`, with optional `details`. `Error::code()` and `details()` preserve server codes and structured details, while `http_status()`, `request_id()`, and `retry_after()` expose transport diagnostics. Failed statements additionally expose `statement_details()`; append failures expose `append_details()`.

After append retries, raw server details and HTTP diagnostics describe the last failed attempt, while `append_details().status` retains `AppendOutcome::Ambiguous` if any attempt had an ambiguous outcome. See the [error-handling guide][error-handling] for partial-progress accessors, `is_retryable()`, and the diagnostic source chain, and [Upgrade from 0.4 to 0.5][migration-0.5] or [Upgrade from 0.3 to 0.4][migration-0.4] for version upgrades.

[write-rows]: ../README.md#write-rows
[error-handling]: errors.md
[migration-0.5]: migration-0.5.md
[migration-0.4]: migration-0.4.md