Add the list block and the module layer with the note module
The list block prints items with a dot, dash, number or checkbox bullet,
wrapping a long item with a hanging indent; a checkbox list ticks its
done items as [√], or [x] on code pages without √. It stands alone or
inside a box, and its items, bullet and done fields carry their enums
and limits as jsonschema tags.
internal/pkg/modules holds modules that build ordinary jobs from their
own payloads: a registry by name, an Env with the clock, time zone and
time layout, strict payload decoding that reports every problem, ErrSkip
and PayloadError. The note module prints a boxed note with an optional
checklist and a time stamp in the footer.
Three routes join the route table: POST /api/v1/print/{module} and
POST /api/v1/preview/{module}, which take ?copies and a reserved
?printer, and GET /api/v1/modules. Print and the module route share one
submit path, and preview shares one too. New error codes:
invalid_payload, invalid_query, unknown_printer, module_not_found and
module_failed; a module that builds an invalid receipt is logged and
reported to Sentry once. [format] gains timezone and time_layout, the
binary embeds the time zone database, and uberbringer preview takes
--module.
The note receipt passed on the TM-T88IV, and its bytes are pinned in
TestPaperReceipts. docs/api.md describes the list block, the module
routes and the note payload; docs/swagger.json is regenerated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6b2e402f1a
commit
3e9779f71a
69 files changed
+3092
-262
No files matched your search
+135
-13
@@ -1,8 +1,8 @@
|
||||
# Überbringer API
|
||||
|
||||
The HTTP API, the print elements and blocks, and the error codes.
|
||||
[README.md](../README.md) covers installing, configuring and running the
|
||||
daemon.
|
||||
The HTTP API, the print elements and blocks, the modules, and the error
|
||||
codes. [README.md](../README.md) covers installing, configuring and running
|
||||
the daemon.
|
||||
|
||||
## Contents
|
||||
|
||||
@@ -15,6 +15,8 @@ daemon.
|
||||
- [Elements](#elements)
|
||||
- [Blocks](#blocks)
|
||||
- [Glyphs and code pages](#glyphs-and-code-pages)
|
||||
- [Modules](#modules)
|
||||
- [`note`](#note)
|
||||
|
||||
## API
|
||||
|
||||
@@ -30,6 +32,9 @@ included, carry `Cache-Control: no-store`.
|
||||
|--------|--------------------|------------------------------------------|
|
||||
| POST | `/api/v1/print` | Queue a print job |
|
||||
| POST | `/api/v1/preview` | Show the lines a job prints, without printing it |
|
||||
| POST | `/api/v1/print/:module` | Queue the receipt a [module](#modules) builds from its payload |
|
||||
| POST | `/api/v1/preview/:module` | Show the lines that receipt prints, without printing it |
|
||||
| GET | `/api/v1/modules` | List the modules |
|
||||
| GET | `/api/v1/jobs/:id` | Job status |
|
||||
| GET | `/healthz` | Printer and queue health (no auth) |
|
||||
| GET | `/openapi.json` | This API's OpenAPI 3.1 document (no auth) |
|
||||
@@ -196,21 +201,30 @@ Errors are JSON:
|
||||
- `code` is stable and meant for clients to match on.
|
||||
- `error` is for people and may change. It never repeats the HTTP
|
||||
framework's own text; for example, an unknown route answers `not found`.
|
||||
- `details` appears only when an `invalid_job` fails validation. Each entry
|
||||
names the element by its path, e.g.
|
||||
`elements[0].children[3] (qr): not allowed inside a box; place it after the box`.
|
||||
- `details` lists every problem when a job fails validation (`invalid_job`),
|
||||
a module payload is invalid (`invalid_payload`) or a module builds an
|
||||
invalid receipt (`module_failed`). Each job problem names the element by
|
||||
its path, e.g.
|
||||
`elements[0].children[3] (qr): not allowed inside a box; place it after the box`,
|
||||
and each payload problem names the field, e.g.
|
||||
`done[0]: index 5 is out of range`.
|
||||
|
||||
| Status | `code` | Meaning |
|
||||
|--------|--------|---------|
|
||||
| 400 | `invalid_job` | Malformed JSON, an unknown field, trailing data after the job, or invalid elements. When elements are invalid, `details` lists every problem. A `{"data": [...]}` body gets a message pointing to the [element format](#elements) |
|
||||
| 400 | `invalid_payload` | A [module](#modules) payload that is not a JSON object, has an unknown field or a field of the wrong type, or is invalid, e.g. `invalid payload for module "note"`. `details` lists every problem |
|
||||
| 400 | `invalid_query` | On a module route: `copies` outside 1–5, a parameter given twice, or a parameter other than `copies` and `printer` |
|
||||
| 400 | `unknown_printer` | On a module route: `printer` names a printer other than `default` |
|
||||
| 400 | `bad_request` | A malformed HTTP request, refused by the server itself |
|
||||
| 401 | `unauthorized` | `missing bearer token`, or `invalid or expired token`. Sent with `WWW-Authenticate` |
|
||||
| 404 | `not_found` | Unknown route |
|
||||
| 404 | `job_not_found` | Unknown, expired or malformed job ID |
|
||||
| 404 | `module_not_found` | Unknown module, e.g. `module "notx" not found` |
|
||||
| 405 | `method_not_allowed` | Known route, wrong method, e.g. `DELETE /api/v1/print` with a valid token (without one, `/api/v1` answers `401` first). Sent with `Allow` |
|
||||
| 408 | `request_timeout` | The request was not received in time |
|
||||
| 413 | `body_too_large` | Body larger than `webserver.body_limit` |
|
||||
| 422 | `too_many_lines` | From `/api/v1/preview`: the job would print more than `format.max_lines` lines |
|
||||
| 422 | `too_many_lines` | From `/api/v1/preview` and `/api/v1/preview/:module`: the job would print more than `format.max_lines` lines |
|
||||
| 422 | `module_failed` | A module built a receipt that fails validation, e.g. `module "note" built an invalid receipt`, with the problems in `details`. This is a bug in the module; it is logged and reported to Sentry |
|
||||
| 429 | `rate_limited` | Rate limit exceeded. `Retry-After` gives the seconds to wait |
|
||||
| 431 | `headers_too_large` | Request headers too large |
|
||||
| 500 | `internal_error` | Unexpected failure. The cause is logged and reported to Sentry, never sent to the client |
|
||||
@@ -230,8 +244,8 @@ to the printer unless the whole receipt rendered.
|
||||
|
||||
A job is `{"elements": [...], "copies": 1}`. `copies` is 1–5, and each copy is
|
||||
cut separately. A partial cut is added after the last element unless that
|
||||
element is already a `cut`. Boxes, rules and key-value rows are described
|
||||
under [Blocks](#blocks).
|
||||
element is already a `cut`. Boxes, rules, key-value rows and lists are
|
||||
described under [Blocks](#blocks).
|
||||
|
||||
### `text`
|
||||
|
||||
@@ -327,10 +341,10 @@ or link-local addresses (including cloud metadata endpoints) are refused unless
|
||||
|
||||
## Blocks
|
||||
|
||||
`box`, `rule` and `kv` are blocks: they are laid out when the job prints, from
|
||||
the printer model, so one job fits the TM-T88's 42 columns as well as the
|
||||
TM-T20's 48. A block starts on a new line, and what follows it starts at the
|
||||
left margin. These fields work on all three:
|
||||
`box`, `rule`, `kv` and `list` are blocks: they are laid out when the job
|
||||
prints, from the printer model, so one job fits the TM-T88's 42 columns as
|
||||
well as the TM-T20's 48. A block starts on a new line, and what follows it
|
||||
starts at the left margin. These fields work on all four:
|
||||
|
||||
| Field | Default | Values |
|
||||
|-----------|---------|--------|
|
||||
@@ -389,6 +403,7 @@ borders:
|
||||
| `feed` | `lines` | Empty lines between the borders |
|
||||
| `rule` | `style` (`single` or `double`), `text`, `align`, `bold` | A divider joined to the side borders: `├──┤`, `╞══╡`, `╟──╢` or `╠══╣` to suit the box. A label reads `├── Label ───┤` |
|
||||
| `kv` | `pairs`, `leader` | Key-value rows across the inside of the box |
|
||||
| `list` | `items`, `bullet`, `done` | A list across the inside of the box |
|
||||
|
||||
`box`, `qr`, `barcode`, `image`, `raw` and `cut` cannot go inside a box;
|
||||
place them after it. Children count toward the 500-element limit.
|
||||
@@ -426,6 +441,47 @@ A value too long for its line wraps into the room after the key, and its
|
||||
further lines are right-aligned. A key too long for the line is cut with
|
||||
`...`.
|
||||
|
||||
### `list`
|
||||
|
||||
```json
|
||||
{"type": "box", "title": "Shopping", "footer": "29 Nov 21:04", "children": [
|
||||
{"type": "text", "text": "Milk, eggs and the good bread from the bakery on Elm St."},
|
||||
{"type": "feed"},
|
||||
{"type": "list", "bullet": "checkbox", "items": ["Milk", "Eggs", "Bread"], "done": [2]}
|
||||
]}
|
||||
```
|
||||
|
||||
```text
|
||||
┌─ Shopping ─────────────────────────────┐
|
||||
│ Milk, eggs and the good bread from the │
|
||||
│ bakery on Elm St. │
|
||||
│ │
|
||||
│ [ ] Milk │
|
||||
│ [ ] Eggs │
|
||||
│ [√] Bread │
|
||||
└──────────────────────── 29 Nov 21:04 ──┘
|
||||
```
|
||||
|
||||
| Field | Default | Values |
|
||||
|----------|---------|--------|
|
||||
| `items` | | Required. 1–200 strings, none empty |
|
||||
| `bullet` | `dot` | `dot` `· `, `dash` `- `, `number` `1. ` (right-aligned to the widest number), or `checkbox` `[ ] ` |
|
||||
| `done` | | Indexes into `items`, from 0, of the ticked items of a `checkbox` list: they print as `[√] `. Each index once |
|
||||
|
||||
An item too long for its line wraps, and its further lines are indented to
|
||||
line up under its first. A `dot` list and a `number` list:
|
||||
|
||||
```text
|
||||
· Replace drive bay 3
|
||||
· Order 2x 8 TB
|
||||
1. First item that is long enough to wrap
|
||||
with a hanging indent
|
||||
2. Second
|
||||
```
|
||||
|
||||
On code pages without `√` (cp850, cp858, cp1252), a done item prints as
|
||||
`[x]`.
|
||||
|
||||
### Glyphs and code pages
|
||||
|
||||
CP437, the default, has every glyph the blocks draw with. Other code pages
|
||||
@@ -451,3 +507,69 @@ For your own text on CP437:
|
||||
receipt, for checking the borders on paper. Send it as the body of
|
||||
`/api/v1/print`, or look at it first with
|
||||
`uberbringer preview examples/blocks.json`.
|
||||
|
||||
## Modules
|
||||
|
||||
A module takes a few plain fields, its payload, and builds the receipt from
|
||||
them, so a script or a phone shortcut does not have to lay one out from
|
||||
elements. `GET /api/v1/modules` lists them:
|
||||
|
||||
```json
|
||||
[{"name": "note", "description": "Boxed note with an optional checklist"}]
|
||||
```
|
||||
|
||||
```sh
|
||||
curl -X POST https://printer.example.com/api/v1/print/note \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"title": "Shopping", "text": "Milk, eggs and the good bread from the bakery on Elm St.",
|
||||
"items": ["Milk", "Eggs", "Bread"], "done": [2]}'
|
||||
```
|
||||
|
||||
`POST /api/v1/print/:module` answers as `/api/v1/print` does: `202` with the
|
||||
job and a `Location` header, or `503` when the queue is full or closing.
|
||||
`POST /api/v1/preview/:module` takes the same payload and answers as
|
||||
[`/api/v1/preview`](#preview-a-receipt) does. On both routes:
|
||||
|
||||
- `204` with no body means the module had nothing to print.
|
||||
- `400` `invalid_payload` lists every problem with the payload in `details`.
|
||||
Payloads are decoded strictly: an unknown field, or a field name in
|
||||
another case, is a problem.
|
||||
- `404` `module_not_found` names an unknown module.
|
||||
- `422` `module_failed` means the module built a receipt that fails
|
||||
validation. That is a bug in the module, and it is logged.
|
||||
- The query may set `copies` (1–5, each copy cut separately) and `printer`,
|
||||
which is reserved: only `default` is accepted. Any other query parameter,
|
||||
or one given twice, is `400` `invalid_query`, and another printer name is
|
||||
`400` `unknown_printer`.
|
||||
|
||||
`uberbringer preview --module note payload.json` shows the receipt without
|
||||
the daemon.
|
||||
|
||||
### `note`
|
||||
|
||||
A box with the text, then, when there are items, an empty line and a
|
||||
checklist. The time it was built, in `format.timezone` and
|
||||
`format.time_layout` ([configuration](../README.md#configuration)), goes in
|
||||
the bottom border. The request above prints:
|
||||
|
||||
```text
|
||||
┌─ Shopping ─────────────────────────────┐
|
||||
│ Milk, eggs and the good bread from the │
|
||||
│ bakery on Elm St. │
|
||||
│ │
|
||||
│ [ ] Milk │
|
||||
│ [ ] Eggs │
|
||||
│ [√] Bread │
|
||||
└──────────────────────── 29 Nov 21:04 ──┘
|
||||
```
|
||||
|
||||
| Field | Default | Values |
|
||||
|----------|----------|--------|
|
||||
| `text` | | Required. The body; it wraps at word boundaries, and `\n` starts a new paragraph |
|
||||
| `title` | | Set into the top border; a long title is cut with `...` |
|
||||
| `items` | | Up to 200 checklist items, none empty |
|
||||
| `done` | | Indexes into `items`, from 0, of the ticked items. Each index once |
|
||||
| `style` | `single` | The border: `single`, `double`, `double-h` or `double-v`, as on a [`box`](#box) |
|
||||
| `footer` | | Text for the bottom border in place of the time |
|
||||
| `stamp` | `true` | `false` leaves the bottom border empty when there is no `footer` |
|
||||
Reference in new issue
Block a user