anderson-ufrj
commited on
Commit
·
3c026fb
1
Parent(s):
473408b
fix: make notification advanced features optional for HuggingFace
Browse files- Add try/except imports for email and webhook services
- Make advanced features optional with fallbacks
- Protect API endpoints when features are unavailable
- Allow basic notification functionality without SMTP config
This ensures the app can start on HuggingFace without email/webhook dependencies.
src/api/routes/notifications.py
CHANGED
|
@@ -10,8 +10,16 @@ from src.services.notification_service import (
|
|
| 10 |
NotificationLevel,
|
| 11 |
Notification
|
| 12 |
)
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
from src.api.dependencies import get_current_user
|
| 16 |
from src.core.logging import get_logger
|
| 17 |
|
|
@@ -228,6 +236,9 @@ async def get_webhooks(
|
|
| 228 |
current_user: dict = Depends(get_current_user)
|
| 229 |
) -> List[Dict[str, Any]]:
|
| 230 |
"""Get user's webhook configurations."""
|
|
|
|
|
|
|
|
|
|
| 231 |
webhooks = webhook_service.list_webhooks()
|
| 232 |
|
| 233 |
# Filter by user (in production, this would be from database)
|
|
|
|
| 10 |
NotificationLevel,
|
| 11 |
Notification
|
| 12 |
)
|
| 13 |
+
# Optional imports
|
| 14 |
+
try:
|
| 15 |
+
from src.services.webhook_service import webhook_service, WebhookConfig
|
| 16 |
+
from src.models.notification_models import NotificationPreference
|
| 17 |
+
WEBHOOK_SUPPORT = True
|
| 18 |
+
except (ImportError, AttributeError):
|
| 19 |
+
webhook_service = None
|
| 20 |
+
WebhookConfig = None
|
| 21 |
+
NotificationPreference = None
|
| 22 |
+
WEBHOOK_SUPPORT = False
|
| 23 |
from src.api.dependencies import get_current_user
|
| 24 |
from src.core.logging import get_logger
|
| 25 |
|
|
|
|
| 236 |
current_user: dict = Depends(get_current_user)
|
| 237 |
) -> List[Dict[str, Any]]:
|
| 238 |
"""Get user's webhook configurations."""
|
| 239 |
+
if not WEBHOOK_SUPPORT or not webhook_service:
|
| 240 |
+
return []
|
| 241 |
+
|
| 242 |
webhooks = webhook_service.list_webhooks()
|
| 243 |
|
| 244 |
# Filter by user (in production, this would be from database)
|
src/services/notification_service.py
CHANGED
|
@@ -14,9 +14,21 @@ from enum import Enum
|
|
| 14 |
from pydantic import BaseModel, Field
|
| 15 |
import structlog
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
from src.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
from src.core.logging import get_logger
|
| 21 |
from src.core import json_utils
|
| 22 |
|
|
@@ -107,10 +119,10 @@ class NotificationService:
|
|
| 107 |
# Send through each channel
|
| 108 |
tasks = []
|
| 109 |
|
| 110 |
-
if "email" in channels:
|
| 111 |
tasks.append(self._send_email(user_id, notification))
|
| 112 |
|
| 113 |
-
if "webhook" in channels:
|
| 114 |
tasks.append(self._send_webhook(notification))
|
| 115 |
|
| 116 |
if "push" in channels:
|
|
@@ -231,6 +243,9 @@ class NotificationService:
|
|
| 231 |
|
| 232 |
async def _send_email(self, user_id: str, notification: Notification) -> bool:
|
| 233 |
"""Send notification via email."""
|
|
|
|
|
|
|
|
|
|
| 234 |
try:
|
| 235 |
# Get user email (would come from database)
|
| 236 |
user_email = f"{user_id}@example.com" # Placeholder
|
|
@@ -276,6 +291,9 @@ class NotificationService:
|
|
| 276 |
|
| 277 |
async def _send_webhook(self, notification: Notification) -> bool:
|
| 278 |
"""Send notification via webhook."""
|
|
|
|
|
|
|
|
|
|
| 279 |
try:
|
| 280 |
# Map notification type to webhook event
|
| 281 |
event_map = {
|
|
|
|
| 14 |
from pydantic import BaseModel, Field
|
| 15 |
import structlog
|
| 16 |
|
| 17 |
+
# Optional imports for advanced features
|
| 18 |
+
try:
|
| 19 |
+
from src.services.email_service import email_service, EmailMessage
|
| 20 |
+
from src.services.webhook_service import webhook_service, WebhookEvent
|
| 21 |
+
from src.models.notification_models import NotificationPreference, NotificationChannel
|
| 22 |
+
ADVANCED_FEATURES_AVAILABLE = True
|
| 23 |
+
except (ImportError, AttributeError):
|
| 24 |
+
# Fallback for environments without email/webhook support
|
| 25 |
+
email_service = None
|
| 26 |
+
webhook_service = None
|
| 27 |
+
EmailMessage = None
|
| 28 |
+
WebhookEvent = None
|
| 29 |
+
NotificationPreference = None
|
| 30 |
+
NotificationChannel = None
|
| 31 |
+
ADVANCED_FEATURES_AVAILABLE = False
|
| 32 |
from src.core.logging import get_logger
|
| 33 |
from src.core import json_utils
|
| 34 |
|
|
|
|
| 119 |
# Send through each channel
|
| 120 |
tasks = []
|
| 121 |
|
| 122 |
+
if "email" in channels and ADVANCED_FEATURES_AVAILABLE and email_service:
|
| 123 |
tasks.append(self._send_email(user_id, notification))
|
| 124 |
|
| 125 |
+
if "webhook" in channels and ADVANCED_FEATURES_AVAILABLE and webhook_service:
|
| 126 |
tasks.append(self._send_webhook(notification))
|
| 127 |
|
| 128 |
if "push" in channels:
|
|
|
|
| 243 |
|
| 244 |
async def _send_email(self, user_id: str, notification: Notification) -> bool:
|
| 245 |
"""Send notification via email."""
|
| 246 |
+
if not ADVANCED_FEATURES_AVAILABLE or not email_service or not EmailMessage:
|
| 247 |
+
return False
|
| 248 |
+
|
| 249 |
try:
|
| 250 |
# Get user email (would come from database)
|
| 251 |
user_email = f"{user_id}@example.com" # Placeholder
|
|
|
|
| 291 |
|
| 292 |
async def _send_webhook(self, notification: Notification) -> bool:
|
| 293 |
"""Send notification via webhook."""
|
| 294 |
+
if not ADVANCED_FEATURES_AVAILABLE or not webhook_service or not WebhookEvent:
|
| 295 |
+
return False
|
| 296 |
+
|
| 297 |
try:
|
| 298 |
# Map notification type to webhook event
|
| 299 |
event_map = {
|