diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/harbor-ctl.py b/harbor-ctl.py index bc64a4a..64f733c 100755 --- a/harbor-ctl.py +++ b/harbor-ctl.py @@ -46,11 +46,20 @@ def api_request(url, user, password, method="GET", data=None): try: with urllib.request.urlopen(req, context=ctx, timeout=30) as resp: - if resp.status == 204 or resp.status == 202: - return {"status": resp.status} + # Handle responses with no body + if resp.status in (200, 201, 202, 204): + body = resp.read().decode().strip() + if not body: + return {"status": resp.status} + return json.loads(body) return json.loads(resp.read().decode()) except urllib.error.HTTPError as e: - return {"error": e.code, "message": e.read().decode()} + body = e.read().decode() + try: + err_json = json.loads(body) + return {"error": e.code, "message": err_json.get("errors", [{}])[0].get("message", body)} + except json.JSONDecodeError: + return {"error": e.code, "message": body} except Exception as e: return {"error": str(e)}