94 lines
3.9 KiB
Python

"""Bot API endpoints"""
from fastapi import APIRouter, HTTPException
# Import shared models - NO FALLBACKS!
try:
from shared.models import (
BotProviderRegisterRequest,
BotProviderRegisterResponse,
BotProviderListResponse,
BotListResponse,
BotJoinLobbyRequest,
BotJoinLobbyResponse,
BotLeaveLobbyRequest,
BotLeaveLobbyResponse,
BotInstanceModel,
)
except ImportError as e:
raise ImportError(
f"Failed to import shared models: {e}. Ensure shared/models.py is accessible and PYTHONPATH is correctly set."
)
def create_bot_router(
bot_manager, session_manager, lobby_manager, public_url: str = "/"
) -> APIRouter:
"""Create bot API router with dependencies"""
router = APIRouter(prefix=f"{public_url}api/bots", tags=["bots"])
@router.post("/providers/register", response_model=BotProviderRegisterResponse)
async def register_bot_provider(request: BotProviderRegisterRequest) -> BotProviderRegisterResponse:
"""Register a new bot provider with authentication"""
try:
return await bot_manager.register_provider(request)
except ValueError as e:
if "Invalid provider key" in str(e):
raise HTTPException(status_code=403, detail=str(e))
else:
raise HTTPException(status_code=400, detail=str(e))
@router.get("/providers", response_model=BotProviderListResponse)
async def list_bot_providers() -> BotProviderListResponse:
"""List all registered bot providers"""
return bot_manager.list_providers()
@router.get("", response_model=BotListResponse)
async def list_available_bots() -> BotListResponse:
"""List all available bots from all registered providers"""
return await bot_manager.list_bots()
@router.post("/{bot_name}/join", response_model=BotJoinLobbyResponse)
async def request_bot_join_lobby(bot_name: str, request: BotJoinLobbyRequest) -> BotJoinLobbyResponse:
"""Request a bot to join a specific lobby"""
try:
return await bot_manager.request_bot_join(bot_name, request, session_manager, lobby_manager)
except ValueError as e:
if "not found" in str(e).lower():
raise HTTPException(status_code=404, detail=str(e))
elif "timeout" in str(e).lower():
raise HTTPException(status_code=504, detail=str(e))
elif "provider error" in str(e).lower():
raise HTTPException(status_code=502, detail=str(e))
else:
raise HTTPException(status_code=500, detail=str(e))
@router.post(
"/instances/{bot_instance_id}/leave", response_model=BotLeaveLobbyResponse
)
async def request_bot_leave_lobby(bot_instance_id: str) -> BotLeaveLobbyResponse:
"""Request a bot instance to leave from all lobbies and disconnect"""
try:
request = BotLeaveLobbyRequest(bot_instance_id=bot_instance_id)
return await bot_manager.request_bot_leave(request, session_manager)
except ValueError as e:
if "not found" in str(e).lower():
raise HTTPException(status_code=404, detail=str(e))
elif "not a bot" in str(e).lower():
raise HTTPException(status_code=400, detail=str(e))
else:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/instances/{bot_instance_id}", response_model=dict)
async def get_bot_instance(bot_instance_id: str) -> dict:
"""Get information about a specific bot instance"""
try:
return await bot_manager.get_bot_instance(bot_instance_id)
except ValueError as e:
if "not found" in str(e).lower():
raise HTTPException(status_code=404, detail=str(e))
else:
raise HTTPException(status_code=500, detail=str(e))
return router