diet-guard/app/lib/services/kuchnia_client.dart
Krzysztof kuhy Rudnicki 3d82ddefd4 feat: let the phone fetch and bank the catering delivery itself
The phone now walks the panel on its own -- login, active order, delivery,
menu -- parses the dishes and banks them, so the feature works with the PC
switched off. Read-only: nothing here logs a meal.

`package:http` has no cookie jar, so the `SESSION` cookie is read off the login
response and replayed by hand. `Response.headers` folds duplicate `set-cookie`
values into one comma-joined string, so the parser splits on both `,` and `;`
and looks for the named cookie rather than assuming the header holds only it --
tested against both orderings and against a panel that sets no session cookie
at all.

Android-only, as decided: the panel sends no CORS headers, so a browser cannot
call it. `refreshDelivery` checks `kIsWeb` first and returns the unsupported
reason without touching the network. No `dart:io` anywhere -- `package:http`
compiles for web, it is the panel that refuses, not the toolchain, so this
needs no `_io`/`_web` seam.

The idempotency guard is ported with its teeth: `addManualEntry` restamps
`editedAt` unconditionally, so re-banking an unchanged dish republishes the
whole curated bank to every peer. `bankDishes` compares the nutritional fields
first and returns a write *count*, which is what the tests assert on -- "the
entry exists" passes while this misbehaves. The comparison is numeric, not
string, so `270 == 270.0` is a match exactly as in Python's `_matches`.

Every failure becomes a reason string, never a throw, so a catering outage
cannot break the caller: a rejected login, a delivery whose dishes all fail
the unit check, and missing credentials each have a test.

One test bug worth noting: the assertion that nothing is logged was missing an
`await` and so was comparing a Future against `isEmpty` -- the single most
important guarantee in this file was passing vacuously until the split forced
it to run.

Test files split three ways for the 250-line cap; block counts checked against
the pre-split file (16 either side), not just a green suite.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QQpCiPaSbyRpwsLkH3dgsX
2026-08-23 08:30:21 +02:00

193 lines
7.0 KiB
Dart

