From aaab29f2ca1ea0bdf7625de9be95f3f215661a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mence=20Lesn=C3=A9?= Date: Wed, 24 Apr 2024 16:43:30 +0200 Subject: [PATCH] fix: Twilio API calls random issues with asyncio Async implementation seems not stable with guvicorn. --- persistence/twilio.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/persistence/twilio.py b/persistence/twilio.py index 100932f4..aa79132b 100644 --- a/persistence/twilio.py +++ b/persistence/twilio.py @@ -4,7 +4,6 @@ from models.readiness import ReadinessStatus from persistence.isms import ISms from twilio.base.exceptions import TwilioRestException -from twilio.http.async_http_client import AsyncTwilioHttpClient from twilio.rest import Client @@ -19,10 +18,9 @@ def __init__(self, config: TwilioModel): _logger.info(f"Using Twilio from number {config.phone_number}") self._config = config self._client = Client( - http_client=AsyncTwilioHttpClient(), password=config.auth_token.get_secret_value(), username=config.account_sid, - ) + ) # TODO: Use async client, but get multiple "attached to a different loop" errors with AsyncTwilioHttpClient async def areadiness(self) -> ReadinessStatus: """ @@ -32,8 +30,8 @@ async def areadiness(self) -> ReadinessStatus: """ account_sid = self._config.account_sid try: - account = await self._client.api.accounts(account_sid).fetch_async() - balance = await account.balance.fetch_async() + account = self._client.api.accounts(account_sid).fetch() + balance = account.balance.fetch() assert balance.balance and float(balance.balance) > 0 return ReadinessStatus.OK except AssertionError: @@ -45,7 +43,7 @@ async def asend(self, content: str, phone_number: PhoneNumber) -> bool: success = False _logger.info(f"SMS content: {content}") try: - res = await self._client.messages.create_async( + res = self._client.messages.create( body=content, from_=str(self._config.phone_number), to=str(phone_number),