# HTTP-клиент к publisher-service (микросервис). # web больше НЕ импортирует publisher.bot — только этот клиент (HTTP POST /api/v1/publish). import os import httpx PUBLISHER_URL = os.getenv("PUBLISHER_URL", "http://127.0.0.1:8410") TIMEOUT = 30.0 def publish(card: dict, channels: list[str] | None = None) -> dict: """Вызывает publisher-service. Возвращает {"ok", "results", "dry_run"} от сервиса. При недоступности сервиса — raise RuntimeError с понятным текстом. """ payload = {"card": card} if channels: payload["channels"] = channels try: r = httpx.post(f"{PUBLISHER_URL}/api/v1/publish", json=payload, timeout=TIMEOUT) except Exception as e: raise RuntimeError(f"publisher-service недоступен ({PUBLISHER_URL}): {e}") from e if r.status_code >= 400: try: detail = r.json().get("detail", r.text[:200]) except Exception: detail = r.text[:200] raise RuntimeError(f"publisher-service ошибка {r.status_code}: {detail}") return r.json() def get_views(channel: str, message_id: int) -> int: """Views по каналу/сообщению из publisher-service.""" try: r = httpx.get(f"{PUBLISHER_URL}/api/v1/views/{channel}/{message_id}", timeout=TIMEOUT) if r.status_code == 200: return int(r.json().get("views", 0)) except Exception: pass return 0