mirror of https://github.com/xtekky/gpt4free
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
8 months ago
|
from __future__ import annotations
|
||
|
|
||
|
from aiohttp import ClientSession, ClientResponse, ClientTimeout
|
||
|
from typing import AsyncGenerator, Any
|
||
|
|
||
8 months ago
|
from ..providers.helper import get_connector
|
||
8 months ago
|
from .defaults import DEFAULT_HEADERS
|
||
8 months ago
|
|
||
|
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 = {
|
||
8 months ago
|
**DEFAULT_HEADERS,
|
||
8 months ago
|
**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
|
||
|
)
|