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_curl_cffi.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_curl_cffi.py')
-rw-r--r-- | g4f/requests_curl_cffi.py | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/g4f/requests_curl_cffi.py b/g4f/requests_curl_cffi.py new file mode 100644 index 00000000..64e41d65 --- /dev/null +++ b/g4f/requests_curl_cffi.py @@ -0,0 +1,77 @@ +from __future__ import annotations + +from curl_cffi.requests import AsyncSession, Response +from typing import AsyncGenerator, Any +from functools import partialmethod +import json + +class StreamResponse: + """ + A wrapper class for handling asynchronous streaming responses. + + Attributes: + inner (Response): The original Response object. + """ + + def __init__(self, inner: Response) -> None: + """Initialize the StreamResponse with the provided Response object.""" + self.inner: Response = inner + + async def text(self) -> str: + """Asynchronously get the response text.""" + return await self.inner.atext() + + def raise_for_status(self) -> None: + """Raise an HTTPError if one occurred.""" + self.inner.raise_for_status() + + async def json(self, **kwargs) -> Any: + """Asynchronously parse the JSON response content.""" + return json.loads(await self.inner.acontent(), **kwargs) + + async def iter_lines(self) -> AsyncGenerator[bytes, None]: + """Asynchronously iterate over the lines of the response.""" + async for line in self.inner.aiter_lines(): + yield line + + async def iter_content(self) -> AsyncGenerator[bytes, None]: + """Asynchronously iterate over the response content.""" + async for chunk in self.inner.aiter_content(): + yield chunk + + async def __aenter__(self): + """Asynchronously enter the runtime context for the response object.""" + inner: Response = await self.inner + self.inner = inner + self.request = inner.request + 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 + return self + + async def __aexit__(self, *args): + """Asynchronously exit the runtime context for the response object.""" + await self.inner.aclose() + +class StreamSession(AsyncSession): + """ + An asynchronous session class for handling HTTP requests with streaming. + + Inherits from AsyncSession. + """ + + def request( + self, method: str, url: str, **kwargs + ) -> StreamResponse: + """Create and return a StreamResponse object for the given HTTP request.""" + return StreamResponse(super().request(method, url, stream=True, **kwargs)) + + # Defining HTTP methods as partial methods of the request method. + head = partialmethod(request, "HEAD") + get = partialmethod(request, "GET") + post = partialmethod(request, "POST") + put = partialmethod(request, "PUT") + patch = partialmethod(request, "PATCH") + delete = partialmethod(request, "DELETE") |