understanding commited on
Commit
5bf0f62
·
verified ·
1 Parent(s): ebafdb9

Update bot/integrations/cf_worker1.py

Browse files
Files changed (1) hide show
  1. bot/integrations/cf_worker1.py +52 -6
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 {"Authorization": f"Bearer {Workers.BOT_BACKEND_KEY}", "Content-Type": "application/json"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- async def profile_add(tg_id: int, client_id: str, client_secret: str, label: str = "main", ttl_sec: int = 600) -> dict:
9
- return await post_json(
 
 
 
 
 
 
 
10
  f"{Workers.WORKER1_URL}/api/profile/add",
11
  _h(),
12
- {"tg_id": str(tg_id), "client_id": client_id, "client_secret": client_secret, "label": label, "ttl_sec": ttl_sec},
 
 
 
 
 
 
13
  )
 
 
14
 
15
  async def profile_list(tg_id: int) -> dict:
16
- return await post_json(f"{Workers.WORKER1_URL}/api/profile/list", _h(), {"tg_id": str(tg_id)})
 
 
 
 
 
 
17
 
18
  async def profile_set_default(tg_id: int, profile_id: str) -> dict:
19
- return await post_json(f"{Workers.WORKER1_URL}/api/profile/set_default", _h(), {"tg_id": str(tg_id), "profile_id": profile_id})
 
 
 
 
 
 
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)