47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Optional
|
|
|
|
from fastapi import APIRouter, HTTPException, Query
|
|
|
|
from ..services.gloss_service import (
|
|
GlossBackendUnavailableError,
|
|
TranscriptionSourceNotFoundError,
|
|
UnsupportedPairError,
|
|
generate_gloss,
|
|
supported_pairs,
|
|
)
|
|
from ..services.project_service import ProjectOperationError, get_project_or_raise
|
|
|
|
router = APIRouter(prefix="/api/projects", tags=["projects"])
|
|
|
|
|
|
def _get_project_or_404(project_id: str) -> dict:
|
|
try:
|
|
return get_project_or_raise(project_id)
|
|
except ProjectOperationError as exc:
|
|
raise HTTPException(status_code=exc.status_code, detail=exc.detail) from exc
|
|
|
|
|
|
@router.get("/{project_id}/gloss")
|
|
def get_project_gloss(
|
|
project_id: str,
|
|
target_lang: str = Query(..., min_length=2),
|
|
source_lang: Optional[str] = Query(None),
|
|
):
|
|
project = _get_project_or_404(project_id)
|
|
try:
|
|
return generate_gloss(project, target_lang=target_lang, source_lang=source_lang)
|
|
except UnsupportedPairError:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail={
|
|
"error": "unsupported_pair",
|
|
"supported": supported_pairs(),
|
|
},
|
|
)
|
|
except TranscriptionSourceNotFoundError as exc:
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
except GlossBackendUnavailableError as exc:
|
|
raise HTTPException(status_code=503, detail=str(exc)) from exc
|