-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_client.py
More file actions
72 lines (60 loc) · 2.81 KB
/
api_client.py
File metadata and controls
72 lines (60 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import aiohttp
from typing import Optional, Dict, Any, List
class OneBotAPIClient:
"""Client for OneBot API"""
def __init__(self, base_url: str = "https://api.bot1.org"):
self.base_url = base_url.rstrip('/')
self.session: Optional[aiohttp.ClientSession] = None
async def _get_session(self) -> aiohttp.ClientSession:
"""Get or create aiohttp session"""
if self.session is None or self.session.closed:
self.session = aiohttp.ClientSession()
return self.session
async def _make_request(self, method: str, endpoint: str, api_key: str,
data: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
"""Make HTTP request to API"""
url = f"{self.base_url}{endpoint}"
headers = {
"Authorization": api_key,
"Content-Type": "application/x-www-form-urlencoded" if data else "application/json"
}
# Use a new session for each request to avoid unclosed session issues
async with aiohttp.ClientSession() as session:
try:
async with session.request(method, url, headers=headers, data=data) as response:
if response.status == 200:
return await response.json()
else:
error_text = await response.text()
raise Exception(f"API request failed: {response.status}")
except aiohttp.ClientError as e:
raise Exception(f"Network error: {e}")
async def get_balance(self, api_key: str) -> float:
"""Get account balance"""
result = await self._make_request("GET", "/balance", api_key)
return float(result)
async def register_udid(self, api_key: str, udid: str, plan: str) -> Dict[str, Any]:
"""Register UDID with selected plan"""
data = {
"udid": udid,
"register_plan": plan
}
return await self._make_request("POST", "/register", api_key, data)
async def get_certificate(self, api_key: str, udid: str = None,
certificate_id: str = None) -> List[Dict[str, Any]]:
"""Get certificate information"""
params = []
if udid:
params.append(f"udid={udid}")
if certificate_id:
params.append(f"certificate_id={certificate_id}")
endpoint = "/certificate"
if params:
endpoint += "?" + "&".join(params)
result = await self._make_request("GET", endpoint, api_key)
return result if isinstance(result, list) else [result]
async def close(self):
"""Close the session"""
if self.session and not self.session.closed:
await self.session.close()
self.session = None