2026-10-23 08:49:49 +02:00
|
|
|
"""Glutz eAccess Cloud — minimal JSON-RPC client (stdlib only).
|
2026-10-22 21:45:29 +02:00
|
|
|
|
2026-10-23 08:49:49 +02:00
|
|
|
Taken from ../glutz-eaccess-api/glutz_api.py (reduced to the client
|
|
|
|
|
functions) so that this deployment is self-contained.
|
2026-10-22 21:45:29 +02:00
|
|
|
|
2026-10-23 08:49:49 +02:00
|
|
|
* Endpoint: https://<instance>.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]
|
2026-10-22 21:45:29 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
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:
|
2026-10-23 08:49:49 +02:00
|
|
|
"""Load KEY=VALUE pairs from .env (next to this script); never overrides
|
|
|
|
|
variables already present in the environment."""
|
2026-10-22 21:45:29 +02:00
|
|
|
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:
|
2026-10-23 08:49:49 +02:00
|
|
|
"""Single JSON-RPC call; exits with an error message on failure."""
|
2026-10-22 21:45:29 +02:00
|
|
|
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:
|
2026-10-23 08:49:49 +02:00
|
|
|
sys.exit(f"Network error during {method}: {err}")
|
2026-10-22 21:45:29 +02:00
|
|
|
if "error" in data:
|
2026-10-23 08:49:49 +02:00
|
|
|
sys.exit(f"RPC error during {method}: {json.dumps(data['error'])}")
|
2026-10-22 21:45:29 +02:00
|
|
|
return data.get("result", {})
|