requests_aiohttp.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. from __future__ import annotations
  2. from aiohttp import ClientSession, ClientResponse, ClientTimeout
  3. from typing import AsyncGenerator, Any
  4. from .Provider.helper import get_connector
  5. from .defaults import DEFAULT_HEADERS
  6. class StreamResponse(ClientResponse):
  7. async def iter_lines(self) -> AsyncGenerator[bytes, None]:
  8. async for line in self.content:
  9. yield line.rstrip(b"\r\n")
  10. async def json(self) -> Any:
  11. return await super().json(content_type=None)
  12. class StreamSession(ClientSession):
  13. def __init__(self, headers: dict = {}, timeout: int = None, proxies: dict = {}, impersonate = None, **kwargs):
  14. if impersonate:
  15. headers = {
  16. **DEFAULT_HEADERS,
  17. **headers
  18. }
  19. super().__init__(
  20. **kwargs,
  21. timeout=ClientTimeout(timeout) if timeout else None,
  22. response_class=StreamResponse,
  23. connector=get_connector(kwargs.get("connector"), proxies.get("https")),
  24. headers=headers
  25. )