3
|
1 import asyncio
|
|
2 from dataclasses import dataclass
|
|
3 from pathlib import Path
|
|
4
|
|
5 import aiohttp
|
|
6
|
|
7
|
|
8 @dataclass
|
|
9 class HttpServer:
|
|
10 server_path: Path
|
|
11
|
|
12 async def __aenter__(self):
|
|
13 self.subprocess = await asyncio.create_subprocess_exec(
|
|
14 'pdm', 'run', 'python', self.server_path)
|
|
15 self._session = await aiohttp.ClientSession().__aenter__()
|
|
16 return self
|
|
17
|
|
18 async def __aexit__(self, exc_type, exc, tb):
|
|
19 self.subprocess.terminate()
|
|
20
|
|
21 async def get(self, url: str) -> aiohttp.ClientResponse:
|
|
22 while True:
|
|
23 try:
|
|
24 return await self._session.get(url)
|
|
25 except aiohttp.ClientConnectorError:
|
|
26 await asyncio.sleep(0.05)
|