"""
FastAPI 路由定义
"""
import logging
from fastapi import APIRouter, HTTPException, Depends
from typing import List
from ..models.schemas import (
CompoundInfo, SafetyInfo, ToxicityData, ErrorResponse
)
from ..services.pubchem_service import PubChemService
from ..services.cache_service import CacheService
logger = logging.getLogger(__name__)
# 创建路由器
router = APIRouter(prefix="/api/v1", tags=["pubchem"])
# 依赖注入
async def get_pubchem_service() -> PubChemService:
"""获取PubChem服务实例"""
cache_service = CacheService()
await cache_service.initialize()
return PubChemService(cache_service)
@router.get("/compound/{name}", response_model=CompoundInfo)
async def get_compound_info(
name: str,
service: PubChemService = Depends(get_pubchem_service)
):
"""获取化合物基础信息"""
try:
result = await service.get_compound_info(name)
return result
except Exception as e:
logger.error(f"Error getting compound info for {name}: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/safety/{cid}", response_model=SafetyInfo)
async def get_safety_info(
cid: int,
service: PubChemService = Depends(get_pubchem_service)
):
"""获取GHS安全分类信息"""
try:
result = await service.get_safety_info(cid)
return result
except Exception as e:
logger.error(f"Error getting safety info for CID {cid}: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/toxicity/{cid}", response_model=ToxicityData)
async def get_toxicity_data(
cid: int,
service: PubChemService = Depends(get_pubchem_service)
):
"""获取毒性实验数据"""
try:
result = await service.get_toxicity_data(cid)
return result
except Exception as e:
logger.error(f"Error getting toxicity data for CID {cid}: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/health")
async def health_check():
"""健康检查"""
return {"status": "healthy", "service": "pubchem-mcp-server"}
@router.get("/")
async def root():
"""根路径"""
return {
"message": "PubChem Chemical Safety MCP Server",
"version": "0.1.0",
"endpoints": {
"compound_info": "/api/v1/compound/{name}",
"safety_info": "/api/v1/safety/{cid}",
"toxicity_data": "/api/v1/toxicity/{cid}",
"health": "/api/v1/health"
}
}