Akash4911 commited on
Commit
eaeeb48
·
1 Parent(s): b348fb9

Allow legacy text endpoints to use guest auth when no Authorization header is present

Browse files
Files changed (1) hide show
  1. backend/app/routers/legacy_router.py +55 -44
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(get_current_user)):
15
- """Compatibility endpoint for older clients that POST to /analyze/text"""
16
- from app.services.pipeline import run_text_pipeline
17
- try:
18
- result, _ = await run_text_pipeline(
19
- user_email=user["email"],
20
- text=req.text,
21
- mode=req.mode,
22
- include_highlights=req.include_highlights,
23
- )
24
- return JSONResponse({"status": "success", "data": result})
25
- except ValueError as e:
26
- raise HTTPException(400, str(e))
27
- except Exception as e:
28
- raise HTTPException(500, str(e))
29
 
30
 
31
  @router.post("/analyze/text/async")
32
- async def legacy_analyze_async(req: TextRequest, background_tasks: BackgroundTasks, user: dict = Depends(get_current_user)):
33
- """Compatibility async endpoint for older clients that POST to /analyze/text/async"""
34
- import uuid
35
- job_id = "job_" + str(uuid.uuid4())[:8]
36
- _legacy_jobs[job_id] = {"status": "processing", "data": None, "user_email": user["email"]}
37
-
38
- async def run_job():
39
- from app.services.pipeline import run_text_pipeline
40
- try:
41
- result, _ = await run_text_pipeline(
42
- user_email=user["email"],
43
- text=req.text,
44
- mode=req.mode,
45
- include_highlights=req.include_highlights,
46
- )
47
- _legacy_jobs[job_id] = {"status": "complete", "data": result, "user_email": user["email"]}
48
- except Exception as e:
49
- _legacy_jobs[job_id] = {"status": "error", "data": str(e), "user_email": user["email"]}
50
-
51
- background_tasks.add_task(run_job)
52
- return {"status": "accepted", "job_id": job_id}
53
 
54
 
55
  @router.get("/analyze/text/status/{job_id}")
56
- async def legacy_job_status(job_id: str, user: dict = Depends(get_current_user)):
57
- if job_id not in _legacy_jobs:
58
- raise HTTPException(404, "Job not found")
59
- job = _legacy_jobs[job_id]
60
- if job.get("user_email") != user["email"]:
61
- raise HTTPException(403, "Not authorized to view this job status")
62
- return job
 
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