gpt4free/g4f/requests.py

53 lines
1.7 KiB
Python
Raw Normal View History

from __future__ import annotations
2023-10-07 08:17:43 +00:00
import json
from contextlib import asynccontextmanager
from functools import partialmethod
from typing import AsyncGenerator
from curl_cffi.requests import AsyncSession, Response
class StreamResponse:
def __init__(self, inner: Response) -> None:
2023-10-07 08:17:43 +00:00
self.inner: Response = inner
self.request = inner.request
2023-10-07 08:17:43 +00:00
self.status_code: int = inner.status_code
self.reason: str = inner.reason
self.ok: bool = inner.ok
self.headers = inner.headers
self.cookies = inner.cookies
async def text(self) -> str:
return await self.inner.atext()
2023-10-07 08:17:43 +00:00
def raise_for_status(self) -> None:
self.inner.raise_for_status()
2023-10-07 08:17:43 +00:00
async def json(self, **kwargs) -> dict:
return json.loads(await self.inner.acontent(), **kwargs)
async def iter_lines(self) -> AsyncGenerator[bytes, None]:
async for line in self.inner.aiter_lines():
yield line
async def iter_content(self) -> AsyncGenerator[bytes, None]:
async for chunk in self.inner.aiter_content():
yield chunk
class StreamSession(AsyncSession):
@asynccontextmanager
async def request(
2023-10-07 17:00:45 +00:00
self, method: str, url: str, **kwargs
) -> AsyncGenerator[StreamResponse]:
response = await super().request(method, url, stream=True, **kwargs)
try:
yield StreamResponse(response)
finally:
await response.aclose()
2023-10-07 08:17:43 +00:00
head = partialmethod(request, "HEAD")
get = partialmethod(request, "GET")
post = partialmethod(request, "POST")
put = partialmethod(request, "PUT")
patch = partialmethod(request, "PATCH")
delete = partialmethod(request, "DELETE")