diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-01-27 16:09:21 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-27 16:09:21 +0100 |
commit | 0d83bdeef91001c7c00d69f79fccad5a78b4b94b (patch) | |
tree | 362ebeb245cc4e123914635b03f890a314a7a40d /g4f/requests_aiohttp.py | |
parent | Merge pull request #1512 from KennyPhan123/patch-1 (diff) | |
parent | Fix PerplexityLabs Provider, Improve bypass_cloudflare helper (diff) | |
download | gpt4free-0d83bdeef91001c7c00d69f79fccad5a78b4b94b.tar gpt4free-0d83bdeef91001c7c00d69f79fccad5a78b4b94b.tar.gz gpt4free-0d83bdeef91001c7c00d69f79fccad5a78b4b94b.tar.bz2 gpt4free-0d83bdeef91001c7c00d69f79fccad5a78b4b94b.tar.lz gpt4free-0d83bdeef91001c7c00d69f79fccad5a78b4b94b.tar.xz gpt4free-0d83bdeef91001c7c00d69f79fccad5a78b4b94b.tar.zst gpt4free-0d83bdeef91001c7c00d69f79fccad5a78b4b94b.zip |
Diffstat (limited to 'g4f/requests_aiohttp.py')
-rw-r--r-- | g4f/requests_aiohttp.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/g4f/requests_aiohttp.py b/g4f/requests_aiohttp.py new file mode 100644 index 00000000..aa097312 --- /dev/null +++ b/g4f/requests_aiohttp.py @@ -0,0 +1,39 @@ +from __future__ import annotations + +from aiohttp import ClientSession, ClientResponse, ClientTimeout +from typing import AsyncGenerator, Any + +from .Provider.helper import get_connector + +class StreamResponse(ClientResponse): + async def iter_lines(self) -> AsyncGenerator[bytes, None]: + async for line in self.content: + yield line.rstrip(b"\r\n") + + async def json(self) -> Any: + return await super().json(content_type=None) + +class StreamSession(ClientSession): + def __init__(self, headers: dict = {}, timeout: int = None, proxies: dict = {}, impersonate = None, **kwargs): + if impersonate: + headers = { + 'Accept-Encoding': 'gzip, deflate, br', + 'Accept-Language': 'en-US', + 'Connection': 'keep-alive', + 'Sec-Fetch-Dest': 'empty', + 'Sec-Fetch-Mode': 'cors', + 'Sec-Fetch-Site': 'same-site', + "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36', + 'Accept': '*/*', + 'sec-ch-ua': '"Google Chrome";v="107", "Chromium";v="107", "Not?A_Brand";v="24"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"Windows"', + **headers + } + super().__init__( + **kwargs, + timeout=ClientTimeout(timeout) if timeout else None, + response_class=StreamResponse, + connector=get_connector(kwargs.get("connector"), proxies.get("https")), + headers=headers + )
\ No newline at end of file |