/// HTTP access to the Kuchnia Wikinga panel, from the phone.
///
/// The Dart mirror of `diet_guard/_kuchnia_client.py`. 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.
///
/// `package:http` has no cookie jar, so the `set-cookie` header is read and
/// replayed by hand. `Response.headers` folds duplicate `set-cookie` values
/// into one comma-joined string, which is why the parser below splits and
/// looks for the named cookie rather than assuming the header holds only it.
///
/// **Android only.** The panel sends no CORS headers, so a browser cannot call
/// it at all; on web every entry point returns an unsupported reason rather
/// than throwing. The `kIsWeb` check comes first, before anything
/// platform-shaped, and this file imports no `dart:io` -- `package:http`
/// compiles for web, it is the *panel* that refuses, not the toolchain.
library;
import 'dart:convert';
import 'package:diet_guard_app/services/kuchnia_errors.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
/// Base URL of the panel's undocumented API.
const kuchniaApiBase = 'https://panel.kuchniavikinga.pl/api';
/// Sent as the `company-id` header. The panel wants the company *name*, not a
/// numeric id.
const kuchniaCompany = 'kuchniavikinga';
/// The panel's own launcher tag for a browser panel session.
const kuchniaLauncherType = 'BROWSER_PANEL';
/// The only cookie the panel issues on login.
///
/// It sets no `XSRF-TOKEN`, so the CSRF echo its own JavaScript performs is a
/// no-op for this account -- the session cookie alone authenticates.
const kuchniaSessionCookie = 'SESSION';
/// Per-request timeout.
const kuchniaTimeout = Duration(seconds: 8);
/// Whole-walk ceiling.
///
/// The import is three or four sequential requests, so a per-request timeout
/// alone would permit ~32s against a slow provider -- and this runs on a path
/// the user is waiting on.
const kuchniaTotalDeadline = Duration(seconds: 12);
/// Why the catering panel cannot be reached from a browser.
const kuchniaWebUnsupportedReason =
'Android only — the catering panel blocks browser requests';
/// Extracts [name]'s value from a (possibly comma-joined) `set-cookie` header.
///
/// `http`'s `Response.headers` collapses repeated headers into one string, so
/// a panel that sets more than one cookie hands over
/// `SESSION=abc; Path=/, OTHER=def; Path=/`. Returns null when the cookie is
/// absent, which the caller treats as a failed login rather than guessing.
String? sessionCookieFrom(String? setCookieHeader) {
if (setCookieHeader == null || setCookieHeader.isEmpty) return null;
// Split on both separators: ',' joins whole cookies, ';' separates a
// cookie's attributes from its value.
for (final part in setCookieHeader.split(RegExp('[,;]'))) {
final trimmed = part.trim();
if (trimmed.startsWith('$kuchniaSessionCookie=')) {
final value = trimmed.substring(kuchniaSessionCookie.length + 1);
if (value.isNotEmpty) return value;
}
}
return null;
}
/// A logged-in panel session with a whole-walk deadline.
class KuchniaSession {
/// Creates a session over [client], authenticating lazily.
KuchniaSession({
required this.username,
required this.password,
http.Client? client,
DateTime? startedAt,
}) : _client = client ?? http.Client(),
_deadline =
(startedAt ?? DateTime.now()).add(kuchniaTotalDeadline);
/// The panel e-mail.
final String username;
/// The panel password.
final String password;
final http.Client _client;
final DateTime _deadline;
/// The session cookie: seeded from a cache to skip the login, and read back
/// afterwards so the caller can persist a freshly minted one.
///
/// Device-local by design -- it is regenerable from the password, so syncing
/// it would widen exposure and buy nothing.
String? sessionCookie;
Map<String, String> get _headers => {
'company-id': kuchniaCompany,
'X-Launcher-Type': kuchniaLauncherType,
'User-Agent': 'diet_guard/1.0 (personal diet tracker)',
'Accept': 'application/json',
if (sessionCookie != null) 'Cookie': '$kuchniaSessionCookie=$sessionCookie',
};
void _checkDeadline() {
if (!DateTime.now().isBefore(_deadline)) {
throw const KuchniaError('catering panel too slow (deadline exceeded)');
}
}
/// Authenticates and caches the session cookie.
///
/// No client-wide `Content-Type`: the login body is form-urlencoded and a
/// sticky JSON default would mislabel it. Passing a `Map` to `http.post`
/// sets the right one automatically.
Future<void> login() async {
_checkDeadline();
final http.Response response;
try {
response = await _client
.post(
Uri.parse('$kuchniaApiBase/auth/login'),
headers: _headers,
body: {'username': username, 'password': password},
)
.timeout(kuchniaTimeout);
} on Exception catch (error) {
throw KuchniaError('catering panel unreachable: $error');
}
if (response.statusCode < 200 || response.statusCode >= 300) {
throw KuchniaError(
'catering login rejected (HTTP ${response.statusCode})',
);
}
final cookie = sessionCookieFrom(response.headers['set-cookie']);
if (cookie == null) {
throw const KuchniaError('catering login returned no session cookie');
}
sessionCookie = cookie;
}
/// GETs [path] and decodes 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.
Future<Object?> getJson(String path) async {
if (sessionCookie == null) await login();
var response = await _get(path);
if (response.statusCode == 401 || response.statusCode == 403) {
sessionCookie = null;
await login();
response = await _get(path);
}
if (response.statusCode < 200 || response.statusCode >= 300) {
throw KuchniaError(
'catering panel returned HTTP ${response.statusCode} for $path',
);
}
try {
return jsonDecode(response.body);
} on FormatException {
throw KuchniaError('catering panel returned a non-JSON body for $path');
}
}
Future<http.Response> _get(String path) async {
_checkDeadline();
try {
return await _client
.get(Uri.parse('$kuchniaApiBase/$path'), headers: _headers)
.timeout(kuchniaTimeout);
} on Exception catch (error) {
throw KuchniaError('catering panel unreachable: $error');
}
}
/// Releases the underlying HTTP client.
void close() => _client.close();
}
/// True when this platform can reach the catering panel at all.
///
/// False on web: the panel sends no CORS headers, so the browser refuses the
/// request before it leaves. Checked before anything platform-shaped runs.
bool get kuchniaFetchSupported => !kIsWeb;