75 lines
No EOL
3 KiB
Python
75 lines
No EOL
3 KiB
Python
from fastapi import APIRouter, HTTPException, Depends
|
|
from typing import List
|
|
from ..models.agent import Agent, AgentCreate, AgentUpdate
|
|
from ..services.agent_service import AgentService
|
|
from ..auth import get_current_user
|
|
|
|
router = APIRouter(prefix="/api/agents", tags=["agents"])
|
|
|
|
@router.get("/", response_model=List[Agent])
|
|
async def list_agents(current_user: str = Depends(get_current_user)):
|
|
"""Liste tous les agents."""
|
|
try:
|
|
agent_service = AgentService()
|
|
agents = await agent_service.list_agents()
|
|
return agents
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.post("/", response_model=Agent)
|
|
async def create_agent(agent: AgentCreate, current_user: str = Depends(get_current_user)):
|
|
"""Crée un nouvel agent."""
|
|
try:
|
|
agent_service = AgentService()
|
|
new_agent = await agent_service.create_agent(agent)
|
|
return new_agent
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.get("/{agent_id}", response_model=Agent)
|
|
async def get_agent(agent_id: str, current_user: str = Depends(get_current_user)):
|
|
"""Récupère les informations d'un agent spécifique."""
|
|
try:
|
|
agent_service = AgentService()
|
|
agent = await agent_service.get_agent(agent_id)
|
|
if not agent:
|
|
raise HTTPException(status_code=404, detail="Agent non trouvé")
|
|
return agent
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.put("/{agent_id}", response_model=Agent)
|
|
async def update_agent(agent_id: str, agent: AgentUpdate, current_user: str = Depends(get_current_user)):
|
|
"""Met à jour un agent."""
|
|
try:
|
|
agent_service = AgentService()
|
|
updated_agent = await agent_service.update_agent(agent_id, agent)
|
|
if not updated_agent:
|
|
raise HTTPException(status_code=404, detail="Agent non trouvé")
|
|
return updated_agent
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.delete("/{agent_id}")
|
|
async def delete_agent(agent_id: str, current_user: str = Depends(get_current_user)):
|
|
"""Supprime un agent."""
|
|
try:
|
|
agent_service = AgentService()
|
|
success = await agent_service.delete_agent(agent_id)
|
|
if not success:
|
|
raise HTTPException(status_code=404, detail="Agent non trouvé")
|
|
return {"message": "Agent supprimé avec succès"}
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
@router.get("/{agent_id}/status")
|
|
async def get_agent_status(agent_id: str, current_user: str = Depends(get_current_user)):
|
|
"""Récupère le statut d'un agent."""
|
|
try:
|
|
agent_service = AgentService()
|
|
status = await agent_service.get_agent_status(agent_id)
|
|
if not status:
|
|
raise HTTPException(status_code=404, detail="Agent non trouvé")
|
|
return status
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e)) |