Unmapped ohne Kreuzjoin, Listen-GET kurz, Rebuild im Hintergrund. Tandoor-Zugang (URL/Token) in den Einstellungen mit ausliefern. Co-authored-by: Cursor <cursoragent@cursor.com>
68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
"""Persist per-profile Tandoor credentials. Token never returned in full."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from data_layer.tandoor_client import public_settings
|
|
|
|
|
|
def get_settings(cur, profile_id: str) -> dict[str, Any] | None:
|
|
cur.execute(
|
|
"""
|
|
SELECT profile_id, base_url, api_token, last_ok_at, last_error, updated_at
|
|
FROM profile_tandoor_settings
|
|
WHERE profile_id = %s
|
|
""",
|
|
(profile_id,),
|
|
)
|
|
row = cur.fetchone()
|
|
return dict(row) if row else None
|
|
|
|
|
|
def get_public_settings(cur, profile_id: str) -> dict[str, Any]:
|
|
return public_settings(get_settings(cur, profile_id))
|
|
|
|
|
|
def upsert_settings(cur, profile_id: str, base_url: str, token: str | None, *, clear_token: bool = False) -> dict[str, Any]:
|
|
existing = get_settings(cur, profile_id)
|
|
if clear_token:
|
|
cur.execute("DELETE FROM profile_tandoor_settings WHERE profile_id = %s", (profile_id,))
|
|
return public_settings(None)
|
|
stored = (token or "").strip() or ((existing or {}).get("api_token") or "")
|
|
if not stored:
|
|
raise ValueError("API-Token fehlt")
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO profile_tandoor_settings (profile_id, base_url, api_token, last_error, updated_at)
|
|
VALUES (%s, %s, %s, NULL, NOW())
|
|
ON CONFLICT (profile_id) DO UPDATE SET
|
|
base_url = EXCLUDED.base_url,
|
|
api_token = EXCLUDED.api_token,
|
|
last_error = NULL,
|
|
updated_at = NOW()
|
|
""",
|
|
(profile_id, base_url, stored),
|
|
)
|
|
return get_public_settings(cur, profile_id)
|
|
|
|
|
|
def record_probe(cur, profile_id: str, ok: bool, error: str | None = None) -> None:
|
|
if ok:
|
|
cur.execute(
|
|
"""
|
|
UPDATE profile_tandoor_settings
|
|
SET last_ok_at = NOW(), last_error = NULL, updated_at = NOW()
|
|
WHERE profile_id = %s
|
|
""",
|
|
(profile_id,),
|
|
)
|
|
else:
|
|
cur.execute(
|
|
"""
|
|
UPDATE profile_tandoor_settings
|
|
SET last_error = %s, updated_at = NOW()
|
|
WHERE profile_id = %s
|
|
""",
|
|
((error or "")[:300], profile_id),
|
|
)
|