understanding commited on
Commit
b16da09
·
verified ·
1 Parent(s): 4a35888

Create app/cf_api.py

Browse files
Files changed (1) hide show
  1. app/cf_api.py +109 -0
app/cf_api.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+ import httpx
3
+ from typing import Any, Dict, Optional
4
+
5
+ class CFClient:
6
+ def __init__(self, worker1_url: str, worker2_url: str, bot_backend_key: str, hf_api_key: str):
7
+ self.w1 = worker1_url.rstrip("/")
8
+ self.w2 = worker2_url.rstrip("/")
9
+ self.bot_backend_key = bot_backend_key
10
+ self.hf_api_key = hf_api_key
11
+ self._client = httpx.AsyncClient(timeout=httpx.Timeout(60.0, read=120.0))
12
+
13
+ async def close(self):
14
+ await self._client.aclose()
15
+
16
+ # ---------- Worker2 (HF-only) ----------
17
+ async def is_allowed(self, tg_id: str) -> bool:
18
+ r = await self._client.post(
19
+ f"{self.w2}/api/is_allowed",
20
+ headers={"Authorization": f"Bearer {self.hf_api_key}"},
21
+ json={"tg_id": tg_id},
22
+ )
23
+ if r.status_code != 200:
24
+ return False
25
+ j = r.json()
26
+ return bool(j.get("allowed", False))
27
+
28
+ async def allow_user(self, tg_id: str) -> Dict[str, Any]:
29
+ return await self._post_w2("/api/allow_user", {"tg_id": tg_id})
30
+
31
+ async def disallow_user(self, tg_id: str) -> Dict[str, Any]:
32
+ return await self._post_w2("/api/disallow_user", {"tg_id": tg_id})
33
+
34
+ async def list_profiles_w2(self, tg_id: str) -> Dict[str, Any]:
35
+ return await self._post_w2("/api/list_profiles", {"tg_id": tg_id})
36
+
37
+ async def pick_profile(self, tg_id: str, channel_id: str, rotate_after: int) -> Dict[str, Any]:
38
+ return await self._post_w2("/api/pick_profile", {"tg_id": tg_id, "channel_id": channel_id, "rotate_after": rotate_after})
39
+
40
+ async def access_token(self, tg_id: str, profile_id: str) -> Dict[str, Any]:
41
+ return await self._post_w2("/api/access_token", {"tg_id": tg_id, "profile_id": profile_id})
42
+
43
+ async def record_upload(self, tg_id: str, profile_id: str) -> Dict[str, Any]:
44
+ return await self._post_w2("/api/record_upload", {"tg_id": tg_id, "profile_id": profile_id})
45
+
46
+ async def stats_today(self) -> Dict[str, Any]:
47
+ return await self._post_w2("/api/stats_today", {})
48
+
49
+ async def log_error(self, tg_id: str, profile_id: str, where: str, err: str) -> None:
50
+ try:
51
+ await self._post_w2("/api/log_error", {"tg_id": tg_id, "profile_id": profile_id, "where": where, "err": err})
52
+ except Exception:
53
+ pass
54
+
55
+ async def _post_w2(self, path: str, body: Dict[str, Any]) -> Dict[str, Any]:
56
+ r = await self._client.post(
57
+ f"{self.w2}{path}",
58
+ headers={"Authorization": f"Bearer {self.hf_api_key}"},
59
+ json=body,
60
+ )
61
+ try:
62
+ return r.json()
63
+ except Exception:
64
+ return {"ok": False, "err": f"bad_json_{r.status_code}"}
65
+
66
+ # ---------- Worker1 (BOT backend key) ----------
67
+ async def profile_add(self, tg_id: str, client_id: str, client_secret: str, label: str = "main", ttl_sec: int = 600) -> Dict[str, Any]:
68
+ r = await self._client.post(
69
+ f"{self.w1}/api/profile/add",
70
+ headers={"Authorization": f"Bearer {self.bot_backend_key}"},
71
+ json={"tg_id": tg_id, "client_id": client_id, "client_secret": client_secret, "label": label, "ttl_sec": ttl_sec},
72
+ )
73
+ try:
74
+ return r.json()
75
+ except Exception:
76
+ return {"ok": False, "err": f"bad_json_{r.status_code}"}
77
+
78
+ async def profile_list_w1(self, tg_id: str) -> Dict[str, Any]:
79
+ r = await self._client.post(
80
+ f"{self.w1}/api/profile/list",
81
+ headers={"Authorization": f"Bearer {self.bot_backend_key}"},
82
+ json={"tg_id": tg_id},
83
+ )
84
+ try:
85
+ return r.json()
86
+ except Exception:
87
+ return {"ok": False, "err": f"bad_json_{r.status_code}"}
88
+
89
+ async def profile_set_default(self, tg_id: str, profile_id: str) -> Dict[str, Any]:
90
+ r = await self._client.post(
91
+ f"{self.w1}/api/profile/set_default",
92
+ headers={"Authorization": f"Bearer {self.bot_backend_key}"},
93
+ json={"tg_id": tg_id, "profile_id": profile_id},
94
+ )
95
+ try:
96
+ return r.json()
97
+ except Exception:
98
+ return {"ok": False, "err": f"bad_json_{r.status_code}"}
99
+
100
+ async def profile_remove(self, tg_id: str, profile_id: str) -> Dict[str, Any]:
101
+ r = await self._client.post(
102
+ f"{self.w1}/api/profile/remove",
103
+ headers={"Authorization": f"Bearer {self.bot_backend_key}"},
104
+ json={"tg_id": tg_id, "profile_id": profile_id},
105
+ )
106
+ try:
107
+ return r.json()
108
+ except Exception:
109
+ return {"ok": False, "err": f"bad_json_{r.status_code}"}