Spaces:
Runtime error
Runtime error
Update bot/integrations/cf_worker1.py
Browse files
bot/integrations/cf_worker1.py
CHANGED
|
@@ -2,18 +2,64 @@
|
|
| 2 |
from bot.config import Workers
|
| 3 |
from bot.integrations.http import post_json
|
| 4 |
|
|
|
|
| 5 |
def _h():
|
| 6 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
f"{Workers.WORKER1_URL}/api/profile/add",
|
| 11 |
_h(),
|
| 12 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
|
|
|
|
|
|
| 14 |
|
| 15 |
async def profile_list(tg_id: int) -> dict:
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
async def profile_set_default(tg_id: int, profile_id: str) -> dict:
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from bot.config import Workers
|
| 3 |
from bot.integrations.http import post_json
|
| 4 |
|
| 5 |
+
|
| 6 |
def _h():
|
| 7 |
+
return {
|
| 8 |
+
"Authorization": f"Bearer {Workers.BOT_BACKEND_KEY}",
|
| 9 |
+
"Content-Type": "application/json",
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def _unwrap(r: dict) -> dict:
|
| 14 |
+
"""
|
| 15 |
+
post_json() may return:
|
| 16 |
+
A) direct backend json -> {"ok": true, ...}
|
| 17 |
+
B) wrapper -> {"ok": true, "status": 200, "data": {...}}
|
| 18 |
+
handlers.py expects (A), so convert (B) -> (A)
|
| 19 |
+
"""
|
| 20 |
+
if isinstance(r, dict) and isinstance(r.get("data"), dict):
|
| 21 |
+
data = r["data"]
|
| 22 |
+
# keep fallback ok/status for debugging if you want
|
| 23 |
+
if "ok" not in data and "ok" in r:
|
| 24 |
+
data["ok"] = r["ok"]
|
| 25 |
+
return data
|
| 26 |
+
return r
|
| 27 |
|
| 28 |
+
|
| 29 |
+
async def profile_add(
|
| 30 |
+
tg_id: int,
|
| 31 |
+
client_id: str,
|
| 32 |
+
client_secret: str,
|
| 33 |
+
label: str = "main",
|
| 34 |
+
ttl_sec: int = 600,
|
| 35 |
+
) -> dict:
|
| 36 |
+
r = await post_json(
|
| 37 |
f"{Workers.WORKER1_URL}/api/profile/add",
|
| 38 |
_h(),
|
| 39 |
+
{
|
| 40 |
+
"tg_id": str(tg_id),
|
| 41 |
+
"client_id": client_id,
|
| 42 |
+
"client_secret": client_secret,
|
| 43 |
+
"label": label,
|
| 44 |
+
"ttl_sec": ttl_sec,
|
| 45 |
+
},
|
| 46 |
)
|
| 47 |
+
return _unwrap(r)
|
| 48 |
+
|
| 49 |
|
| 50 |
async def profile_list(tg_id: int) -> dict:
|
| 51 |
+
r = await post_json(
|
| 52 |
+
f"{Workers.WORKER1_URL}/api/profile/list",
|
| 53 |
+
_h(),
|
| 54 |
+
{"tg_id": str(tg_id)},
|
| 55 |
+
)
|
| 56 |
+
return _unwrap(r)
|
| 57 |
+
|
| 58 |
|
| 59 |
async def profile_set_default(tg_id: int, profile_id: str) -> dict:
|
| 60 |
+
r = await post_json(
|
| 61 |
+
f"{Workers.WORKER1_URL}/api/profile/set_default",
|
| 62 |
+
_h(),
|
| 63 |
+
{"tg_id": str(tg_id), "profile_id": profile_id},
|
| 64 |
+
)
|
| 65 |
+
return _unwrap(r)
|