Skip to content

Commit

Permalink
fix: Twilio API calls random issues with asyncio
Browse files Browse the repository at this point in the history
Async implementation seems not stable with guvicorn.
  • Loading branch information
clemlesne committed Apr 24, 2024
1 parent 6967721 commit aaab29f
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions persistence/twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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:
"""
Expand All @@ -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:
Expand All @@ -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),
Expand Down

0 comments on commit aaab29f

Please sign in to comment.