|
8 | 8 | import logging |
9 | 9 | import re |
10 | 10 | import time |
11 | | -from collections.abc import Mapping, MutableMapping |
| 11 | +from collections.abc import Mapping, MutableMapping, Sequence |
12 | 12 | from datetime import datetime |
13 | 13 | from typing import Any, override |
14 | 14 | from urllib.parse import urljoin |
@@ -307,7 +307,7 @@ async def _make_request_with_resilience( |
307 | 307 | method: str, |
308 | 308 | url: str, |
309 | 309 | data: Mapping[str, Any] | None = None, |
310 | | - expected_status: int = 200, |
| 310 | + expected_status: int | Sequence[int] = 200, |
311 | 311 | ) -> Mapping[str, Any]: |
312 | 312 | """ |
313 | 313 | Make HTTP request with comprehensive resilience mechanisms. |
@@ -416,7 +416,7 @@ async def _make_request( |
416 | 416 | method: str, |
417 | 417 | url: str, |
418 | 418 | data: Mapping[str, Any] | None = None, |
419 | | - expected_status: int = 200, |
| 419 | + expected_status: int | Sequence[int] = 200, |
420 | 420 | ) -> Mapping[str, Any]: |
421 | 421 | """ |
422 | 422 | Make HTTP request with proper error handling and metrics. |
@@ -531,7 +531,7 @@ def _sanitize_request_data( |
531 | 531 | async def _handle_response( |
532 | 532 | self, |
533 | 533 | response: aiohttp.ClientResponse, |
534 | | - expected_status: int, |
| 534 | + expected_status: int | Sequence[int], |
535 | 535 | request_url: str, |
536 | 536 | request_data: Mapping[str, Any] | None, |
537 | 537 | start_time: float, |
@@ -571,7 +571,10 @@ async def _handle_response( |
571 | 571 | }, |
572 | 572 | ) |
573 | 573 |
|
574 | | - if response.status == expected_status: |
| 574 | + if isinstance(expected_status, int): |
| 575 | + expected_status = [expected_status] |
| 576 | + |
| 577 | + if response.status not in expected_status: |
575 | 578 | try: |
576 | 579 | response_data = await response.json() |
577 | 580 | logger.debug(f"Response data received: {response_data}") |
@@ -771,7 +774,7 @@ async def send_text_message( |
771 | 774 |
|
772 | 775 | url = self._build_url(f"sendText/{self.config.instance_name}") |
773 | 776 | response_data = await self._make_request_with_resilience( |
774 | | - "POST", url, payload, expected_status=201 |
| 777 | + "POST", url, payload, expected_status=[200, 201] |
775 | 778 | ) |
776 | 779 |
|
777 | 780 | message_id = response_data["key"]["id"] |
@@ -885,7 +888,7 @@ async def send_media_message( |
885 | 888 |
|
886 | 889 | url = self._build_url(f"{endpoint}/{self.config.instance_name}") |
887 | 890 | response_data = await self._make_request_with_resilience( |
888 | | - "POST", url, payload, expected_status=201 |
| 891 | + "POST", url, payload, expected_status=[200, 201] |
889 | 892 | ) |
890 | 893 |
|
891 | 894 | message_id = response_data["key"]["id"] |
@@ -982,7 +985,7 @@ async def send_audio_message( |
982 | 985 |
|
983 | 986 | url = self._build_url(f"sendWhatsAppAudio/{self.config.instance_name}") |
984 | 987 | response_data = await self._make_request_with_resilience( |
985 | | - "POST", url, payload, expected_status=201 |
| 988 | + "POST", url, payload, expected_status=[200, 201] |
986 | 989 | ) |
987 | 990 |
|
988 | 991 | message_id = response_data["key"]["id"] |
@@ -1060,7 +1063,7 @@ async def send_typing_indicator(self, to: str, duration: int = 3) -> None: |
1060 | 1063 | use_message_prefix=False, |
1061 | 1064 | ) |
1062 | 1065 | await self._make_request_with_resilience( |
1063 | | - "POST", url, payload, expected_status=201 |
| 1066 | + "POST", url, payload, expected_status=[200, 201] |
1064 | 1067 | ) |
1065 | 1068 |
|
1066 | 1069 | logger.debug( |
@@ -1122,7 +1125,7 @@ async def send_recording_indicator(self, to: str, duration: int = 3) -> None: |
1122 | 1125 | use_message_prefix=False, |
1123 | 1126 | ) |
1124 | 1127 | await self._make_request_with_resilience( |
1125 | | - "POST", url, payload, expected_status=201 |
| 1128 | + "POST", url, payload, expected_status=[200, 201] |
1126 | 1129 | ) |
1127 | 1130 |
|
1128 | 1131 | logger.debug( |
@@ -1173,7 +1176,7 @@ async def mark_message_as_read(self, message_id: str) -> None: |
1173 | 1176 | use_message_prefix=False, |
1174 | 1177 | ) |
1175 | 1178 | await self._make_request_with_resilience( |
1176 | | - "POST", url, payload, expected_status=201 |
| 1179 | + "POST", url, payload, expected_status=[200, 201] |
1177 | 1180 | ) |
1178 | 1181 |
|
1179 | 1182 | logger.debug( |
@@ -1423,7 +1426,7 @@ async def download_media(self, media_id: str) -> DownloadedMedia: |
1423 | 1426 | payload = {"message": {"key": {"id": media_id}}} |
1424 | 1427 |
|
1425 | 1428 | response_data = await self._make_request_with_resilience( |
1426 | | - "POST", url, payload, expected_status=201 |
| 1429 | + "POST", url, payload, expected_status=[200, 201] |
1427 | 1430 | ) |
1428 | 1431 |
|
1429 | 1432 | if "base64" not in response_data: |
|
0 commit comments