Files
glutz-maintenances/glutz_api.py
T

69 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Glutz eAccess Cloud — minimaler JSON-RPC-Client (nur Stdlib).
Übernommen aus ../glutz-eaccess-api/glutz_api.py (auf Client-Funktionen
reduziert), damit dieses Deployment selbst-contained ist.
* Endpunkt: https://<instanz>.eaccess.glutz.com/rpc
* Auth: HTTP Basic (GLUTZ_USER / GLUTZ_PASS aus .env)
* Lesen: eAccess.getModel params=[Collection, Filter, Fields]
* Schreiben: 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:
"""Lädt KEY=VALUE aus .env (neben diesem Script), ohne bestehende ENV zu überschreiben."""
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:
"""Einzelner JSON-RPC-Aufruf; bei Fehler sys.exit mit Meldung."""
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"Netzwerkfehler bei {method}: {err}")
if "error" in data:
sys.exit(f"RPC-Fehler bei {method}: {json.dumps(data['error'])}")
return data.get("result", {})