"""Glutz eAccess Cloud — minimal JSON-RPC client (stdlib only). Taken from ../glutz-eaccess-api/glutz_api.py (reduced to the client functions) so that this deployment is self-contained. * Endpoint: https://.eaccess.glutz.com/rpc * Auth: HTTP Basic (GLUTZ_USER / GLUTZ_PASS from .env) * Read: eAccess.getModel params=[Collection, Filter, Fields] * Write: eAccess.setModel params=[Collection, DataObject] """ from __future__ import annotations import base64 import json import os import sys import urllib.error import urllib.request from pathlib import Path RPC_ID = 4711 def load_env(path: Path | None = None) -> None: """Load KEY=VALUE pairs from .env (next to this script); never overrides variables already present in the environment.""" env_file = path or Path(__file__).resolve().parent / ".env" if not env_file.is_file(): return for line in env_file.read_text().splitlines(): line = line.strip() if not line or line.startswith("#") or "=" not in line: continue key, _, value = line.partition("=") key, value = key.strip(), value.strip().strip('"').strip("'") if key and key not in os.environ: os.environ[key] = value def rpc(method: str, params: list) -> dict: """Single JSON-RPC call; exits with an error message on failure.""" host = os.environ["GLUTZ_HOST"].rstrip("/") user = os.environ["GLUTZ_USER"] password = os.environ["GLUTZ_PASS"] token = base64.b64encode(f"{user}:{password}".encode()).decode() payload = json.dumps( {"jsonrpc": "2.0", "id": RPC_ID, "method": method, "params": params} ).encode() req = urllib.request.Request( f"{host}/rpc", data=payload, headers={ "Content-Type": "application/json", "Authorization": f"Basic {token}", }, method="POST", ) try: with urllib.request.urlopen(req, timeout=30) as resp: data = json.load(resp) except urllib.error.HTTPError as err: sys.exit(f"HTTP {err.code}: {err.read().decode(errors='replace')[:400]}") except urllib.error.URLError as err: sys.exit(f"Network error during {method}: {err}") if "error" in data: sys.exit(f"RPC error during {method}: {json.dumps(data['error'])}") return data.get("result", {})