summaryrefslogtreecommitdiffstats
path: root/g4f/requests/raise_for_status.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-03-16 18:22:26 +0100
committerGitHub <noreply@github.com>2024-03-16 18:22:26 +0100
commitfb2061da48525edab9cd993205bb5e30c386aa1a (patch)
tree1e740bd6955dfd27b9a4d773df07234ed9e5c75e /g4f/requests/raise_for_status.py
parentMerge pull request #1694 from ComRSMaster/main (diff)
parentAdd conversation support for Bing (diff)
downloadgpt4free-fb2061da48525edab9cd993205bb5e30c386aa1a.tar
gpt4free-fb2061da48525edab9cd993205bb5e30c386aa1a.tar.gz
gpt4free-fb2061da48525edab9cd993205bb5e30c386aa1a.tar.bz2
gpt4free-fb2061da48525edab9cd993205bb5e30c386aa1a.tar.lz
gpt4free-fb2061da48525edab9cd993205bb5e30c386aa1a.tar.xz
gpt4free-fb2061da48525edab9cd993205bb5e30c386aa1a.tar.zst
gpt4free-fb2061da48525edab9cd993205bb5e30c386aa1a.zip
Diffstat (limited to '')
-rw-r--r--g4f/requests/raise_for_status.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/g4f/requests/raise_for_status.py b/g4f/requests/raise_for_status.py
new file mode 100644
index 00000000..9e8e141c
--- /dev/null
+++ b/g4f/requests/raise_for_status.py
@@ -0,0 +1,34 @@
+from __future__ import annotations
+
+from typing import Union
+from aiohttp import ClientResponse
+from requests import Response as RequestsResponse
+
+from ..errors import ResponseStatusError, RateLimitError
+from . import Response, StreamResponse
+
+class CloudflareError(ResponseStatusError):
+ ...
+
+def is_cloudflare(text: str) -> bool:
+ return '<div id="cf-please-wait">' in text or "<title>Just a moment...</title>" in text
+
+async def raise_for_status_async(response: Union[StreamResponse, ClientResponse], message: str = None):
+ if response.status in (429, 402):
+ raise RateLimitError(f"Response {response.status}: Rate limit reached")
+ message = await response.text() if not response.ok and message is None else message
+ if response.status == 403 and is_cloudflare(message):
+ raise CloudflareError(f"Response {response.status}: Cloudflare detected")
+ elif not response.ok:
+ raise ResponseStatusError(f"Response {response.status}: {message}")
+
+def raise_for_status(response: Union[Response, StreamResponse, ClientResponse, RequestsResponse], message: str = None):
+ if hasattr(response, "status"):
+ return raise_for_status_async(response, message)
+
+ if response.status_code in (429, 402):
+ raise RateLimitError(f"Response {response.status_code}: Rate limit reached")
+ elif response.status_code == 403 and is_cloudflare(response.text):
+ raise CloudflareError(f"Response {response.status_code}: Cloudflare detected")
+ elif not response.ok:
+ raise ResponseStatusError(f"Response {response.status_code}: {response.text if message is None else message}") \ No newline at end of file