Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-python): reject a final 1xx response as an error

Yudhi Armyndharise31b89340525
diff --git a/sdk/python/openwa/_http.py b/sdk/python/openwa/_http.py
index 71b1e666b..c2d4efc29 100644
--- a/sdk/python/openwa/_http.py
+++ b/sdk/python/openwa/_http.py
@@ -251,8 +251,9 @@ class HttpExecutor:
raise OpenWATimeoutError(self._timeout or 0) from e
# Treat any non-2xx as an error, including 3xx: redirects are deliberately not followed
# (so the API key is never re-sent to the target), which makes an unfollowed 3xx unusable
- # rather than a success. Matches the JS transport's `!res.ok`.
- if res.status_code >= 300:
+ # rather than a success. A final 1xx from a custom transport is no more usable. Matches the JS
+ # transport's `!res.ok`.
+ if not 200 <= res.status_code < 300:
context = f"{method} {path}"
raise OpenWAApiError.from_response(
res.status_code, res.text, context, headers=res.headers, reason=res.reason_phrase
diff --git a/sdk/python/tests/test_sdk.py b/sdk/python/tests/test_sdk.py
index 83b4a023d..c61393020 100644
--- a/sdk/python/tests/test_sdk.py
+++ b/sdk/python/tests/test_sdk.py
@@ -152,6 +152,24 @@ class TestClientCore:
client.sessions.list()
assert len(calls) == 1 # the redirect target was never requested
+ @pytest.mark.parametrize("binary", [False, True])
+ @pytest.mark.parametrize(
+ ("status", "ok"), [(100, False), (101, False), (199, False), (200, True), (204, True), (299, True), (300, False)]
+ )
+ def test_only_a_2xx_response_is_a_success(self, binary: bool, status: int, ok: bool) -> None:
+ # A custom transport can hand back a final informational status; it is no more usable than a 3xx.
+ def handler(_: httpx.Request) -> httpx.Response:
+ return httpx.Response(status, content=b"" if status == 204 else b'{"status": "ok"}')
+
+ with OpenWAClient(base_url="http://localhost", api_key="k", transport=httpx.MockTransport(handler)) as client:
+ call = (lambda: client.messages.media("s", "c", "m")) if binary else client.health.check
+ if ok:
+ call()
+ else:
+ with pytest.raises(OpenWAApiError) as caught:
+ call()
+ assert caught.value.status == status
+
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())