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

Make service::disabled the single control point for enabling/disabling services

- services() now filters disabled entries, so all 15+ callers respect it automatically
- /services route derives supports_chat from ServiceAPI._clients instead of a separate hardcoded _CLIENT_CLASSES dict
- Restore homestay client registration; disable it via config.json disabled flag
ZiB 4 месяцев назад
Родитель
Сommit
a0ec9b7754
4 измененных файлов с 14 добавлено и 16 удалено
  1. 2 2
      room_cal/_utils.py
  2. 3 10
      room_cal/api/_routes/services.py
  3. 7 0
      room_cal/config.json
  4. 2 4
      room_cal/service_api/client.py

+ 2 - 2
room_cal/_utils.py

@@ -462,8 +462,8 @@ def local_today() -> date:
 
 
 
 
 def services() -> list[ServiceConfig]:
 def services() -> list[ServiceConfig]:
-  """Return list of ServiceConfig for all configured services."""
-  return list(get_config().services.values())
+  """Return list of ServiceConfig for all configured services, excluding disabled ones."""
+  return [s for s in get_config().services.values() if not s.disabled]
 
 
 
 
 def service_name(key: str) -> str:
 def service_name(key: str) -> str:

+ 3 - 10
room_cal/api/_routes/services.py

@@ -1,24 +1,17 @@
 from fastapi import APIRouter
 from fastapi import APIRouter
 from room_cal._utils import services as _services
 from room_cal._utils import services as _services
-from room_cal.service_api._airbnb import _AirbnbClient
-from room_cal.service_api._booking import _BookingClient
-# from room_cal.service_api._homestay import _HomestayClient  # disabled until service::disabled is applied uniformly
+from room_cal.service_api import ServiceAPI
 router = APIRouter()
 router = APIRouter()
 
 
-_CLIENT_CLASSES: dict[str, type] = {
-  'airbnb': _AirbnbClient,
-  'booking': _BookingClient,
-  # 'homestay': _HomestayClient,
-}
-
 
 
 @router.get('/services')
 @router.get('/services')
 async def get_services():
 async def get_services():
+  api = ServiceAPI()
   return [
   return [
     {
     {
       'key': s.key,
       'key': s.key,
       'name': s.name,
       'name': s.name,
-      'supports_chat': hasattr(_CLIENT_CLASSES.get(s.key), 'get_thread'),
+      'supports_chat': hasattr(api._clients.get(s.key), 'get_thread'),
       'account_id': s.account_id,
       'account_id': s.account_id,
     }
     }
     for s in _services()
     for s in _services()

+ 7 - 0
room_cal/config.json

@@ -29,6 +29,13 @@
       "fees": {
       "fees": {
         "commission": 0.15
         "commission": 0.15
       }
       }
+    },
+    "homestay": {
+      "name": "Homestay",
+      "disabled": true,
+      "fees": {
+        "client_fee": 0.16
+      }
     }
     }
   },
   },
   "rules": {
   "rules": {

+ 2 - 4
room_cal/service_api/client.py

@@ -4,7 +4,7 @@ from datetime import date
 
 
 from ._airbnb import _AirbnbClient
 from ._airbnb import _AirbnbClient
 from ._booking import _BookingClient
 from ._booking import _BookingClient
-# from ._homestay import _HomestayClient  # disabled until service::disabled is applied uniformly
+from ._homestay import _HomestayClient
 from ._protocols import ChatClient, ServiceClient
 from ._protocols import ChatClient, ServiceClient
 from .types import DayInfo, ServiceAuthError
 from .types import DayInfo, ServiceAuthError
 
 
@@ -28,12 +28,10 @@ class ServiceAPI:
     self._clients = {}
     self._clients = {}
     self._disabled: dict[str, str] = {}
     self._disabled: dict[str, str] = {}
     svc_cfg = get_config().services
     svc_cfg = get_config().services
-    # TODO: service::disabled is not applied uniformly across all codepaths;
-    # audit and fix before re-enabling homestay via config rather than here.
     for key, factory in {
     for key, factory in {
       'airbnb': _AirbnbClient.from_env,
       'airbnb': _AirbnbClient.from_env,
       'booking': _BookingClient.from_env,
       'booking': _BookingClient.from_env,
-      # 'homestay': _HomestayClient.from_env,
+      'homestay': _HomestayClient.from_env,
     }.items():
     }.items():
       if (cfg := svc_cfg.get(key)) and cfg.disabled:
       if (cfg := svc_cfg.get(key)) and cfg.disabled:
         _log.info('service %s disabled by config', key)
         _log.info('service %s disabled by config', key)