48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
|
|
"""Vikunja tasks router."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
from typing import Any, Dict
|
||
|
|
|
||
|
|
from fastapi import APIRouter
|
||
|
|
|
||
|
|
from server.cache import cache
|
||
|
|
from server.config import settings
|
||
|
|
from server.services.vikunja_service import fetch_tasks
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/api", tags=["tasks"])
|
||
|
|
|
||
|
|
CACHE_KEY = "tasks"
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/tasks")
|
||
|
|
async def get_tasks() -> Dict[str, Any]:
|
||
|
|
"""Return Vikunja task data.
|
||
|
|
|
||
|
|
The exact shape depends on what ``fetch_tasks`` returns; on failure an
|
||
|
|
error stub is returned instead::
|
||
|
|
|
||
|
|
{ "error": true, "message": "..." }
|
||
|
|
"""
|
||
|
|
|
||
|
|
# --- cache hit? -----------------------------------------------------------
|
||
|
|
cached = await cache.get(CACHE_KEY)
|
||
|
|
if cached is not None:
|
||
|
|
return cached
|
||
|
|
|
||
|
|
# --- cache miss -----------------------------------------------------------
|
||
|
|
try:
|
||
|
|
data: Dict[str, Any] = await fetch_tasks(
|
||
|
|
settings.vikunja_url,
|
||
|
|
settings.vikunja_token,
|
||
|
|
)
|
||
|
|
except Exception as exc:
|
||
|
|
logger.exception("Failed to fetch Vikunja tasks")
|
||
|
|
return {"error": True, "message": str(exc)}
|
||
|
|
|
||
|
|
await cache.set(CACHE_KEY, data, settings.vikunja_cache_ttl)
|
||
|
|
return data
|