Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

fix: Async HTTP stream should be closed at each transaction

Clémence Lesné6ac3f1465a02
diff --git a/persistence/ai_search.py b/persistence/ai_search.py
index 8450023..60c026e 100644
--- a/persistence/ai_search.py
+++ b/persistence/ai_search.py
@@ -118,5 +118,7 @@ class AiSearchSearch(ISearch):
endpoint=self._config.endpoint,
index_name=self._config.index,
)
- yield db
- await db.close()
+ try:
+ yield db
+ finally:
+ await db.close()
diff --git a/persistence/cosmos.py b/persistence/cosmos.py
index e487aa5..75d3193 100644
--- a/persistence/cosmos.py
+++ b/persistence/cosmos.py
@@ -25,6 +25,7 @@ class CosmosStore(IStore):
async def call_aget(self, call_id: UUID) -> Optional[CallModel]:
_logger.debug(f"Loading call {call_id}")
+ res = None
try:
async with self._use_db() as db:
items = db.query_items(
@@ -33,13 +34,14 @@ class CosmosStore(IStore):
)
raw = await anext(items)
try:
- return CallModel(**raw)
+ res = CallModel(**raw)
except ValidationError as e:
_logger.warn(f"Error parsing call: {e.errors()}")
except StopAsyncIteration:
- return None
+ pass
except CosmosHttpResponseError as e:
_logger.error(f"Error accessing CosmosDB, {e}")
+ return res
async def call_aset(self, call: CallModel) -> bool:
data = jsonable_encoder(call.model_dump(), exclude_none=True)
@@ -55,6 +57,7 @@ class CosmosStore(IStore):
async def call_asearch_one(self, phone_number: str) -> Optional[CallModel]:
_logger.debug(f"Loading last call for {phone_number}")
+ res = None
try:
async with self._use_db() as db:
items = db.query_items(
@@ -78,13 +81,14 @@ class CosmosStore(IStore):
)
raw = await anext(items)
try:
- return CallModel(**raw)
+ res = CallModel(**raw)
except ValidationError as e:
_logger.warn(f"Error parsing call: {e.errors()}")
except StopAsyncIteration:
- return None
+ pass
except CosmosHttpResponseError as e:
_logger.error(f"Error accessing CosmosDB: {e}")
+ return res
async def call_asearch_all(self, phone_number: str) -> Optional[List[CallModel]]:
_logger.debug(f"Loading all calls for {phone_number}")
@@ -122,6 +126,8 @@ class CosmosStore(IStore):
# Authentication with API key
credential=self._config.access_key.get_secret_value(),
)
- database = client.get_database_client(self._config.database)
- yield database.get_container_client(self._config.container)
- await client.close()
+ try:
+ database = client.get_database_client(self._config.database)
+ yield database.get_container_client(self._config.container)
+ finally:
+ await client.close()
diff --git a/persistence/redis.py b/persistence/redis.py
index eef9ae3..ad334cd 100644
--- a/persistence/redis.py
+++ b/persistence/redis.py
@@ -28,9 +28,10 @@ class RedisCache(ICache):
Catch errors for a maximum of 3 times, then raise the error.
"""
+ res = None
async with self._use_db() as db:
res = await db.get(key)
- return res if res else None
+ return res
async def aset(self, key: str, value: Union[str, bytes, None]) -> None:
"""
@@ -59,5 +60,7 @@ class RedisCache(ICache):
# Authentication with password
password=self._config.password.get_secret_value(),
)
- yield client
- await client.aclose()
+ try:
+ yield client
+ finally:
+ await client.aclose()