From b2e513a915f565337fd7efac6657c2696699a01b Mon Sep 17 00:00:00 2001 From: JSC Date: Sun, 21 Sep 2025 13:55:24 +0200 Subject: [PATCH] feat: Add endpoint to retrieve TTS history for the current user and improve request model formatting --- app/api/v1/tts.py | 97 ++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/app/api/v1/tts.py b/app/api/v1/tts.py index b99a9ed..6021285 100644 --- a/app/api/v1/tts.py +++ b/app/api/v1/tts.py @@ -11,16 +11,19 @@ from app.core.dependencies import get_current_active_user_flexible from app.models.user import User from app.services.tts import TTSService - router = APIRouter(prefix="/tts", tags=["tts"]) class TTSGenerateRequest(BaseModel): """TTS generation request model.""" - text: str = Field(..., min_length=1, max_length=1000, description="Text to convert to speech") + text: str = Field( + ..., min_length=1, max_length=1000, description="Text to convert to speech" + ) provider: str = Field(default="gtts", description="TTS provider to use") - options: dict[str, Any] = Field(default_factory=dict, description="Provider-specific options") + options: dict[str, Any] = Field( + default_factory=dict, description="Provider-specific options" + ) class TTSResponse(BaseModel): @@ -51,6 +54,47 @@ async def get_tts_service( return TTSService(session) +@router.get("") +async def get_tts_list( + current_user: Annotated[User, Depends(get_current_active_user_flexible)], + tts_service: Annotated[TTSService, Depends(get_tts_service)], + limit: int = 50, + offset: int = 0, +) -> list[TTSResponse]: + """Get TTS list for the current user.""" + try: + if current_user.id is None: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="User ID not available", + ) + + tts_records = await tts_service.get_user_tts_history( + user_id=current_user.id, + limit=limit, + offset=offset, + ) + + return [ + TTSResponse( + id=tts.id, + text=tts.text, + provider=tts.provider, + options=tts.options, + sound_id=tts.sound_id, + user_id=tts.user_id, + created_at=tts.created_at.isoformat(), + ) + for tts in tts_records + ] + + except Exception as e: + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail=f"Failed to get TTS history: {e!s}", + ) from e + + @router.post("") async def generate_tts( request: TTSGenerateRequest, @@ -69,7 +113,7 @@ async def generate_tts( text=request.text, user_id=current_user.id, provider=request.provider, - **request.options + **request.options, ) tts_record = result["tts"] @@ -84,7 +128,7 @@ async def generate_tts( sound_id=tts_record.sound_id, user_id=tts_record.user_id, created_at=tts_record.created_at.isoformat(), - ) + ), } except ValueError as e: @@ -140,47 +184,6 @@ async def get_provider( ) -@router.get("") -async def get_tts_list( - current_user: Annotated[User, Depends(get_current_active_user_flexible)], - tts_service: Annotated[TTSService, Depends(get_tts_service)], - limit: int = 50, - offset: int = 0, -) -> list[TTSResponse]: - """Get TTS generation history for the current user.""" - try: - if current_user.id is None: - raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail="User ID not available", - ) - - tts_records = await tts_service.get_user_tts_history( - user_id=current_user.id, - limit=limit, - offset=offset, - ) - - return [ - TTSResponse( - id=tts.id, - text=tts.text, - provider=tts.provider, - options=tts.options, - sound_id=tts.sound_id, - user_id=tts.user_id, - created_at=tts.created_at.isoformat(), - ) - for tts in tts_records - ] - - except Exception as e: - raise HTTPException( - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - detail=f"Failed to get TTS history: {e!s}", - ) from e - - @router.delete("/{tts_id}") async def delete_tts( tts_id: int, @@ -213,4 +216,4 @@ async def delete_tts( raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to delete TTS: {e!s}", - ) from e \ No newline at end of file + ) from e