Spaces:
Runtime error
Runtime error
File size: 1,837 Bytes
5f12c93 e6a43c8 5f12c93 e6a43c8 624c7c9 e6a43c8 5f12c93 f485d1b | 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 | """
Helpers HTTP internos compartidos por todos los clientes de integración.
"""
import logging
import httpx
from app.config import settings
from app.integrations import core_auth
logger = logging.getLogger(__name__)
def _headers(token: str | None = None) -> dict:
h = {"Content-Type": "application/json"}
# Si no se pasó un token explícito (ej. propagar el del usuario), usar
# la cuenta de servicio de M6 — necesaria para llamadas disparadas por
# webhooks entrantes, donde no hay usuario humano autenticado. Sin
# CORE_SERVICE_EMAIL/CORE_SERVICE_PASSWORD configurados, devuelve None y
# el request sale sin header Authorization.
effective_token = token or core_auth.get_service_token()
if effective_token:
h["Authorization"] = f"Bearer {effective_token}"
return h
def _post(url: str, payload: dict, token: str | None = None) -> dict | None:
try:
r = httpx.post(url, json=payload, headers=_headers(token), timeout=settings.http_timeout_seconds)
r.raise_for_status()
return r.json()
except Exception as e:
logger.warning("Error en POST %s: %s", url, e)
return None
def _get(url: str, token: str | None = None) -> dict | None:
try:
r = httpx.get(url, headers=_headers(token), timeout=settings.http_timeout_seconds)
r.raise_for_status()
return r.json()
except Exception as e:
logger.warning("Error en GET %s: %s", url, e)
return None
def _patch(url: str, payload: dict, token: str | None = None) -> dict | None:
try:
r = httpx.patch(url, json=payload, headers=_headers(token), timeout=settings.http_timeout_seconds)
r.raise_for_status()
return r.json()
except Exception as e:
logger.warning("Error en PATCH %s: %s", url, e)
return None
|