-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
154 lines (118 loc) · 5.23 KB
/
Copy pathapi.py
File metadata and controls
154 lines (118 loc) · 5.23 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
from contextlib import asynccontextmanager
import httpx
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field
import core
FRICTIONLOG_API_KEY = os.getenv("FRICTIONLOG_API_KEY")
@asynccontextmanager
async def lifespan(app: FastAPI):
async with httpx.AsyncClient() as client:
app.state.http_client = client
yield
app = FastAPI(title="FrictionLog API", lifespan=lifespan)
class FrictionInput(BaseModel):
user_id: str = "anonymous"
description: str = Field(..., min_length=10, description="Descripción del problema")
severity: int = Field(1, ge=1, le=5)
class AnalyzeInput(BaseModel):
description: str = Field(..., min_length=5)
class IAAnalysisData(BaseModel):
categoria: str
tipo_problema: str
impacto: str
idea_solucion: str
class IAResponseWrapper(BaseModel):
analisis: IAAnalysisData
async def _check_auth(request: Request):
if FRICTIONLOG_API_KEY:
auth = request.headers.get("Authorization", "")
if auth != f"Bearer {FRICTIONLOG_API_KEY}":
raise HTTPException(status_code=401, detail="API Key inválida o ausente")
@app.post("/registrar-friccion")
async def registrar_friccion(f: FrictionInput, request: Request):
await _check_auth(request)
client: httpx.AsyncClient = request.app.state.http_client
payload = {
"user_id": f.user_id,
"description": f.description,
"severity": f.severity,
}
res = await client.post(f"{core.PB_URL}/api/collections/fricciones/records", json=payload)
if res.status_code != 200:
raise HTTPException(status_code=502, detail=f"Error en PocketBase: {res.text}")
return {"status": "ok", "id": res.json().get("id")}
@app.get("/fricciones")
async def list_fricciones(request: Request, limit: int = 50):
await _check_auth(request)
client: httpx.AsyncClient = request.app.state.http_client
res = await client.get(
f"{core.PB_URL}/api/collections/fricciones/records?sort=-created&perPage={limit}"
)
if res.status_code != 200:
raise HTTPException(status_code=502, detail="Error al buscar en PocketBase")
items = res.json().get("items", [])
return [
{
"id": item.get("id"),
"user_id": item.get("user_id", "anonymous"),
"description": item.get("description", ""),
"severity": item.get("severity", 1),
"created_at": item.get("created", ""),
"categoria": item.get("categoria"),
"tipo_problema": item.get("tipo_problema"),
"impacto": item.get("impacto"),
"idea_solucion": item.get("idea_solucion"),
}
for item in items
]
@app.post("/fricciones/{friction_id}/analizar")
async def analyze_friction(friction_id: str, request: Request):
await _check_auth(request)
client: httpx.AsyncClient = request.app.state.http_client
get_res = await client.get(f"{core.PB_URL}/api/collections/fricciones/records/{friction_id}")
if get_res.status_code != 200:
raise HTTPException(status_code=404, detail="Fricción no encontrada")
description = get_res.json().get("description", "")
analysis = await core.analyze_with_ai(description)
res_ia = analysis.get("response", {})
if "error" in res_ia:
raise HTTPException(status_code=502, detail=res_ia.get("idea_solucion", "Error de IA"))
patch_payload = {
"categoria": res_ia.get("categoria", "Desconocida"),
"tipo_problema": res_ia.get("tipo_problema", "Desconocido"),
"impacto": res_ia.get("impacto", "Desconocido"),
"idea_solucion": res_ia.get("idea_solucion", "Sin sugerencia"),
}
patch_res = await client.patch(
f"{core.PB_URL}/api/collections/fricciones/records/{friction_id}",
json=patch_payload,
)
if patch_res.status_code != 200:
raise HTTPException(status_code=502, detail=f"Error al actualizar IA: {patch_res.text}")
return {"status": "ok", "analysis": res_ia}
@app.post("/analizar-con-ia", response_model=IAResponseWrapper)
async def api_analize_friction_endpoint(input_data: AnalyzeInput, request: Request):
await _check_auth(request)
try:
resultado_ia = await core.analyze_with_ai(input_data.description)
res = resultado_ia.get("response", {})
if "error" in res:
raise HTTPException(status_code=502, detail=res.get("idea_solucion", "Error de IA"))
return {"analisis": res}
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error inesperado: {e!s}") from e
@app.delete("/fricciones/{friction_id}")
async def delete_friction(friction_id: str, request: Request):
await _check_auth(request)
client: httpx.AsyncClient = request.app.state.http_client
res = await client.delete(f"{core.PB_URL}/api/collections/fricciones/records/{friction_id}")
if res.status_code not in (200, 204):
raise HTTPException(status_code=502, detail=f"Error al eliminar en PocketBase: {res.text}")
return {"status": "ok"}
@app.exception_handler(Exception)
async def global_exception_handler(_request: Request, exc: Exception):
return JSONResponse(status_code=500, content={"detail": str(exc)})