This commit is contained in:
2025-06-17 23:20:49 +03:00
parent eff34b42c2
commit ff19f99704
65 changed files with 1803 additions and 0 deletions

49
maxapi/connection/base.py Normal file
View File

@@ -0,0 +1,49 @@
import aiohttp
from pydantic import BaseModel
from ..types.errors import Error
from ..enums.http_method import HTTPMethod
from ..enums.api_path import ApiPath
class BaseConnection:
API_URL = 'https://botapi.max.ru'
def __init__(self):
self.bot = None
async def request(
self,
method: HTTPMethod,
path: ApiPath,
model: BaseModel,
is_return_raw: bool = False,
**kwargs
):
async with aiohttp.ClientSession(self.API_URL) as s:
r = await s.request(
method=method.value,
url=path.value if isinstance(path, ApiPath) else path,
**kwargs
)
if not r.ok:
raw = await r.text()
return Error(code=r.status, text=raw)
raw = await r.json()
if is_return_raw: return raw
model = model(**raw)
if hasattr(model, 'message'):
attr = getattr(model, 'message')
if hasattr(attr, 'bot'):
attr.bot = self.bot
if hasattr(model, 'bot'):
model.bot = self.bot
return model