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

quality: Code quality

Clémence Lesné8e9ffe7ea68c
diff --git a/app/helpers/features.py b/app/helpers/features.py
index 7753223..86af770 100644
--- a/app/helpers/features.py
+++ b/app/helpers/features.py
@@ -9,10 +9,9 @@ from app.helpers.config_models.cache import MemoryModel
from app.helpers.http import azure_transport
from app.helpers.identity import credential
from app.helpers.logging import logger
-from app.persistence.icache import ICache
from app.persistence.memory import MemoryCache
-_cache: ICache = MemoryCache(MemoryModel(max_size=100))
+_cache = MemoryCache(MemoryModel())
T = TypeVar("T", bool, int, float, str)
@@ -104,7 +103,11 @@ async def _default(
"""
Get a setting from the App Configuration service with a default value.
"""
- return (await _get(key=key, type_res=type_res)) or default
+ res = await _get(key=key, type_res=type_res)
+ if res:
+ return res
+ logger.warning("Setting %s not found, using default: %s", key, default)
+ return default
async def _get(
@@ -118,7 +121,11 @@ async def _get(
cache_key = _cache_key(key)
cached = await _cache.get(cache_key)
if cached:
- return _parse(value=cached.decode(), type_res=type_res)
+ return _parse(
+ type_res=type_res,
+ value=cached.decode(),
+ )
+
# Try live
try:
async with await _use_client() as client:
@@ -126,17 +133,24 @@ async def _get(
# Return default if not found
if not setting:
return
+ res = setting.value
except ResourceNotFoundError:
- logger.warning("Setting %s not found", key)
return
+
+ logger.debug("Setting %s refreshed: %s", key, res)
+
# Update cache
await _cache.set(
key=cache_key,
ttl_sec=CONFIG.app_configuration.ttl_sec,
- value=setting.value,
+ value=res,
+ )
+
+ # Return the type
+ return _parse(
+ type_res=type_res,
+ value=res,
)
- # Return
- return _parse(value=setting.value, type_res=type_res)
@async_lru_cache()
diff --git a/app/persistence/icache.py b/app/persistence/icache.py
index d347864..6f681cf 100644
--- a/app/persistence/icache.py
+++ b/app/persistence/icache.py
@@ -17,7 +17,12 @@ class ICache(ABC):
@abstractmethod
@tracer.start_as_current_span("cache_set")
- async def set(self, key: str, value: str | bytes | None, ttl_sec: int) -> bool:
+ async def set(
+ self,
+ key: str,
+ ttl_sec: int,
+ value: str | bytes | None,
+ ) -> bool:
pass
@abstractmethod
diff --git a/app/persistence/memory.py b/app/persistence/memory.py
index 95ccc43..aedc8ec 100644
--- a/app/persistence/memory.py
+++ b/app/persistence/memory.py
@@ -53,7 +53,12 @@ class MemoryCache(ICache):
self._cache.move_to_end(sha_key, last=False)
return res
- async def set(self, key: str, value: str | bytes | None, ttl_sec: int) -> bool:
+ async def set(
+ self,
+ key: str,
+ ttl_sec: int,
+ value: str | bytes | None,
+ ) -> bool:
"""
Set a value in the cache.
"""
diff --git a/app/persistence/redis.py b/app/persistence/redis.py
index 5e2ad29..b6b7551 100644
--- a/app/persistence/redis.py
+++ b/app/persistence/redis.py
@@ -92,7 +92,12 @@ class RedisCache(ICache):
logger.exception("Error getting value")
return res
- async def set(self, key: str, value: str | bytes | None, ttl_sec: int) -> bool:
+ async def set(
+ self,
+ key: str,
+ ttl_sec: int,
+ value: str | bytes | None,
+ ) -> bool:
"""
Set a value in the cache.