understanding's picture
Update bot/integrations/cf_worker1.py
eeba9fe verified
# PATH: bot/integrations/cf_worker1.py
from __future__ import annotations
from bot.config import Workers
from bot.integrations.http import post_json
def _h():
return {
"Authorization": f"Bearer {Workers.BOT_BACKEND_KEY}",
"Content-Type": "application/json",
}
def _unwrap(r: dict) -> dict:
"""
post_json() may return:
A) direct backend json -> {"ok": true, ...}
B) wrapper -> {"ok": true, "status": 200, "data": {...}}
handlers.py expects (A), so convert (B) -> (A)
"""
if isinstance(r, dict) and isinstance(r.get("data"), dict):
data = r["data"]
if "ok" not in data and "ok" in r:
data["ok"] = r["ok"]
return data
return r
async def profile_add(
tg_id: int,
client_id: str,
client_secret: str,
label: str = "main",
ttl_sec: int = 600,
) -> dict:
r = await post_json(
f"{Workers.WORKER1_URL}/api/profile/add",
_h(),
{
"tg_id": str(tg_id),
"client_id": client_id,
"client_secret": client_secret,
"label": label,
"ttl_sec": ttl_sec,
},
)
return _unwrap(r)
# existing (kept)
async def profile_list(tg_id: int) -> dict:
r = await post_json(
f"{Workers.WORKER1_URL}/api/profile/list",
_h(),
{"tg_id": str(tg_id)},
)
return _unwrap(r)
# existing (kept)
async def profile_set_default(tg_id: int, profile_id: str) -> dict:
r = await post_json(
f"{Workers.WORKER1_URL}/api/profile/set_default",
_h(),
{"tg_id": str(tg_id), "profile_id": profile_id},
)
return _unwrap(r)
# ✅ Added: handlers.py expects this name
async def profile_check_auth(tg_id: int, profile_id: str, ttl_sec: int = 600, force: bool = False) -> dict:
r = await post_json(
f"{Workers.WORKER1_URL}/api/profile/login_link",
_h(),
{"tg_id": str(tg_id), "profile_id": str(profile_id), "ttl_sec": int(ttl_sec), "force": bool(force)},
)
return _unwrap(r)
# ✅ Added: handlers.py expects this name
async def profile_delete(tg_id: int, profile_id: str) -> dict:
r = await post_json(
f"{Workers.WORKER1_URL}/api/profile/remove",
_h(),
{"tg_id": str(tg_id), "profile_id": str(profile_id)},
)
return _unwrap(r)