CheddahBot/cheddahbot/press_advantage.py

82 lines
2.5 KiB
Python

"""Press Advantage REST API client."""
from __future__ import annotations
import logging
import httpx
log = logging.getLogger(__name__)
class PressAdvantageClient:
"""Thin wrapper around the Press Advantage API."""
BASE_URL = "https://app.pressadvantage.com"
def __init__(self, api_key: str):
self._api_key = api_key
self._client = httpx.Client(base_url=self.BASE_URL, timeout=30.0)
def _params(self, **extra) -> dict:
"""Build query params with api_key auth."""
return {"api_key": self._api_key, **extra}
def get_organizations(self) -> list[dict]:
"""GET /api/customers/organizations.json — list all orgs."""
resp = self._client.get(
"/api/customers/organizations.json",
params=self._params(),
)
resp.raise_for_status()
return resp.json()
def create_release(
self,
org_id: int,
title: str,
body: str,
description: str,
distribution: str = "standard",
schedule_distribution: str = "false",
) -> dict:
"""POST /api/customers/releases/with_content.json — create a draft release.
Uses form data with release[field] nested params (not JSON).
"""
resp = self._client.post(
"/api/customers/releases/with_content.json",
params=self._params(),
data={
"release[organization_id]": org_id,
"release[title]": title,
"release[body]": body,
"release[description]": description,
"release[distribution]": distribution,
"release[schedule_distribution]": schedule_distribution,
},
)
resp.raise_for_status()
return resp.json()
def get_release(self, release_id: int) -> dict:
"""GET /api/customers/releases/{id}.json — get release details."""
resp = self._client.get(
f"/api/customers/releases/{release_id}.json",
params=self._params(),
)
resp.raise_for_status()
return resp.json()
def get_built_urls(self, release_id: int) -> list[dict]:
"""GET /api/customers/releases/{id}/built_urls.json — get published URLs."""
resp = self._client.get(
f"/api/customers/releases/{release_id}/built_urls.json",
params=self._params(),
)
resp.raise_for_status()
return resp.json()
def close(self):
self._client.close()