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

refacto: Standardize enum class names

Clémence Lesné9868ec68dc0a
diff --git a/function_app.py b/function_app.py
index 0a0453d..3e94c85 100644
--- a/function_app.py
+++ b/function_app.py
@@ -41,7 +41,7 @@ from helpers.http import azure_transport
from helpers.call_utils import ContextEnum as CallContextEnum
from htmlmin.minify import html_minify
from http import HTTPStatus
-from models.readiness import ReadinessModel, ReadinessCheckModel, ReadinessStatus
+from models.readiness import ReadinessModel, ReadinessCheckModel, ReadinessEnum
from opentelemetry.semconv.trace import SpanAttributes
from os import getenv
from pydantic import TypeAdapter, ValidationError
@@ -101,11 +101,11 @@ async def health_readiness_get(req: func.HttpRequest) -> func.HttpResponse:
_cache.areadiness(), _db.areadiness(), _search.areadiness(), _sms.areadiness()
)
readiness = ReadinessModel(
- status=ReadinessStatus.OK,
+ status=ReadinessEnum.OK,
checks=[
ReadinessCheckModel(id="cache", status=cache_check),
ReadinessCheckModel(id="index", status=db_check),
- ReadinessCheckModel(id="startup", status=ReadinessStatus.OK),
+ ReadinessCheckModel(id="startup", status=ReadinessEnum.OK),
ReadinessCheckModel(id="store", status=search_check),
ReadinessCheckModel(id="stream", status=sms_check),
],
@@ -113,8 +113,8 @@ async def health_readiness_get(req: func.HttpRequest) -> func.HttpResponse:
# If one of the checks fails, the whole readiness fails
status_code = HTTPStatus.OK
for check in readiness.checks:
- if check.status != ReadinessStatus.OK:
- readiness.status = ReadinessStatus.FAIL
+ if check.status != ReadinessEnum.OK:
+ readiness.status = ReadinessEnum.FAIL
status_code = HTTPStatus.SERVICE_UNAVAILABLE
break
return func.HttpResponse(
diff --git a/persistence/ai_search.py b/persistence/ai_search.py
index d774523..af52a3a 100644
--- a/persistence/ai_search.py
+++ b/persistence/ai_search.py
@@ -17,7 +17,7 @@ from azure.search.documents.models import (
from helpers.http import azure_transport
from helpers.config_models.ai_search import AiSearchModel
from helpers.logging import logger
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
from models.training import TrainingModel
from persistence.icache import ICache
from persistence.isearch import ISearch
@@ -44,19 +44,19 @@ class AiSearchSearch(ISearch):
)
self._config = config
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
"""
Check the readiness of the AI Search service.
"""
try:
async with await self._use_client() as client:
await client.get_document_count()
- return ReadinessStatus.OK
+ return ReadinessEnum.OK
except HttpResponseError as e:
logger.error(f"Error requesting AI Search, {e}")
except ServiceRequestError as e:
logger.error(f"Error connecting to AI Search, {e}")
- return ReadinessStatus.FAIL
+ return ReadinessEnum.FAIL
@retry(
reraise=True,
diff --git a/persistence/communication_services.py b/persistence/communication_services.py
index f864d72..bb67c7c 100644
--- a/persistence/communication_services.py
+++ b/persistence/communication_services.py
@@ -5,7 +5,7 @@ from helpers.http import azure_transport
from helpers.config_models.communication_services import CommunicationServicesModel
from helpers.logging import logger
from helpers.pydantic_types.phone_numbers import PhoneNumber
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
from persistence.isms import ISms
from typing import Optional
@@ -18,12 +18,12 @@ class CommunicationServicesSms(ISms):
logger.info(f"Using Communication Services from number {config.phone_number}")
self._config = config
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
"""
Check the readiness of the Communication Services SMS service.
"""
# TODO: How to check the readiness of the SMS service? We could send a SMS for each test, but that would be damm expensive.
- return ReadinessStatus.OK
+ return ReadinessEnum.OK
async def asend(self, content: str, phone_number: PhoneNumber) -> bool:
logger.info(f"Sending SMS to {phone_number}")
diff --git a/persistence/cosmos_db.py b/persistence/cosmos_db.py
index 47b8185..d772950 100644
--- a/persistence/cosmos_db.py
+++ b/persistence/cosmos_db.py
@@ -7,7 +7,7 @@ from helpers.config_models.database import CosmosDbModel
from helpers.http import azure_transport
from helpers.logging import logger
from models.call import CallStateModel
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
from persistence.icache import ICache
from persistence.istore import IStore
from pydantic import ValidationError
@@ -25,7 +25,7 @@ class CosmosDbStore(IStore):
logger.info(f"Using Cosmos DB {config.database}/{config.container}")
self._config = config
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
"""
Check the readiness of the Cosmos DB service.
@@ -43,7 +43,7 @@ class CosmosDbStore(IStore):
try:
# Test the item does not exist
if await self._item_exists(test_id, test_partition):
- return ReadinessStatus.FAIL
+ return ReadinessEnum.FAIL
async with self._use_client() as db:
# Create a new item
await db.upsert_item(body=test_dict)
@@ -58,13 +58,13 @@ class CosmosDbStore(IStore):
await db.delete_item(item=test_id, partition_key=test_partition)
# Test the item does not exist
if await self._item_exists(test_id, test_partition):
- return ReadinessStatus.FAIL
- return ReadinessStatus.OK
+ return ReadinessEnum.FAIL
+ return ReadinessEnum.OK
except AssertionError:
logger.error("Readiness test failed", exc_info=True)
except CosmosHttpResponseError as e:
logger.error(f"Error requesting CosmosDB, {e}")
- return ReadinessStatus.FAIL
+ return ReadinessEnum.FAIL
async def _item_exists(self, test_id: str, partition_key: str) -> bool:
exist = False
diff --git a/persistence/icache.py b/persistence/icache.py
index 97872f2..e4d1c85 100644
--- a/persistence/icache.py
+++ b/persistence/icache.py
@@ -1,12 +1,12 @@
from abc import ABC, abstractmethod
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
from typing import Optional, Union
class ICache(ABC):
@abstractmethod
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
pass
@abstractmethod
diff --git a/persistence/isearch.py b/persistence/isearch.py
index 4745b14..097b12a 100644
--- a/persistence/isearch.py
+++ b/persistence/isearch.py
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
from models.training import TrainingModel
from persistence.icache import ICache
from typing import Optional
@@ -12,7 +12,7 @@ class ISearch(ABC):
self._cache = cache
@abstractmethod
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
pass
@abstractmethod
diff --git a/persistence/isms.py b/persistence/isms.py
index 1cb1310..25fe769 100644
--- a/persistence/isms.py
+++ b/persistence/isms.py
@@ -1,12 +1,12 @@
from abc import ABC, abstractmethod
from helpers.pydantic_types.phone_numbers import PhoneNumber
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
class ISms(ABC):
@abstractmethod
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
pass
@abstractmethod
diff --git a/persistence/istore.py b/persistence/istore.py
index 090f70b..8ae2e19 100644
--- a/persistence/istore.py
+++ b/persistence/istore.py
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod
from models.call import CallStateModel
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
from persistence.icache import ICache
from typing import Optional
from uuid import UUID
@@ -13,7 +13,7 @@ class IStore(ABC):
self._cache = cache
@abstractmethod
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
pass
@abstractmethod
diff --git a/persistence/memory.py b/persistence/memory.py
index 8a37af2..75e5b39 100644
--- a/persistence/memory.py
+++ b/persistence/memory.py
@@ -1,7 +1,7 @@
from collections import OrderedDict
from helpers.config_models.cache import MemoryModel
from helpers.logging import logger
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
from persistence.icache import ICache
from typing import Optional, Union
import hashlib
@@ -25,11 +25,11 @@ class MemoryCache(ICache):
)
self._config = config
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
"""
Check the readiness of the memory cache.
"""
- return ReadinessStatus.OK # Always ready, it's memory :)
+ return ReadinessEnum.OK # Always ready, it's memory :)
async def aget(self, key: str) -> Optional[bytes]:
"""
diff --git a/persistence/redis.py b/persistence/redis.py
index ccea446..ec655fe 100644
--- a/persistence/redis.py
+++ b/persistence/redis.py
@@ -1,6 +1,6 @@
from helpers.config_models.cache import RedisModel
from helpers.logging import logger
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
from opentelemetry.instrumentation.redis import RedisInstrumentor
from persistence.icache import ICache
from redis.asyncio import Redis
@@ -43,7 +43,7 @@ class RedisCache(ICache):
password=config.password.get_secret_value(),
) # Redis manage by itself a low level connection pool with asyncio, but be warning to not use a generator while consuming the connection, it will close it
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
"""
Check the readiness of the Redis cache.
@@ -62,12 +62,12 @@ class RedisCache(ICache):
await self._client.delete(test_name)
# Test the item does not exist
assert await self._client.get(test_name) is None
- return ReadinessStatus.OK
+ return ReadinessEnum.OK
except AssertionError as e:
logger.error(f"Readiness test failed, {e}")
except RedisError as e:
logger.error(f"Error requesting Redis, {e}")
- return ReadinessStatus.FAIL
+ return ReadinessEnum.FAIL
async def aget(self, key: str) -> Optional[bytes]:
"""
diff --git a/persistence/sqlite.py b/persistence/sqlite.py
index a2fbda4..53b97c1 100644
--- a/persistence/sqlite.py
+++ b/persistence/sqlite.py
@@ -4,7 +4,7 @@ from helpers.config import CONFIG
from helpers.config_models.database import SqliteModel
from helpers.logging import logger
from models.call import CallStateModel
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
from opentelemetry.instrumentation.sqlite3 import SQLite3Instrumentor
from persistence.icache import ICache
from persistence.istore import IStore
@@ -39,7 +39,7 @@ class SqliteStore(IStore):
os.makedirs(name=db_folder, exist_ok=True)
self._first_run_done = True
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
"""
Check the readiness of the SQLite database.
@@ -48,10 +48,10 @@ class SqliteStore(IStore):
try:
async with self._use_db() as db:
await db.execute("SELECT 1")
- return ReadinessStatus.OK
+ return ReadinessEnum.OK
except Exception as e:
logger.error(f"Error requesting SQLite, {e}")
- return ReadinessStatus.FAIL
+ return ReadinessEnum.FAIL
async def call_aget(self, call_id: UUID) -> Optional[CallStateModel]:
logger.debug(f"Loading call {call_id}")
diff --git a/persistence/twilio.py b/persistence/twilio.py
index 96a226e..1dc1be1 100644
--- a/persistence/twilio.py
+++ b/persistence/twilio.py
@@ -2,7 +2,7 @@ from helpers.config_models.sms import TwilioModel
from helpers.http import twilio_http
from helpers.logging import logger
from helpers.pydantic_types.phone_numbers import PhoneNumber
-from models.readiness import ReadinessStatus
+from models.readiness import ReadinessEnum
from persistence.isms import ISms
from twilio.base.exceptions import TwilioRestException
from twilio.rest import Client
@@ -17,7 +17,7 @@ class TwilioSms(ISms):
logger.info(f"Using Twilio from number {config.phone_number}")
self._config = config
- async def areadiness(self) -> ReadinessStatus:
+ async def areadiness(self) -> ReadinessEnum:
"""
Check the readiness of the Twilio SMS service.
@@ -29,10 +29,10 @@ class TwilioSms(ISms):
account = await client.api.accounts(account_sid).fetch_async()
balance = account.balance.fetch()
assert balance.balance and float(balance.balance) > 0
- return ReadinessStatus.OK
+ return ReadinessEnum.OK
except AssertionError:
logger.error("Readiness test failed", exc_info=True)
- return ReadinessStatus.FAIL
+ return ReadinessEnum.FAIL
async def asend(self, content: str, phone_number: PhoneNumber) -> bool:
logger.info(f"Sending SMS to {phone_number}")