Spaces:
Sleeping
Sleeping
| # ============================================================================ | |
| # HTTP CLIENT MANAGER | |
| # ============================================================================ | |
| import aiohttp | |
| from typing import Optional | |
| from app.config import Config | |
| from app.utils.logger import logger | |
| class HTTPClientManager: | |
| """Manages aiohttp session lifecycle""" | |
| def __init__(self): | |
| self.session: Optional[aiohttp.ClientSession] = None | |
| async def start(self): | |
| timeout = aiohttp.ClientTimeout(total=Config.REQUEST_TIMEOUT) | |
| self.session = aiohttp.ClientSession(timeout=timeout) | |
| logger.info("HTTP client session started") | |
| async def close(self): | |
| if self.session: | |
| await self.session.close() | |
| logger.info("HTTP client session closed") | |
| def get_session(self) -> aiohttp.ClientSession: | |
| if not self.session: | |
| raise RuntimeError("HTTP client not initialized") | |
| return self.session | |
| # Global instance | |
| http_client = HTTPClientManager() | |