-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequesting.py
More file actions
37 lines (32 loc) · 1.48 KB
/
Copy pathrequesting.py
File metadata and controls
37 lines (32 loc) · 1.48 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
import httpx
from typing import Optional, Any
class RequestManager:
def __init__(self, base_url: Optional[str] = None):
self.base_url = base_url
# Создаем клиент, но не открываем его сразу
self.client = httpx.AsyncClient(
base_url=base_url or "",
timeout=httpx.Timeout(30, connect=5.0),
headers={"User-Agent": "MaxUserBot/1.0"}
)
async def get(self, endpoint: str, params: Optional[dict] = None) -> Any:
response = await self.client.get(endpoint, params=params)
return self._handle_response(response)
async def post(self, endpoint: str, data: Optional[dict] = None, json: Optional[dict] = None) -> Any:
response = await self.client.post(endpoint, data=data, json=json)
return self._handle_response(response)
@staticmethod
def _handle_response(response: httpx.Response):
"""Централизованная обработка статусов"""
try:
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
print(f"[HTTP Error] {e.response.status_code}: {e.response.text}")
return None
except Exception as e:
print(f"[JSON Error] Не удалось распарсить ответ: {e}")
return None
async def close(self):
"""Закрытие сессии при выходе"""
await self.client.aclose()