|
import funciones |
|
from io import BytesIO |
|
from fastapi import FastAPI, Form |
|
from fastapi import FastAPI, File, UploadFile |
|
from fastapi.responses import StreamingResponse, JSONResponse |
|
from fastapi import FastAPI, Depends, HTTPException, status |
|
from fastapi.security.api_key import APIKeyHeader |
|
|
|
API_KEY = "tu_super_secreta_clave_de_api" |
|
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False) |
|
|
|
async def get_api_key(api_key: str = Depends(api_key_header)): |
|
if not api_key or api_key != API_KEY: |
|
raise HTTPException( |
|
status_code=status.HTTP_401_UNAUTHORIZED, |
|
detail="Credenciales de autenticación inválidas", |
|
) |
|
return api_key |
|
|
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
@app.get("/health", |
|
tags=["Health Check"], |
|
description="Verifica el estado de salud de la API.", |
|
summary="Health Check" |
|
) |
|
async def health_check(): |
|
""" |
|
Este endpoint devuelve una respuesta 200 OK para indicar que la API está funcionando. |
|
""" |
|
return JSONResponse(content={"status": "ok"}, status_code=200) |
|
|
|
@app.post("/echo-image/", |
|
tags=["Health Check"], |
|
description="Test endpoint que recibe y regresa la misma imagen, para probar envío, recepción y problemas con api o red.", |
|
summary="Summary" |
|
) |
|
async def echo_image( |
|
image: UploadFile = File(...), |
|
|
|
): |
|
if not image.content_type.startswith("image/"): |
|
return {"error": "El archivo no es una imagen"} |
|
contents = await image.read() |
|
return StreamingResponse(BytesIO(contents), media_type=image.content_type) |
|
|
|
@app.post( |
|
"/identifica_documento/", |
|
tags=["General"], |
|
summary="Reconocimiento Avanzado de Documentos") |
|
async def procesa_documento(image: UploadFile = File(...)): |
|
if not image.content_type.startswith("image/"): |
|
return {"error": "El archivo no es una imagen"} |
|
return await funciones.identifica_documento(image) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post( |
|
"/procesa_ine/", |
|
tags=["Zurich"], |
|
summary="Documentos México") |
|
async def procesa_documento(image: UploadFile = File(...)): |
|
if not image.content_type.startswith("image/"): |
|
return {"error": "El archivo no es una imagen"} |
|
return await funciones.procesa_ine(image) |