homelab_automation/app/routes/notifications.py

82 lines
2.4 KiB
Python

"""
Routes API pour les notifications ntfy.
"""
from typing import Optional
from fastapi import APIRouter, Depends
from app.core.dependencies import verify_api_key
from app.schemas.notification import NotificationRequest, NotificationResponse, NtfyConfig
from app.services import notification_service
router = APIRouter()
@router.get("/config")
async def get_notification_config(api_key_valid: bool = Depends(verify_api_key)):
"""Récupère la configuration actuelle des notifications ntfy."""
config = notification_service.config
return {
"enabled": config.enabled,
"base_url": config.base_url,
"default_topic": config.default_topic,
"timeout": config.timeout,
"has_auth": config.has_auth,
}
@router.post("/test")
async def test_notification(
topic: Optional[str] = None,
message: str = "🧪 Test de notification depuis Homelab Automation API",
api_key_valid: bool = Depends(verify_api_key)
):
"""Envoie une notification de test."""
success = await notification_service.send(
topic=topic,
message=message,
title="🔔 Test Notification",
priority=3,
tags=["test_tube", "robot"]
)
return {
"success": success,
"topic": topic or notification_service.config.default_topic,
"message": "Notification envoyée" if success else "Échec de l'envoi (voir logs serveur)"
}
@router.post("/send", response_model=NotificationResponse)
async def send_custom_notification(
request: NotificationRequest,
api_key_valid: bool = Depends(verify_api_key)
):
"""Envoie une notification personnalisée via ntfy."""
return await notification_service.send_request(request)
@router.post("/toggle")
async def toggle_notifications(
enabled: bool,
api_key_valid: bool = Depends(verify_api_key)
):
"""Active ou désactive les notifications ntfy."""
current_config = notification_service.config
new_config = NtfyConfig(
base_url=current_config.base_url,
default_topic=current_config.default_topic,
enabled=enabled,
timeout=current_config.timeout,
username=current_config.username,
password=current_config.password,
token=current_config.token,
)
notification_service.reconfigure(new_config)
return {
"enabled": enabled,
"message": f"Notifications {'activées' if enabled else 'désactivées'}"
}