# Python 3, standard library only. Requires catalog:read permission. import hashlib import hmac import json import os import secrets import time from urllib.error import HTTPError from urllib.request import Request, urlopen key = os.environ["ZYNOO_API_KEY"] secret = os.environ["ZYNOO_API_SECRET"] path = "/v1/plans" timestamp = str(int(time.time())) nonce = secrets.token_hex(18) canonical = "\n".join([ timestamp, nonce, "GET", path, hashlib.sha256(b"").hexdigest(), "" ]) signature = hmac.new( secret.encode("utf-8"), canonical.encode("utf-8"), hashlib.sha256 ).hexdigest() request = Request( "https://bill.zynoo.com/vps-api/index.php" + path, headers={ "Accept": "application/json", "X-VR-Key": key, "X-VR-Timestamp": timestamp, "X-VR-Nonce": nonce, "X-VR-Signature-Version": "2", "X-VR-Signature": signature, }, method="GET", ) try: with urlopen(request, timeout=45) as response: result = json.load(response) except HTTPError as error: failure = json.loads(error.read()) raise RuntimeError(f"HTTP {error.code}: {failure['error']['code']}") from error if not result.get("ok"): raise RuntimeError(result.get("error", {}).get("code", "request_failed")) # Upsert local billing products by supplier_pid; keep every offered cycle. products = [{ "supplier_pid": int(plan["pid"]), "name": plan["name"], "group": plan["group"], "prices": plan["prices"], "provisioning": plan["provisioning"], } for plan in result["plans"]] print(json.dumps({"currency": result["currency"], "products": products}, indent=2, ensure_ascii=False))