"""Pipeline state management — syncs with matrixhost portal API.""" import logging from datetime import datetime, timezone import httpx logger = logging.getLogger(__name__) class PipelineStateManager: """Manages pipeline state via portal API.""" def __init__(self, portal_url: str, api_key: str): self.portal_url = portal_url.rstrip("/") self.api_key = api_key async def fetch_active_pipelines(self) -> list[dict]: async with httpx.AsyncClient(timeout=15.0) as client: resp = await client.get( f"{self.portal_url}/api/pipelines/active", headers={"x-api-key": self.api_key}, ) resp.raise_for_status() data = resp.json() return data.get("pipelines", []) async def create_execution(self, pipeline_id: str, trigger_data: dict | None = None) -> dict: async with httpx.AsyncClient(timeout=10.0) as client: resp = await client.post( f"{self.portal_url}/api/pipelines/{pipeline_id}/execution", headers={"x-api-key": self.api_key}, json={"triggerData": trigger_data}, ) resp.raise_for_status() data = resp.json() return data["execution"] async def update_execution(self, execution_id: str, **kwargs) -> None: try: async with httpx.AsyncClient(timeout=10.0) as client: await client.put( f"{self.portal_url}/api/pipelines/executions/{execution_id}", headers={"x-api-key": self.api_key}, json=kwargs, ) except Exception: logger.warning("Failed to update execution %s", execution_id, exc_info=True) async def fetch_pending_approvals(self) -> list[dict]: try: async with httpx.AsyncClient(timeout=10.0) as client: resp = await client.get( f"{self.portal_url}/api/pipelines/executions/pending", headers={"x-api-key": self.api_key}, ) resp.raise_for_status() data = resp.json() return data.get("executions", []) except Exception: logger.debug("Failed to fetch pending approvals", exc_info=True) return []