Download app/integrations/_base.py from Modulo-6/api: direct link, hf CLI and curl.
- Browser
- Download file 1.84 kB
-
https://huggingface.co/spaces/Modulo-6/api/resolve/main/app/integrations/_base.py
- Command line
-
hf download hf://spaces/Modulo-6/api/app/integrations/_base.py
-
curl -L -o _base.py https://huggingface.co/spaces/Modulo-6/api/resolve/main/app/integrations/_base.py
1.84 kB
| """ | |
| 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 | |