File size: 3,303 Bytes
6ca8e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""POST /api/corrections — submit a correction for a library's extracted docs."""

import os
import uuid
from typing import Optional

from fastapi import APIRouter
from pydantic import BaseModel

router = APIRouter(prefix="/api/corrections", tags=["corrections"])

DEV_MODE = os.getenv("DEV_MODE", "true").lower() == "true"
SUPABASE_URL = os.getenv("SUPABASE_URL", "")
SUPABASE_KEY = os.getenv("SUPABASE_KEY", "")

# In-memory store for DEV_MODE
_corrections: dict[str, dict] = {}


class CorrectionRequest(BaseModel):
    library: str
    version: str
    component_name: str
    field: str  # "gotcha" | "prop" | "usage_example" | "import_path"
    correction: str
    reporter_email: Optional[str] = None


class CorrectionResponse(BaseModel):
    correction_id: str
    status: str


@router.post("", response_model=CorrectionResponse)
async def submit_correction(req: CorrectionRequest) -> CorrectionResponse:
    """Submit a correction for a library's extracted documentation."""
    correction_id = str(uuid.uuid4())

    if DEV_MODE or not (SUPABASE_URL and SUPABASE_KEY):
        _corrections[correction_id] = {
            "id": correction_id,
            "library": req.library,
            "version": req.version,
            "component_name": req.component_name,
            "field": req.field,
            "correction": req.correction,
            "reporter_email": req.reporter_email,
            "status": "pending",
        }
    else:
        import httpx
        async with httpx.AsyncClient(timeout=10) as client:
            await client.post(
                f"{SUPABASE_URL}/rest/v1/corrections",
                headers={
                    "apikey": SUPABASE_KEY,
                    "Authorization": f"Bearer {SUPABASE_KEY}",
                    "Content-Type": "application/json",
                    "Prefer": "return=minimal",
                },
                json={
                    "id": correction_id,
                    "library": req.library,
                    "version": req.version,
                    "component_name": req.component_name,
                    "field": req.field,
                    "correction": req.correction,
                    "reporter_email": req.reporter_email,
                    "status": "pending",
                },
            )

    return CorrectionResponse(correction_id=correction_id, status="pending")


@router.get("/{library}")
async def get_corrections(library: str, version: Optional[str] = None) -> dict:
    """Get approved corrections for a library."""
    if DEV_MODE or not (SUPABASE_URL and SUPABASE_KEY):
        results = [
            c for c in _corrections.values()
            if c["library"] == library and c["status"] == "approved"
            and (version is None or c["version"] == version)
        ]
        return {"corrections": results}

    import httpx
    params = f"library=eq.{library}&status=eq.approved"
    if version:
        params += f"&version=eq.{version}"
    async with httpx.AsyncClient(timeout=10) as client:
        resp = await client.get(
            f"{SUPABASE_URL}/rest/v1/corrections?{params}",
            headers={"apikey": SUPABASE_KEY, "Authorization": f"Bearer {SUPABASE_KEY}"},
        )
    return {"corrections": resp.json() if resp.status_code == 200 else []}