Metadata-Version: 2.4
Name: inkwire-receiver-core
Version: 1.0.0
Summary: The Inkwire receiver for Python: core, stores and a FastAPI app.
Author: Isaac Gounton
License-Expression: MIT
Project-URL: Repository, https://github.com/dahoai/inkwire
Project-URL: Documentation, https://github.com/dahoai/inkwire/blob/master/inkwire/SPEC.md
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pydantic>=2.9
Requires-Dist: markdown>=3.7
Requires-Dist: nh3>=0.2.18
Requires-Dist: email-validator>=2.0
Requires-Dist: pyyaml>=6.0
Requires-Dist: httpx>=0.27
Provides-Extra: fastapi
Requires-Dist: fastapi>=0.115; extra == "fastapi"
Requires-Dist: python-multipart>=0.0.9; extra == "fastapi"
Provides-Extra: dev
Requires-Dist: fastapi>=0.115; extra == "dev"
Requires-Dist: python-multipart>=0.0.9; extra == "dev"
Requires-Dist: pytest>=8.3; extra == "dev"
Requires-Dist: httpx>=0.27; extra == "dev"
Requires-Dist: uvicorn>=0.30; extra == "dev"
Dynamic: license-file

# inkwire-receiver-core

The Python [Inkwire](https://github.com/dahoai/inkwire) receiver: a core that validates
requests, derives SEO metadata, renders and sanitizes Markdown, resolves slugs and talks to a
store, plus a ready-to-run FastAPI app. It implements the whole
[protocol specification](https://github.com/dahoai/inkwire/blob/master/inkwire/SPEC.md).

```bash
pip install "inkwire-receiver-core[fastapi]" uvicorn
```

## Run it

```bash
export INKWIRE_API_KEYS=dev-key,audit-key:ro   # ":ro" marks a read-only key
export INKWIRE_ROOT=./content                  # entries are written as content/<kind>/<slug>.md
uvicorn inkwire_receiver.app:create_app --factory
```

| Variable | Default | |
| --- | --- | --- |
| `INKWIRE_API_KEYS` | (required) | comma-separated bearer keys; a `:ro` suffix makes one read-only |
| `INKWIRE_KINDS` | `post,page` | the kinds the receiver holds |
| `INKWIRE_STORE` | `file` | `file` (a folder of Markdown files) or `memory` |
| `INKWIRE_ROOT` | `./content` | the file store's folder |
| `INKWIRE_MEDIA_URL_BASE` | `/inkwire-media` | public base of uploaded files; a path is served by the app from `<root>/media` |
| `INKWIRE_SITE_NAME`, `INKWIRE_SITE_URL` | | discovery; the URL is also the base of entry URLs |
| `INKWIRE_SITE_DESCRIPTION`, `INKWIRE_SITE_LANGUAGE` | | discovery (a BCP 47 tag such as `en`) |
| `INKWIRE_URL_TEMPLATE` | `/blog/<slug>` for posts | entry URL with `{site}`, `{kind}`, `{slug}` |
| `INKWIRE_MARKDOWN_LIMIT_BYTES` | `1000000` | over it: `413 too_large` |
| `INKWIRE_MEDIA_LIMIT_BYTES` | `10000000` | over it: `413 too_large` |

## Endpoints

Every endpoint is under `/api`, takes `Authorization: Bearer <key>` and answers with
`Inkwire-Version: 1`. A read-only key may call every `GET`.

| | |
| --- | --- |
| `GET /api/inkwire` | discovery: site, features, kinds, limits |
| `POST /api/content` | create or update an entry, upserting on `external_id` and `kind` |
| `GET /api/content`, `GET /api/content/{id}` | list (`status`, `kind`, `tag`, `category`, `search`, `external_id`, `slug`, `limit`, `cursor`) and read, newest first |
| `PATCH /api/content/{id}` | edit part of an entry; honours `If-Match` |
| `DELETE /api/content/{id}` | remove an entry |
| `GET /api/terms` | the tags and categories entries use (`kind`, `search`, `limit`) |
| `POST /api/media` | upload an image (multipart) or have the receiver fetch a public URL |
| `GET /api/media` | the media library, newest first (`search`, `limit`, `cursor`) |
| `PATCH /api/media/{id}` | set or clear (`null`) a media item's `alt` and `title` |

The advertised features follow what the store can do: `terms` is always there, `media`,
`media_list` and `media_update` appear when the store saves, lists and updates media, and
`schedule` only when `InkwireConfig(schedule=True)` says the site holds a future-dated entry
back until its time. The file store has no renderer, so the reference app does not.

## Use the core in your own app

`create_app` takes a store, the keys and a config, so a site with its own database or routes
builds the app from its own parts:

```python
from inkwire_receiver import InkwireConfig, Limits, Site, parse_api_keys
from inkwire_receiver.app import create_app

app = create_app(
    store=MyStore(),  # see below
    api_keys=parse_api_keys("dev-key,audit-key:ro"),
    config=InkwireConfig(
        site=Site(name="Acme", url="https://acme.com", description="Acme's blog", language="en"),
        kinds=["post", "page"],
        limits=Limits(markdown_bytes=1_000_000, media_bytes=10_000_000),
        url_for=lambda entry: f"https://acme.com/blog/{entry['slug']}",
    ),
)
```

Each endpoint is also one function (`handle_upsert`, `handle_list`, `handle_get`,
`handle_patch`, `handle_delete`, `handle_terms`, `handle_media`, `handle_media_list`,
`handle_media_update`, `handle_discovery`) returning `(status, headers, body)`, to mount in
any framework.

## Stores

A store is four async methods, all scoped by the API key's `key`:

```python
async def get(self, key, id) -> StoredContent | None
async def list(self, key, filter: ListFilter) -> tuple[list[StoredContent], str | None]
async def save(self, key, entry: StoredContent) -> StoredContent
async def remove(self, key, id) -> bool
```

`list` returns one page, newest first by `updated_at` (ties by `id`); `paginate` does the
ordering and the keyset cursor. Slug conflicts and `external_id` lookups are ordinary `list`
calls with exact filters.

Media is optional, one method per feature: `media` (and `fetch_media` for `{url}` uploads)
saves a file, `list_media` lists the library (`paginate_media`), `update_media` changes
`alt` / `title`. A store without a method makes the receiver omit that feature.

`MemoryStore` is for development and tests. `FileStore` keeps `<root>/<kind>/<slug>.md` with
YAML frontmatter (existing files are read as they are) and `<root>/media/<id>-<name>` with a
`.meta.json` sidecar for each upload.
