Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-python): ignore a Retry-After too long to parse

Yudhi Armyndharis4f65344be82d
diff --git a/sdk/python/openwa/errors.py b/sdk/python/openwa/errors.py
index 2bbcca7d4..9131b6cbe 100644
--- a/sdk/python/openwa/errors.py
+++ b/sdk/python/openwa/errors.py
@@ -187,7 +187,11 @@ def _retry_after_seconds(from_body: Any, header: str | None) -> int | None:
if not value:
return None
if value.isascii() and value.isdigit():
- return int(value)
+ try:
+ return int(value)
+ except ValueError:
+ # Past the interpreter's integer string limit: unparseable, like a malformed date below.
+ return None
try:
at = parsedate_to_datetime(value)
except (TypeError, ValueError, OverflowError):
diff --git a/sdk/python/tests/test_sdk.py b/sdk/python/tests/test_sdk.py
index c61393020..733c275b1 100644
--- a/sdk/python/tests/test_sdk.py
+++ b/sdk/python/tests/test_sdk.py
@@ -170,6 +170,23 @@ class TestClientCore:
call()
assert caught.value.status == status
+ @pytest.mark.parametrize("binary", [False, True])
+ def test_a_retry_after_too_long_to_parse_keeps_the_api_error(self, binary: bool) -> None:
+ import sys
+
+ limit = getattr(sys, "get_int_max_str_digits", lambda: 0)()
+ if limit == 0:
+ pytest.skip("this interpreter has no limit on integer string conversion")
+ headers = {"Retry-After": "9" * (limit + 1)}
+ transport = httpx.MockTransport(
+ lambda _: httpx.Response(429, json={"statusCode": 429, "message": "slow down"}, headers=headers)
+ )
+ with OpenWAClient(base_url="http://localhost", api_key="k", transport=transport) as client:
+ with pytest.raises(OpenWARateLimitError) as caught:
+ client.messages.media("s", "c", "m") if binary else client.health.check()
+ assert caught.value.status == 429
+ assert caught.value.retry_after_seconds is None
+
def test_strips_trailing_slash(self):
backend = MockBackend().on("GET", "/api/sessions", body=[])
client = OpenWAClient(base_url="http://localhost:2785/", api_key="k", transport=backend.as_transport())