All checks were successful
Java Maven 3.9.9 & JDK 26 CI/CD Pipeline / build-and-deploy (push) Successful in 3m22s
37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
"""HTTP client for external programs calling notify-gateway."""
|
|
import httpx
|
|
|
|
|
|
class NotifyClient:
|
|
def __init__(self, base_url: str, token: str, timeout: float = 20.0) -> None:
|
|
self.base_url = base_url.rstrip("/")
|
|
self.token = token
|
|
self.timeout = timeout
|
|
|
|
async def send(self, title: str, text: str, channels: list[str],
|
|
telegram_bot: str = "",
|
|
idempotency_key: str = "",
|
|
images: list[dict] | None = None) -> dict:
|
|
if not channels:
|
|
raise ValueError("channels must explicitly select at least one channel")
|
|
if "telegram" in channels and not telegram_bot.strip():
|
|
raise ValueError("telegram_bot is required for Telegram delivery")
|
|
payload = {"title": title, "text": text, "channels": channels}
|
|
if "telegram" in channels:
|
|
payload["telegram_bot"] = telegram_bot.strip()
|
|
if images:
|
|
payload["images"] = images
|
|
if idempotency_key:
|
|
payload["idempotency_key"] = idempotency_key
|
|
async with httpx.AsyncClient(timeout=self.timeout, trust_env=False) as client:
|
|
r = await client.post(
|
|
f"{self.base_url}/send",
|
|
headers={"Authorization": f"Bearer {self.token}"},
|
|
json=payload,
|
|
)
|
|
r.raise_for_status()
|
|
data = r.json()
|
|
if not data.get("ok"):
|
|
raise RuntimeError(f"notify-gateway delivery incomplete: {data.get('results', {})}")
|
|
return data
|