Spaces:
Sleeping
Sleeping
Allow legacy text endpoints to use guest auth when no Authorization header is present
Browse files
backend/app/routers/legacy_router.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
|
| 2 |
-
from fastapi import APIRouter, HTTPException, BackgroundTasks, Depends
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
from app.schemas.text_schema import TextRequest
|
| 5 |
from app.dependencies import get_current_user
|
|
@@ -10,53 +10,64 @@ router = APIRouter()
|
|
| 10 |
_legacy_jobs = {}
|
| 11 |
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
@router.post("/analyze/text")
|
| 14 |
-
async def legacy_analyze_sync(req: TextRequest, user: dict = Depends(
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
|
| 31 |
@router.post("/analyze/text/async")
|
| 32 |
-
async def legacy_analyze_async(req: TextRequest, background_tasks: BackgroundTasks, user: dict = Depends(
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
|
| 55 |
@router.get("/analyze/text/status/{job_id}")
|
| 56 |
-
async def legacy_job_status(job_id: str, user: dict = Depends(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 1 |
|
| 2 |
+
from fastapi import APIRouter, HTTPException, BackgroundTasks, Depends, Header
|
| 3 |
from fastapi.responses import JSONResponse
|
| 4 |
from app.schemas.text_schema import TextRequest
|
| 5 |
from app.dependencies import get_current_user
|
|
|
|
| 10 |
_legacy_jobs = {}
|
| 11 |
|
| 12 |
|
| 13 |
+
async def get_optional_user(authorization: str = Header(None)):
|
| 14 |
+
if not authorization:
|
| 15 |
+
return {
|
| 16 |
+
"email": "guest@fakeshield.local",
|
| 17 |
+
"full_name": "Guest User",
|
| 18 |
+
"subscription_tier": "free",
|
| 19 |
+
"is_offline": True,
|
| 20 |
+
}
|
| 21 |
+
return await get_current_user(authorization)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
@router.post("/analyze/text")
|
| 25 |
+
async def legacy_analyze_sync(req: TextRequest, user: dict = Depends(get_optional_user)):
|
| 26 |
+
"""Compatibility endpoint for older clients that POST to /analyze/text"""
|
| 27 |
+
from app.services.pipeline import run_text_pipeline
|
| 28 |
+
try:
|
| 29 |
+
result, _ = await run_text_pipeline(
|
| 30 |
+
user_email=user["email"],
|
| 31 |
+
text=req.text,
|
| 32 |
+
mode=req.mode,
|
| 33 |
+
include_highlights=req.include_highlights,
|
| 34 |
+
)
|
| 35 |
+
return JSONResponse({"status": "success", "data": result})
|
| 36 |
+
except ValueError as e:
|
| 37 |
+
raise HTTPException(400, str(e))
|
| 38 |
+
except Exception as e:
|
| 39 |
+
raise HTTPException(500, str(e))
|
| 40 |
|
| 41 |
|
| 42 |
@router.post("/analyze/text/async")
|
| 43 |
+
async def legacy_analyze_async(req: TextRequest, background_tasks: BackgroundTasks, user: dict = Depends(get_optional_user)):
|
| 44 |
+
"""Compatibility async endpoint for older clients that POST to /analyze/text/async"""
|
| 45 |
+
import uuid
|
| 46 |
+
job_id = "job_" + str(uuid.uuid4())[:8]
|
| 47 |
+
_legacy_jobs[job_id] = {"status": "processing", "data": None, "user_email": user["email"]}
|
| 48 |
+
|
| 49 |
+
async def run_job():
|
| 50 |
+
from app.services.pipeline import run_text_pipeline
|
| 51 |
+
try:
|
| 52 |
+
result, _ = await run_text_pipeline(
|
| 53 |
+
user_email=user["email"],
|
| 54 |
+
text=req.text,
|
| 55 |
+
mode=req.mode,
|
| 56 |
+
include_highlights=req.include_highlights,
|
| 57 |
+
)
|
| 58 |
+
_legacy_jobs[job_id] = {"status": "complete", "data": result, "user_email": user["email"]}
|
| 59 |
+
except Exception as e:
|
| 60 |
+
_legacy_jobs[job_id] = {"status": "error", "data": str(e), "user_email": user["email"]}
|
| 61 |
+
|
| 62 |
+
background_tasks.add_task(run_job)
|
| 63 |
+
return {"status": "accepted", "job_id": job_id}
|
| 64 |
|
| 65 |
|
| 66 |
@router.get("/analyze/text/status/{job_id}")
|
| 67 |
+
async def legacy_job_status(job_id: str, user: dict = Depends(get_optional_user)):
|
| 68 |
+
if job_id not in _legacy_jobs:
|
| 69 |
+
raise HTTPException(404, "Job not found")
|
| 70 |
+
job = _legacy_jobs[job_id]
|
| 71 |
+
if job.get("user_email") != user["email"]:
|
| 72 |
+
raise HTTPException(403, "Not authorized to view this job status")
|
| 73 |
+
return job
|