88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
# Copyright (c) 2025 Patrick Motsch
|
|
# All rights reserved.
|
|
"""
|
|
Options API routes for dynamic frontend options.
|
|
Provides endpoints for fetching options for select/multiselect fields.
|
|
"""
|
|
|
|
from fastapi import APIRouter, HTTPException, Depends, Query, Request
|
|
from typing import List, Dict, Any
|
|
import logging
|
|
|
|
from modules.auth import getCurrentUser, limiter
|
|
from modules.datamodels.datamodelUam import User
|
|
from modules.features.dynamicOptions.mainDynamicOptions import getOptions, getAvailableOptionsNames
|
|
from modules.services import getInterface as getServices
|
|
|
|
# Configure logger
|
|
logger = logging.getLogger(__name__)
|
|
|
|
router = APIRouter(
|
|
prefix="/api/options",
|
|
tags=["Options"],
|
|
responses={404: {"description": "Not found"}}
|
|
)
|
|
|
|
|
|
@router.get("/{optionsName}", response_model=List[Dict[str, Any]])
|
|
@limiter.limit("120/minute")
|
|
async def getOptionsEndpoint(
|
|
request: Request,
|
|
optionsName: str,
|
|
currentUser: User = Depends(getCurrentUser)
|
|
) -> List[Dict[str, Any]]:
|
|
"""
|
|
Get options for a given options name.
|
|
|
|
Path Parameters:
|
|
- optionsName: Name of the options set (e.g., "user.role", "user.connection")
|
|
|
|
Returns:
|
|
- List of option dictionaries with "value" and "label" keys
|
|
|
|
Examples:
|
|
- GET /api/options/user.role
|
|
- GET /api/options/user.connection
|
|
- GET /api/options/auth.authority
|
|
- GET /api/options/connection.status
|
|
"""
|
|
try:
|
|
logger.debug(f"Options request: {optionsName} for user {currentUser.id}")
|
|
services = getServices(currentUser, None)
|
|
options = getOptions(optionsName, services, currentUser)
|
|
logger.debug(f"Options response: {optionsName} returned {len(options)} items")
|
|
return options
|
|
except ValueError as e:
|
|
logger.error(f"ValueError for options {optionsName}: {str(e)}")
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail=str(e)
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"Error getting options for {optionsName}: {str(e)}")
|
|
raise HTTPException(
|
|
status_code=500,
|
|
detail=f"Failed to get options: {str(e)}"
|
|
)
|
|
|
|
|
|
@router.get("/", response_model=List[str])
|
|
@limiter.limit("30/minute")
|
|
async def listAvailableOptions(
|
|
request: Request,
|
|
currentUser: User = Depends(getCurrentUser)
|
|
) -> List[str]:
|
|
"""
|
|
Get list of all available options names.
|
|
|
|
Returns:
|
|
- List of available options names
|
|
"""
|
|
try:
|
|
return getAvailableOptionsNames()
|
|
except Exception as e:
|
|
logger.error(f"Error listing available options: {str(e)}")
|
|
raise HTTPException(
|
|
status_code=500,
|
|
detail=f"Failed to list options: {str(e)}"
|
|
)
|