Duplicated owner/forwarder branching across 4 endpoints in app.py #18

Closed
opened 2026-08-13 19:25:17 +00:00 by paulloth1 · 1 comment
paulloth1 commented 2026-08-13 19:25:17 +00:00 (Migrated from github.com)

Problem

Four endpoints in backend/app.py each repeat the same "if this process is the rotation owner, handle locally; otherwise forward to the owner over loopback HTTP, swallowing httpx.HTTPError" branch:

  • slide_current (line 115-129)
  • slide_visible (line 132-141)
  • report_slide_visible (line 144-157)
  • view_now (line 322-332)

Each copy re-implements the same httpx.AsyncClient(timeout=5) + try/except pattern with slightly different request shapes (GET vs POST, query params vs none, different fallback payloads on failure). This is easy to get subtly inconsistent as more owner-forwarded endpoints get added (e.g. a future one might forget the try/except, or use a different timeout).

Suggested fix

Extract a small helper, e.g.:

async def _forward_to_owner(method: str, path: str, **kwargs):
    try:
        async with httpx.AsyncClient(timeout=5) as client:
            resp = await client.request(method, f"{ROTATION_OWNER_URL}{path}", **kwargs)
            return resp
    except httpx.HTTPError:
        return None

and have each forwarding branch call it, handling the per-endpoint fallback response shape at the call site. Reduces the duplicated branching from 4 copies to 1.

Where

backend/app.py:115-157, backend/app.py:322-332

## Problem Four endpoints in `backend/app.py` each repeat the same "if this process is the rotation owner, handle locally; otherwise forward to the owner over loopback HTTP, swallowing `httpx.HTTPError`" branch: - `slide_current` (line 115-129) - `slide_visible` (line 132-141) - `report_slide_visible` (line 144-157) - `view_now` (line 322-332) Each copy re-implements the same `httpx.AsyncClient(timeout=5)` + try/except pattern with slightly different request shapes (GET vs POST, query params vs none, different fallback payloads on failure). This is easy to get subtly inconsistent as more owner-forwarded endpoints get added (e.g. a future one might forget the try/except, or use a different timeout). ## Suggested fix Extract a small helper, e.g.: ```python async def _forward_to_owner(method: str, path: str, **kwargs): try: async with httpx.AsyncClient(timeout=5) as client: resp = await client.request(method, f"{ROTATION_OWNER_URL}{path}", **kwargs) return resp except httpx.HTTPError: return None ``` and have each forwarding branch call it, handling the per-endpoint fallback response shape at the call site. Reduces the duplicated branching from 4 copies to 1. ## Where `backend/app.py:115-157`, `backend/app.py:322-332`
paulloth1 commented 2026-08-13 19:31:08 +00:00 (Migrated from github.com)

Fixed and deployed. Extracted _forward_to_owner(method, path, **kwargs) in backend/app.py and switched all 4 owner/forwarder endpoints (slide_current, slide_visible, report_slide_visible, view_now) to use it — same requests/fallback behavior, one shared implementation instead of four copies.

Fixed and deployed. Extracted `_forward_to_owner(method, path, **kwargs)` in backend/app.py and switched all 4 owner/forwarder endpoints (slide_current, slide_visible, report_slide_visible, view_now) to use it — same requests/fallback behavior, one shared implementation instead of four copies.
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#18
No description provided.