Просмотр исходного кода

Use credential freshness check for Homestay health probe instead of live GET

ZiB 4 месяцев назад
Родитель
Сommit
0993c2db63
3 измененных файлов с 18 добавлено и 3 удалено
  1. 2 3
      room_cal/_health.py
  2. 8 0
      room_cal/service_api/_homestay.py
  3. 8 0
      room_cal/service_api/_protocols.py

+ 2 - 3
room_cal/_health.py

@@ -8,7 +8,7 @@ from datetime import datetime, timezone
 from room_cal.store import Calendar, DEFAULT_CALENDAR_NAME
 from room_cal.service_api import ServiceAPI
 from room_cal.service_api.types import ServiceAuthError
-from room_cal._utils import services, local_today
+from room_cal._utils import services
 
 _log = logging.getLogger(__name__)
 
@@ -61,9 +61,8 @@ async def caldav_checks(calendar_name: str = DEFAULT_CALENDAR_NAME) -> tuple[Hea
 
 
 async def _probe_service(key: str, api: ServiceAPI) -> HealthResult:
-  today = local_today().isoformat()
   try:
-    await api._clients[key].get_calendar(today, today)
+    await api._clients[key].probe()
     return HealthResult(ok=True)
   except ServiceAuthError as e:
     return HealthResult(ok=False, reason=str(e))

+ 8 - 0
room_cal/service_api/_homestay.py

@@ -167,6 +167,14 @@ class _HomestayClient:
     # leisure_bookings field structure not yet validated against real data
     return []
 
+  async def probe(self) -> None:
+    from .credentials import check_credentials
+    if is_stale('homestay'):
+      raise HomestayAuthError(f'Homestay credentials are stale — run: {_AUTH_HINT}')
+    info = check_credentials('homestay')
+    if not info['valid']:
+      raise HomestayAuthError(f'Homestay credentials missing — run: {_AUTH_HINT}')
+
   async def _post_form(self, path: str, fields: dict) -> dict:
     body = {
       'utf8': '✓',

+ 8 - 0
room_cal/service_api/_protocols.py

@@ -9,6 +9,14 @@ class ServiceClient(Protocol):
   async def get_reservations(self, date_from: str, date_to: str) -> list[Reservation]: ...
   async def set_price(self, d: str, price: int) -> None: ...
   async def set_availability(self, d: str, available: bool) -> None: ...
+  async def probe(self) -> None:
+    """Liveness check used by the health monitor. Raises ServiceAuthError on failure.
+    Default implementation calls get_calendar for today. Override for clients where
+    a live calendar fetch is not a reliable health signal.
+    """
+    from room_cal._utils import local_today
+    today = local_today().isoformat()
+    await self.get_calendar(today, today)
 
 
 @runtime_checkable