kirche-einfach/backend/app/flows/base_client.py

27 lines
872 B
Python

"""Shared base for LFX + Langflow async HTTP clients.
Consolidates base_url stripping, header building, per-request client
construction, and HTTPError → domain-error mapping. Subclasses only
declare their error types and endpoint calls.
"""
from __future__ import annotations
import httpx
class BaseApiClient:
"""Thin wrapper around httpx.AsyncClient construction."""
def __init__(self, base_url: str, timeout: float = 60.0, api_key: str | None = None) -> None:
self.base_url = base_url.rstrip("/")
self.timeout = timeout
self.api_key = api_key or ""
def _headers(self) -> dict[str, str]:
return {"x-api-key": self.api_key} if self.api_key else {}
def _client(self) -> httpx.AsyncClient:
return httpx.AsyncClient(
base_url=self.base_url, timeout=self.timeout, headers=self._headers()
)