Duplicated is_available/render boilerplate across slide types #9

Closed
opened 2026-07-28 21:02:07 +00:00 by paulloth1 · 1 comment
paulloth1 commented 2026-07-28 21:02:07 +00:00 (Migrated from github.com)

Problem

`backend/slides/mastodon.py`, `matrix.py`, and `train.py` each independently implement near-identical `is_available()`/`render()` pairs: open an `httpx.AsyncClient(timeout=3.0)`, GET a URL, catch `httpx.HTTPError`, return a "Unable to load ..." fallback `

` on failure. `webcam.py`'s `is_available()` follows the same shape with a streaming GET.

This is a fair amount of copy-pasted control flow for what's conceptually "fetch JSON from a URL, render a fallback on failure."

Suggested fix

Factor the common "fetch with timeout, catch HTTPError, return fallback HTML on failure" pattern into a small shared helper in `backend/slides/registry.py` (or a new `_http.py`), and have the four slide types call it. Not urgent — the current duplication is small and easy to follow — but worth doing before adding a 5th similar slide type.

## Problem \`backend/slides/mastodon.py\`, \`matrix.py\`, and \`train.py\` each independently implement near-identical \`is_available()\`/\`render()\` pairs: open an \`httpx.AsyncClient(timeout=3.0)\`, GET a URL, catch \`httpx.HTTPError\`, return a "Unable to load ..." fallback \`<div>\` on failure. \`webcam.py\`'s \`is_available()\` follows the same shape with a streaming GET. This is a fair amount of copy-pasted control flow for what's conceptually "fetch JSON from a URL, render a fallback on failure." ## Suggested fix Factor the common "fetch with timeout, catch HTTPError, return fallback HTML on failure" pattern into a small shared helper in \`backend/slides/registry.py\` (or a new \`_http.py\`), and have the four slide types call it. Not urgent — the current duplication is small and easy to follow — but worth doing before adding a 5th similar slide type.
paulloth1 commented 2026-07-28 21:23:03 +00:00 (Migrated from github.com)

Fixed in 5f58e3e (merged to main at 103cdf2): added backend/slides/_http.py with shared fetch_json()/url_ok() helpers; mastodon.py, matrix.py, and train.py now use them instead of each hand-rolling their own httpx try/except block. Same URLs, headers, and fallback HTML preserved.

Fixed in 5f58e3e (merged to main at 103cdf2): added backend/slides/_http.py with shared fetch_json()/url_ok() helpers; mastodon.py, matrix.py, and train.py now use them instead of each hand-rolling their own httpx try/except block. Same URLs, headers, and fallback HTML preserved.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
paul_loth/krautspaceTV#9
No description provided.