# `Ankusa.SDK.Sources`
[🔗](https://github.com/jamescarr/ankusa/blob/sdk-elixir-v0.6.0/packages/sdk-elixir/lib/ankusa/sdk/sources.ex#L1)

The tenant-scoped source-management API: `Ankusa.Admin.Router` on
`admin.port` (default `4002`) serves list/get/create/update/delete for a
tenant's ingest sources.

A source is addressed as `<tenant>.<name>`; this client speaks in those terms
and builds the paths for you. Only `[A-Za-z0-9_-]{1,64}` tenants and names are
accepted, and both are checked before any path is built, so a caller-supplied
name cannot escape its tenant through URL normalization.

## The version latch

`:expected_version` is an optional safety latch: when set, every API call
first makes sure `GET /health` reported that version, and a mismatch is an
`Ankusa.SDK.VersionMismatchError` before the request is sent.

The client is immutable, so the fetched version is returned rather than
remembered. Run `verify_version/1` once at startup and keep the returned
client; every later call then re-checks the cached version without another
`/health` request:

```elixir
{:ok, sources} = Ankusa.SDK.Sources.verify_version(Ankusa.SDK.Sources.new(url, expected_version: "0.3.0"))
{:ok, list} = Ankusa.SDK.Sources.list_sources(sources, "acme")
```

Failures are `Ankusa.SDK.SourceNotFoundError` (`404`),
`Ankusa.SDK.SourceConflictError` (`409`), `Ankusa.SDK.SourceStoreReadOnlyError`
(`409 source_store_read_only`: the deployment's source store is a static seed),
`Ankusa.SDK.SourceInvalidError` (`400`, or an invalid tenant/name caught before
any request), `Ankusa.SDK.VersionMismatchError`, and
`Ankusa.SDK.SourcesUnavailableError` (unreachable, timed out, or `5xx`). Every
one carries `:status` and `:body`.

# `t`

```elixir
@type t() :: %Ankusa.SDK.Sources{
  expected_version: String.t() | nil,
  http: term(),
  server_version: String.t() | nil
}
```

# `create_source`

```elixir
@spec create_source(t(), term(), term(), Ankusa.SDK.Sources.Spec.t()) ::
  {:ok, Ankusa.SDK.Sources.Source.t()} | {:error, Exception.t()}
```

Create a source: `POST /v1/tenants/{tenant}/sources`.

The name travels in the body alongside the spec; the tenant comes from the
URL.

# `delete_source`

```elixir
@spec delete_source(t(), term(), term()) :: :ok | {:error, Exception.t()}
```

Delete a source: `DELETE /v1/tenants/{tenant}/sources/{name}`.

Returns `:ok` on `2xx` (`204` with an empty body). A source that still has
undelivered hooks on the server's node is `409 source_has_deliveries`:
`{:error, %Ankusa.SDK.SourceConflictError{}}` whose `body` carries that
`"error"` and the `"pending"`/`"inflight"` counts. This function never sends
the admin API's `?deliveries=dead_letter`.

# `get_source`

```elixir
@spec get_source(t(), term(), term()) ::
  {:ok, Ankusa.SDK.Sources.Source.t()} | {:error, Exception.t()}
```

Fetch one source: `GET /v1/tenants/{tenant}/sources/{name}`.

# `list_sources`

```elixir
@spec list_sources(t(), term()) ::
  {:ok, [Ankusa.SDK.Sources.Source.t()]} | {:error, Exception.t()}
```

List a tenant's sources: `GET /v1/tenants/{tenant}/sources`.

# `new`

```elixir
@spec new(String.t(), keyword()) :: t()
```

Build a client for `base_url`.

Options are `:headers` (a map or `{name, value}` list sent on every request),
`:timeout_ms` (default `10_000`, applied to connect *and* receive — to receive
only with a `:finch` pool, which owns its connect options),
`:req_options` (transport tuning: `:finch`, `:connect_options`,
`:pool_timeout`, `:plug`; `:finch` and `:connect_options` are exclusive), and
`:expected_version` (a version string to latch against, or `nil` for no latch).

# `server_version`

```elixir
@spec server_version(t()) ::
  {:ok, String.t()}
  | {:error,
     Ankusa.SDK.SourcesUnavailableError.t()
     | Ankusa.SDK.VersionMismatchError.t()}
```

The deployment's Ankusa version: `verify_version/1` then the `"version"` field.

# `update_source`

```elixir
@spec update_source(t(), term(), term(), Ankusa.SDK.Sources.Spec.t()) ::
  {:ok, Ankusa.SDK.Sources.Source.t()} | {:error, Exception.t()}
```

Replace a source: `PUT /v1/tenants/{tenant}/sources/{name}`.

The name comes from the URL; the spec carries no name.

# `verify_version`

```elixir
@spec verify_version(t()) ::
  {:ok, t()}
  | {:error,
     Ankusa.SDK.SourcesUnavailableError.t()
     | Ankusa.SDK.VersionMismatchError.t()}
```

Fetch `GET /health` when the version isn't cached yet, enforce
`:expected_version`, and return the client with the version cached.

Run it once at startup and keep the returned client; later calls then skip the
probe.

---

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