import logging import requests from settings import WEB_URL, INTERNAL_API_KEY logger = logging.getLogger("alert") class WebClient: def __init__(self, base_url: str = WEB_URL): self.base_url = base_url.rstrip("/") self.headers = {"X-Internal-Api-Key": INTERNAL_API_KEY} def submit_flat(self, flat_payload: dict) -> bool: try: r = requests.post( f"{self.base_url}/internal/flats", json=flat_payload, headers=self.headers, timeout=10, ) if r.status_code >= 400: logger.error(f"web rejected flat: {r.status_code} {r.text[:300]}") return False return True except requests.RequestException as e: logger.error(f"web unreachable: {e}") return False def heartbeat(self) -> None: try: requests.post( f"{self.base_url}/internal/heartbeat", json={"service": "alert"}, headers=self.headers, timeout=5, ) except requests.RequestException: pass def fetch_secrets(self) -> dict: """Pull the current runtime secrets dict from web. Empty on failure — caller falls back to env values.""" try: r = requests.get( f"{self.base_url}/internal/secrets", headers=self.headers, timeout=5, ) if r.ok: return r.json() or {} except requests.RequestException as e: logger.warning(f"secrets fetch failed: {e}") return {}