855f1b132c
OVH API v1, 5 commands: get account, list cloud projects, list/get dedicated servers. SHA-1 signed application-key auth, stdlib-only. py_compile clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
import json, os, sys, hashlib, urllib.parse, urllib.request, urllib.error
|
|
|
|
|
|
def _cfg():
|
|
return json.loads(os.environ.get("INTEGRATION_SECRETS", "{}"))
|
|
|
|
|
|
def _inputs():
|
|
return json.loads(os.environ.get("INTEGRATION_INPUTS", "{}"))
|
|
|
|
|
|
def _endpoint(cfg):
|
|
return (str(cfg.get("endpoint") or "https://eu.api.ovh.com/1.0")).rstrip("/")
|
|
|
|
|
|
def _server_time(cfg):
|
|
req = urllib.request.Request(_endpoint(cfg) + "/auth/time", method="GET")
|
|
with urllib.request.urlopen(req, timeout=30) as r:
|
|
return r.read().decode("utf-8", "replace").strip()
|
|
|
|
|
|
def request(method, path, cfg, body=None):
|
|
url = _endpoint(cfg) + path
|
|
app_key = str(cfg.get("application_key", ""))
|
|
app_secret = str(cfg.get("application_secret", ""))
|
|
consumer = str(cfg.get("consumer_key", ""))
|
|
ts = _server_time(cfg)
|
|
body_str = json.dumps(body) if body is not None else ""
|
|
to_sign = app_secret + "+" + consumer + "+" + method.upper() + "+" + url + "+" + body_str + "+" + ts
|
|
signature = "$1$" + hashlib.sha1(to_sign.encode("utf-8")).hexdigest()
|
|
headers = {
|
|
"X-Ovh-Application": app_key,
|
|
"X-Ovh-Consumer": consumer,
|
|
"X-Ovh-Timestamp": ts,
|
|
"X-Ovh-Signature": signature,
|
|
"Accept": "application/json",
|
|
}
|
|
data = None
|
|
if body is not None:
|
|
data = body_str.encode("utf-8")
|
|
headers["Content-Type"] = "application/json"
|
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
with urllib.request.urlopen(req, timeout=60) as r:
|
|
raw = r.read()
|
|
return json.loads(raw) if raw else {}
|
|
|
|
|
|
def _run(fn):
|
|
try:
|
|
print(json.dumps(fn(_cfg(), _inputs())))
|
|
except urllib.error.HTTPError as e:
|
|
print(json.dumps({"error": "HTTP " + str(e.code), "detail": e.read().decode("utf-8", "replace")}))
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(json.dumps({"error": str(e)}))
|
|
sys.exit(1)
|
|
|
|
|
|
def main(cfg, inputs):
|
|
resp = request("GET", "/me", cfg)
|
|
return {"ok": True, "nichandle": resp.get("nichandle")}
|
|
|
|
|
|
_run(main)
|