diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-03-12 02:06:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-12 02:06:06 +0100 |
commit | 6ef282de3a3245acbfecd08ae48dba85ff91d031 (patch) | |
tree | 0236c9678eea8f9c78ed7c09f3d86eaf3d7c691c /g4f/requests/aiohttp.py | |
parent | Update .gitignore (diff) | |
download | gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar.gz gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar.bz2 gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar.lz gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar.xz gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.tar.zst gpt4free-6ef282de3a3245acbfecd08ae48dba85ff91d031.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/requests/aiohttp.py | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/g4f/requests/aiohttp.py b/g4f/requests/aiohttp.py index d9bd6541..6979b20a 100644 --- a/g4f/requests/aiohttp.py +++ b/g4f/requests/aiohttp.py @@ -1,16 +1,20 @@ from __future__ import annotations -from aiohttp import ClientSession, ClientResponse, ClientTimeout -from typing import AsyncGenerator, Any +from aiohttp import ClientSession, ClientResponse, ClientTimeout, BaseConnector +from typing import AsyncIterator, Any, Optional -from ..providers.helper import get_connector from .defaults import DEFAULT_HEADERS +from ..errors import MissingRequirementsError class StreamResponse(ClientResponse): - async def iter_lines(self) -> AsyncGenerator[bytes, None]: + async def iter_lines(self) -> AsyncIterator[bytes]: async for line in self.content: yield line.rstrip(b"\r\n") + async def iter_content(self) -> AsyncIterator[bytes]: + async for chunk in self.content.iter_any(): + yield chunk + async def json(self) -> Any: return await super().json(content_type=None) @@ -27,4 +31,16 @@ class StreamSession(ClientSession): response_class=StreamResponse, connector=get_connector(kwargs.get("connector"), proxies.get("https")), headers=headers - )
\ No newline at end of file + ) + +def get_connector(connector: BaseConnector = None, proxy: str = None, rdns: bool = False) -> Optional[BaseConnector]: + if proxy and not connector: + try: + from aiohttp_socks import ProxyConnector + if proxy.startswith("socks5h://"): + proxy = proxy.replace("socks5h://", "socks5://") + rdns = True + connector = ProxyConnector.from_url(proxy, rdns=rdns) + except ImportError: + raise MissingRequirementsError('Install "aiohttp_socks" package for proxy support') + return connector
\ No newline at end of file |