wu981526092's picture
Security: Fix critical vulnerabilities before public release
bcbd2ec
"""
Methods API Router
This module provides API endpoints for managing knowledge extraction methods.
"""
from typing import Dict, Any, List, Optional
from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse
from backend.services.method_service import get_method_service
router = APIRouter(prefix="/api/methods", tags=["methods"])
# Get the method service instance
method_service = get_method_service()
@router.get("/available")
async def get_available_methods(method_type: Optional[str] = None, schema_type: Optional[str] = None) -> Dict[str, Any]:
"""
Get all available knowledge extraction methods with their metadata.
Args:
method_type: Optional filter by method type (production/baseline)
schema_type: Optional filter by schema type (reference_based/direct_based)
Returns:
Dictionary of method names to method information
"""
try:
methods = method_service.get_available_methods()
# Apply filters if specified
if method_type:
methods = {k: v for k, v in methods.items() if v["method_type"] == method_type}
if schema_type:
methods = {k: v for k, v in methods.items() if v["schema_type"] == schema_type}
return {
"status": "success",
"methods": methods,
"count": len(methods),
"filters": {
"method_type": method_type,
"schema_type": schema_type
}
}
except Exception as e:
raise HTTPException(status_code=500, detail="An internal error occurred while retrieving methods")
# Removed separate production/baseline endpoints - use unified /available endpoint with filtering
@router.get("/default")
async def get_default_method() -> Dict[str, Any]:
"""
Get the default method configuration.
Returns:
Default method name and information
"""
try:
default_method = method_service.get_default_method()
method_info = method_service.get_method_info(default_method)
return {
"status": "success",
"default_method": default_method,
"method_info": method_info
}
except Exception as e:
raise HTTPException(status_code=500, detail="An internal error occurred while retrieving default method")
@router.get("/{method_name}")
async def get_method_info(method_name: str) -> Dict[str, Any]:
"""
Get detailed information about a specific method.
Args:
method_name: Name of the method to retrieve
Returns:
Method information including capabilities and requirements
"""
try:
method_info = method_service.get_method_info(method_name)
if not method_info:
raise HTTPException(
status_code=404,
detail=f"Method '{method_name}' not found"
)
return {
"status": "success",
"method": method_info
}
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail="An internal error occurred while retrieving method info")
@router.post("/{method_name}/validate")
async def validate_method(method_name: str) -> Dict[str, Any]:
"""
Validate a method name and return validation result.
Args:
method_name: Name of the method to validate
Returns:
Validation result with method information if valid
"""
try:
validation_result = method_service.validate_method(method_name)
if not validation_result["valid"]:
return JSONResponse(
status_code=400,
content={
"status": "invalid",
"error": validation_result["error"]
}
)
return {
"status": "valid",
"method_info": validation_result["method_info"]
}
except Exception as e:
raise HTTPException(status_code=500, detail="An internal error occurred while validating method")
@router.get("/{method_name}/schema-compatibility")
async def get_method_schema_compatibility(method_name: str) -> Dict[str, Any]:
"""
Get schema compatibility information for a method.
Args:
method_name: Name of the method
Returns:
Schema compatibility information
"""
try:
compatibility_info = method_service.get_method_schema_compatibility(method_name)
if "error" in compatibility_info:
raise HTTPException(
status_code=404,
detail=compatibility_info["error"]
)
return {
"status": "success",
"compatibility": compatibility_info
}
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail="An internal error occurred while retrieving compatibility info")
# Removed separate filter endpoints - use unified /available endpoint with query parameters