"""Minimal Cellaria API client using only the Python standard library.""" import json import os import urllib.error import urllib.request class CellariaClient: def __init__(self, api_key, base_url="https://api.market.winerim.wine"): if not api_key: raise ValueError("Cellaria API key is required") self.api_key = api_key self.base_url = base_url.rstrip("/") def request(self, path, method="GET", body=None): data = json.dumps(body).encode("utf-8") if body is not None else None request = urllib.request.Request( f"{self.base_url}{path}", data=data, method=method, headers={ "Accept": "application/json", "Content-Type": "application/json", "X-API-Key": self.api_key, }, ) try: with urllib.request.urlopen(request, timeout=30) as response: return json.loads(response.read().decode("utf-8")) except urllib.error.HTTPError as error: payload = error.read().decode("utf-8") raise RuntimeError(f"Cellaria API {error.code}: {payload}") from error def overview(self): return self.request("/v1/market/overview") def ask(self, question, buyer_type="analyst", filters=None): return self.request( "/v1/market/ask", method="POST", body={"question": question, "buyer_type": buyer_type, "filters": filters or {}}, ) def create_report_request(self, title, question, region, wine_or_category, period="90d"): return self.request( "/v1/market/report-requests", method="POST", body={ "title": title, "question": question, "requested_scope": "Cellaria report", "region": region, "wine_or_category": wine_or_category, "period": period, "priority": "normal", }, ) if __name__ == "__main__": client = CellariaClient(os.environ.get("CELLARIA_API_KEY")) print(json.dumps(client.overview(), indent=2, ensure_ascii=False))