"""Модлуль для конструирования и обработки запросов.""" from app_config import AppConfig from aiohttp import ClientResponse import asyncio import aiohttp class RequestBulder: def __init__(self, cfg: AppConfig): self.cfg = cfg _conn = aiohttp.TCPConnector(limit=cfg.requests_count) self.session = aiohttp.ClientSession(connector=_conn) async def __check_resp(self, resp: ClientResponse) -> bool: if not resp.status == 200: return False response = await resp.json() if not response["done"]: return False return True async def send_request(self, url, json_body) -> dict | bool: """Выполняет запрос, при успехе возвращает json с ответом, при неудаче возвращает False.""" _url = f"{self.cfg.central_host_url}/{url}" raw_resp = await self.session.get(_url, json=json_body) if await self.__check_resp(raw_resp): json_resp = await raw_resp.json() json_resp.pop("done") return json_resp return False async def wait(self) -> None: """Ждет frequency_sec время.""" await asyncio.sleep(self.cfg.frequency_sec) # def __del__(self): # if self.session is not None: # self.session.close()