diet-guard/diet_guard/_kuchnia_client.py
Krzysztof kuhy Rudnicki 26f1770090 feat: import Kuchnia Wikinga catering deliveries into the food bank
Pulls the day's delivered dishes and their real per-portion macros out of
panel.kuchniavikinga.pl, so a catering meal is one keypress instead of five
retyped numbers.

A delivered meal is not an eaten meal, so nothing is ever logged unattended:
dishes land in the *curated* bank, and entries are written only after an
explicit confirmation. Auto-logging would let the gate satisfy its own
checkpoint from a delivery note, and would record what the courier dropped off
rather than what was eaten.

Three surfaces:
  - `diet-guard kuchnia [--log] [--yes]`
  - the lock screen's "Today's delivery" button, which prefills the meal form
    dish by dish (the user still clicks Log & Continue)
  - a warm-up after each logged meal, on the existing background thread

All event-driven; no new systemd timer. The two automatic paths go through
`refresh_delivery_once`, guarded to one fetch per day.

The panel is an undocumented white-labelled Dietly API; the endpoint map was
recovered from its JS bundles and confirmed against live responses. Notes,
including the traps, are in docs/kuchnia-wikinga.md.

Load-bearing details:

* Macros are per portion and `weight` is that portion in grams -- proven, not
  assumed: 4P+4C+9F reproduces the stated calories within ~1% on every meal of
  the captured day. `_energy_is_consistent` re-runs that check per dish and
  drops any that fails; a per-100g mix-up is invisible in a payload and would
  silently skew every logged meal by a factor of several.
* `bank_dishes` compares nutritional fields before calling `add_manual_entry`,
  which restamps `t` unconditionally. Without the compare, every refresh
  republishes the whole curated bank to every peer. Tests assert on call count.
* Duplicate log suppression reads today's synced log, not a local marker:
  `log_meal` mints a fresh uuid, so a re-import after marker loss would merge
  as duplicates on every peer.
* The gate hook is in `on_focus_ready`, not `_cli_gate._should_lock`:
  `gate_is_due()` never reads the food bank, so a refresh there could not
  change the lock decision while adding a third-party round trip to the ~105ms
  `gate --check` fast path.
* `requests` is lazy-imported so the gate's not-due tick does not pay for it.
* Credentials and the cached session cookie live in ~/.config/diet_guard at
  mode 600, outside the synced tree. The credentials file is written by hand,
  never by the package -- the same contract as sync_token.

`_GateDashboard` is merged into `_GateMealFlow`, and both "pull it in from
elsewhere" flows share one `_PullFlows` mixin: pylint's max-parents counts
every MRO entry, so the gate's controller chain had no room for a new class.

Also adds a side-by-side row for the two pull buttons -- stacking them cost
42px and pushed the tab past the viewport at 1366x768.

989 tests, 100% branch coverage, pre-commit clean.
2026-08-22 23:04:22 +02:00

166 lines
5.9 KiB
Python

