Repository navigation
Expand file tree
/
Copy pathapi.py
More file actions
175 lines (135 loc) · 5.91 KB
/
Copy pathapi.py
File metadata and controls
175 lines (135 loc) · 5.91 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import os
from contextlib import asynccontextmanager
import httpx
from fastapi import Depends, 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")
async def _safe_pb_request(client: httpx.AsyncClient, method: str, url: str, **kwargs) -> httpx.Response:
try:
func = getattr(client, method.lower())
return await func(url, **kwargs)
except httpx.RequestError as e:
raise HTTPException(status_code=502, detail=f"Error de conexión con PocketBase: {e!s}") from e
@app.post("/registrar-friccion", dependencies=[Depends(_check_auth)])
async def registrar_friccion(f: FrictionInput, request: Request):
client: httpx.AsyncClient = request.app.state.http_client
payload = {
"user_id": f.user_id,
"description": f.description,
"severity": f.severity,
}
res = await _safe_pb_request(
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", dependencies=[Depends(_check_auth)])
async def list_fricciones(request: Request, limit: int = 50):
client: httpx.AsyncClient = request.app.state.http_client
res = await _safe_pb_request(
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", dependencies=[Depends(_check_auth)])
async def analyze_friction(friction_id: str, request: Request):
client: httpx.AsyncClient = request.app.state.http_client
get_res = await _safe_pb_request(
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 _safe_pb_request(
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,
dependencies=[Depends(_check_auth)],
)
async def api_analize_friction_endpoint(input_data: AnalyzeInput):
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}", dependencies=[Depends(_check_auth)])
async def delete_friction(friction_id: str, request: Request):
client: httpx.AsyncClient = request.app.state.http_client
res = await _safe_pb_request(
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):
if isinstance(exc, HTTPException):
return JSONResponse(status_code=exc.status_code, content={"detail": exc.detail})
return JSONResponse(status_code=500, content={"detail": str(exc)})