summaryrefslogtreecommitdiffstats
path: root/g4f/requests.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-01-13 15:52:52 +0100
committerGitHub <noreply@github.com>2024-01-13 15:52:52 +0100
commitd632190f1d9ff04759a427d1938c0813e918e5e8 (patch)
tree44e3827128489f13fd6fbe7d18171d81176abc86 /g4f/requests.py
parentCleanup unused dependencies (diff)
parentMerge pull request #1462 from hlohaus/img (diff)
downloadgpt4free-d632190f1d9ff04759a427d1938c0813e918e5e8.tar
gpt4free-d632190f1d9ff04759a427d1938c0813e918e5e8.tar.gz
gpt4free-d632190f1d9ff04759a427d1938c0813e918e5e8.tar.bz2
gpt4free-d632190f1d9ff04759a427d1938c0813e918e5e8.tar.lz
gpt4free-d632190f1d9ff04759a427d1938c0813e918e5e8.tar.xz
gpt4free-d632190f1d9ff04759a427d1938c0813e918e5e8.tar.zst
gpt4free-d632190f1d9ff04759a427d1938c0813e918e5e8.zip
Diffstat (limited to 'g4f/requests.py')
-rw-r--r--g4f/requests.py31
1 files changed, 17 insertions, 14 deletions
diff --git a/g4f/requests.py b/g4f/requests.py
index 467ea371..1a13dec9 100644
--- a/g4f/requests.py
+++ b/g4f/requests.py
@@ -11,12 +11,6 @@ from .webdriver import WebDriver, WebDriverSession, bypass_cloudflare, get_drive
class StreamResponse:
def __init__(self, inner: Response) -> None:
self.inner: Response = 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
async def text(self) -> str:
return await self.inner.atext()
@@ -34,17 +28,26 @@ class StreamResponse:
async def iter_content(self) -> AsyncGenerator[bytes, None]:
async for chunk in self.inner.aiter_content():
yield chunk
+
+ async def __aenter__(self):
+ 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):
+ await self.inner.aclose()
class StreamSession(AsyncSession):
- @asynccontextmanager
- async def request(
+ def request(
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()
+ ) -> StreamResponse:
+ return StreamResponse(super().request(method, url, stream=True, **kwargs))
head = partialmethod(request, "HEAD")
get = partialmethod(request, "GET")