"""HTTP access to the Kuchnia Wikinga panel.
Auth is a cookie session, not a bearer token: ``POST auth/login`` with a
**form-urlencoded** body sets a ``SESSION`` cookie that every later call
carries. The cookie is cached (:mod:`diet_guard._kuchnia_config`) so a refresh
normally skips the login round trip entirely, and an expired cookie costs one
extra request rather than a failed import.
``requests`` is imported lazily. The gate's not-due tick imports the CLI and
must not pay ~78ms for an HTTP stack it never touches -- the same reason
:mod:`diet_guard._estimator_off` and :mod:`diet_guard._cli_prune` defer theirs.
Call sites reach through ``sys.modules[__name__]`` so a test's ``patch.object``
still wins over the lazy hook.
"""
from __future__ import annotations
from importlib import import_module
import sys
import time
from typing import TYPE_CHECKING
from diet_guard._constants import (
KUCHNIA_API_BASE,
KUCHNIA_COMPANY,
KUCHNIA_LAUNCHER_TYPE,
KUCHNIA_TIMEOUT_SECONDS,
KUCHNIA_TOTAL_DEADLINE_SECONDS,
)
from diet_guard._kuchnia_config import (
SESSION_COOKIE,
clear_session_cookie,
load_session_cookie,
read_credentials,
save_session_cookie,
)
from diet_guard._kuchnia_errors import KuchniaError
if TYPE_CHECKING: # pragma: no cover - typing only
import requests
_LAZY_ATTRS = ("requests",)
_COOKIE_DOMAIN = "panel.kuchniavikinga.pl"
def __getattr__(name: str) -> object:
"""Resolve the deferred ``requests`` import on first attribute access."""
if name not in _LAZY_ATTRS:
msg = f"module {__name__!r} has no attribute {name!r}"
raise AttributeError(msg)
return import_module(name)
class PanelSession:
"""A logged-in panel session with a whole-walk deadline.
The deadline exists because the import is several sequential requests: a
per-request timeout alone would let a slow provider stack them into half a
minute, and this runs on paths a user is waiting on.
"""
def __init__(
self, deadline_seconds: float = KUCHNIA_TOTAL_DEADLINE_SECONDS
) -> None:
"""Build an unauthenticated session with the panel's required headers."""
module = sys.modules[__name__]
self._session = module.requests.Session()
# No session-level Content-Type: the login body is form-urlencoded and
# a sticky JSON default would mislabel it. ``requests`` sets it per call.
self._session.headers.update(
{
"company-id": KUCHNIA_COMPANY,
"X-Launcher-Type": KUCHNIA_LAUNCHER_TYPE,
"User-Agent": "diet_guard/1.0 (personal diet tracker)",
"Accept": "application/json",
},
)
self._expires_at = time.monotonic() + deadline_seconds
def _check_deadline(self) -> None:
"""Raise once the whole-walk budget is spent."""
if time.monotonic() >= self._expires_at:
msg = "catering panel too slow (deadline exceeded)"
raise KuchniaError(msg)
def _request(
self,
method: str,
path: str,
data: dict[str, str] | None = None,
) -> requests.Response:
"""Perform one request, translating transport errors into KuchniaError.
``data`` is the only body this client ever sends (the form-urlencoded
login), so it is spelled out rather than taken as ``**kwargs`` -- a
concrete signature types better and keeps the surface honest.
"""
self._check_deadline()
module = sys.modules[__name__]
try:
return self._session.request(
method,
f"{KUCHNIA_API_BASE}/{path}",
timeout=KUCHNIA_TIMEOUT_SECONDS,
data=data,
)
except module.requests.RequestException as exc:
msg = f"catering panel unreachable: {exc}"
raise KuchniaError(msg) from exc
def _login(self) -> None:
"""Authenticate with the stored credentials and cache the cookie."""
username, password = read_credentials()
response = self._request(
"POST",
"auth/login",
data={"username": username, "password": password},
)
if not response.ok:
msg = f"catering login rejected (HTTP {response.status_code})"
raise KuchniaError(msg)
cookie = self._session.cookies.get(SESSION_COOKIE)
if cookie is None:
msg = "catering login returned no session cookie"
raise KuchniaError(msg)
save_session_cookie(cookie)
def authenticate(self) -> None:
"""Attach the cached cookie, logging in only when there is not one."""
cached = load_session_cookie()
if cached is None:
self._login()
return
self._session.cookies.set(SESSION_COOKIE, cached, domain=_COOKIE_DOMAIN)
def get_json(self, path: str) -> object:
"""GET ``path`` and decode it, re-logging in once if the cookie expired.
A cached cookie that has expired is indistinguishable from a good one
until it is used, so an auth failure is retried exactly once with fresh
credentials. A second failure is real.
Args:
path: Path below the API base, without a leading slash.
Returns:
The decoded JSON body.
Raises:
KuchniaError: On a non-OK status or an undecodable body.
"""
response = self._request("GET", path)
if response.status_code in {401, 403}:
clear_session_cookie()
self._login()
response = self._request("GET", path)
if not response.ok:
msg = f"catering panel returned HTTP {response.status_code} for {path}"
raise KuchniaError(msg)
try:
return response.json()
except ValueError as exc:
msg = f"catering panel returned a non-JSON body for {path}"
raise KuchniaError(msg) from exc