69 lines
2.3 KiB
Python
69 lines
2.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlmodel import Session
|
|
|
|
from core.database import get_session
|
|
from services.bot_lifecycle_service import (
|
|
deactivate_bot_instance,
|
|
delete_bot_instance,
|
|
disable_bot_instance,
|
|
enable_bot_instance,
|
|
start_bot_instance,
|
|
stop_bot_instance,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/api/bots/{bot_id}/start")
|
|
async def start_bot(bot_id: str, session: Session = Depends(get_session)):
|
|
try:
|
|
return await start_bot_instance(session, bot_id)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
except PermissionError as exc:
|
|
raise HTTPException(status_code=403, detail=str(exc)) from exc
|
|
except RuntimeError as exc:
|
|
raise HTTPException(status_code=500, detail=str(exc)) from exc
|
|
|
|
|
|
@router.post("/api/bots/{bot_id}/stop")
|
|
def stop_bot(bot_id: str, session: Session = Depends(get_session)):
|
|
try:
|
|
return stop_bot_instance(session, bot_id)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
except PermissionError as exc:
|
|
raise HTTPException(status_code=403, detail=str(exc)) from exc
|
|
|
|
|
|
@router.post("/api/bots/{bot_id}/enable")
|
|
def enable_bot(bot_id: str, session: Session = Depends(get_session)):
|
|
try:
|
|
return enable_bot_instance(session, bot_id)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
|
|
|
|
@router.post("/api/bots/{bot_id}/disable")
|
|
def disable_bot(bot_id: str, session: Session = Depends(get_session)):
|
|
try:
|
|
return disable_bot_instance(session, bot_id)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
|
|
|
|
@router.post("/api/bots/{bot_id}/deactivate")
|
|
def deactivate_bot(bot_id: str, session: Session = Depends(get_session)):
|
|
try:
|
|
return deactivate_bot_instance(session, bot_id)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
|
|
|
|
@router.delete("/api/bots/{bot_id}")
|
|
def delete_bot(bot_id: str, delete_workspace: bool = True, session: Session = Depends(get_session)):
|
|
try:
|
|
return delete_bot_instance(session, bot_id, delete_workspace=delete_workspace)
|
|
except ValueError as exc:
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|