import aiohttp
import asyncio

from global_logger import logger


class AsyncHTTPClient:
    def __init__(self, url, timeout):
        self.url = url
        self.timeout = timeout
        self.queue = asyncio.Queue()

    async def send_loop(self):
        async with aiohttp.ClientSession() as session:
            while True:
                data = await self.queue.get()
                try:
                    async with session.post(self.url, json=data, timeout=self.timeout) as response:
                        logger.info(f"HTTP 响应: {await response.text()}")
                except Exception as e:
                    logger.exception(f"HTTP 发送失败: {e}, 数据: {data}")

    async def send(self, data: dict):
        await self.queue.put(data)
