Skip to content

Commit 5810ab9

Browse files
committed
refactor(whatsapp): Update expected_status handling in API requests
- Modify expected_status parameter in request methods to accept both int and Sequence[int] - Update all relevant API request calls to use a list for expected_status values - Enhance resilience in handling HTTP responses by allowing multiple acceptable status codes
1 parent 44e5f90 commit 5810ab9

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

agentle/agents/whatsapp/providers/evolution/evolution_api_provider.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import logging
99
import re
1010
import time
11-
from collections.abc import Mapping, MutableMapping
11+
from collections.abc import Mapping, MutableMapping, Sequence
1212
from datetime import datetime
1313
from typing import Any, override
1414
from urllib.parse import urljoin
@@ -307,7 +307,7 @@ async def _make_request_with_resilience(
307307
method: str,
308308
url: str,
309309
data: Mapping[str, Any] | None = None,
310-
expected_status: int = 200,
310+
expected_status: int | Sequence[int] = 200,
311311
) -> Mapping[str, Any]:
312312
"""
313313
Make HTTP request with comprehensive resilience mechanisms.
@@ -416,7 +416,7 @@ async def _make_request(
416416
method: str,
417417
url: str,
418418
data: Mapping[str, Any] | None = None,
419-
expected_status: int = 200,
419+
expected_status: int | Sequence[int] = 200,
420420
) -> Mapping[str, Any]:
421421
"""
422422
Make HTTP request with proper error handling and metrics.
@@ -531,7 +531,7 @@ def _sanitize_request_data(
531531
async def _handle_response(
532532
self,
533533
response: aiohttp.ClientResponse,
534-
expected_status: int,
534+
expected_status: int | Sequence[int],
535535
request_url: str,
536536
request_data: Mapping[str, Any] | None,
537537
start_time: float,
@@ -571,7 +571,10 @@ async def _handle_response(
571571
},
572572
)
573573

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:
575578
try:
576579
response_data = await response.json()
577580
logger.debug(f"Response data received: {response_data}")
@@ -771,7 +774,7 @@ async def send_text_message(
771774

772775
url = self._build_url(f"sendText/{self.config.instance_name}")
773776
response_data = await self._make_request_with_resilience(
774-
"POST", url, payload, expected_status=201
777+
"POST", url, payload, expected_status=[200, 201]
775778
)
776779

777780
message_id = response_data["key"]["id"]
@@ -885,7 +888,7 @@ async def send_media_message(
885888

886889
url = self._build_url(f"{endpoint}/{self.config.instance_name}")
887890
response_data = await self._make_request_with_resilience(
888-
"POST", url, payload, expected_status=201
891+
"POST", url, payload, expected_status=[200, 201]
889892
)
890893

891894
message_id = response_data["key"]["id"]
@@ -982,7 +985,7 @@ async def send_audio_message(
982985

983986
url = self._build_url(f"sendWhatsAppAudio/{self.config.instance_name}")
984987
response_data = await self._make_request_with_resilience(
985-
"POST", url, payload, expected_status=201
988+
"POST", url, payload, expected_status=[200, 201]
986989
)
987990

988991
message_id = response_data["key"]["id"]
@@ -1060,7 +1063,7 @@ async def send_typing_indicator(self, to: str, duration: int = 3) -> None:
10601063
use_message_prefix=False,
10611064
)
10621065
await self._make_request_with_resilience(
1063-
"POST", url, payload, expected_status=201
1066+
"POST", url, payload, expected_status=[200, 201]
10641067
)
10651068

10661069
logger.debug(
@@ -1122,7 +1125,7 @@ async def send_recording_indicator(self, to: str, duration: int = 3) -> None:
11221125
use_message_prefix=False,
11231126
)
11241127
await self._make_request_with_resilience(
1125-
"POST", url, payload, expected_status=201
1128+
"POST", url, payload, expected_status=[200, 201]
11261129
)
11271130

11281131
logger.debug(
@@ -1173,7 +1176,7 @@ async def mark_message_as_read(self, message_id: str) -> None:
11731176
use_message_prefix=False,
11741177
)
11751178
await self._make_request_with_resilience(
1176-
"POST", url, payload, expected_status=201
1179+
"POST", url, payload, expected_status=[200, 201]
11771180
)
11781181

11791182
logger.debug(
@@ -1423,7 +1426,7 @@ async def download_media(self, media_id: str) -> DownloadedMedia:
14231426
payload = {"message": {"key": {"id": media_id}}}
14241427

14251428
response_data = await self._make_request_with_resilience(
1426-
"POST", url, payload, expected_status=201
1429+
"POST", url, payload, expected_status=[200, 201]
14271430
)
14281431

14291432
if "base64" not in response_data:

0 commit comments

Comments
 (0)