80 lines
2.5 KiB
Python
80 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Smoke-check organizations API v2 list and detail endpoints."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from typing import Any
|
|
from urllib.error import HTTPError, URLError
|
|
from urllib.parse import urlencode, urlparse
|
|
from urllib.request import Request, urlopen
|
|
|
|
|
|
def request_json(url: str) -> dict[str, Any]:
|
|
parsed_url = urlparse(url)
|
|
if parsed_url.scheme not in {"http", "https"}:
|
|
raise RuntimeError(f"Unsupported URL scheme for {url}")
|
|
|
|
request = Request(url, headers={"Accept": "application/json"}) # noqa: S310
|
|
try:
|
|
with urlopen(request, timeout=30) as response: # noqa: S310
|
|
body = response.read().decode("utf-8")
|
|
except HTTPError as exc:
|
|
detail = exc.read().decode("utf-8", errors="replace")
|
|
raise RuntimeError(f"GET {url} failed: HTTP {exc.code}: {detail}") from exc
|
|
except URLError as exc:
|
|
raise RuntimeError(f"GET {url} failed: {exc.reason}") from exc
|
|
|
|
return json.loads(body)
|
|
|
|
|
|
def first_result_uid(payload: dict[str, Any]) -> str:
|
|
data = payload.get("data")
|
|
if not isinstance(data, list) or not data:
|
|
raise RuntimeError("List response does not contain any organizations in data")
|
|
|
|
uid = data[0].get("uid")
|
|
if not isinstance(uid, str) or not uid:
|
|
raise RuntimeError("First organization does not contain uid")
|
|
return uid
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(
|
|
description="Send GET list and GET item requests to organizations API v2."
|
|
)
|
|
parser.add_argument(
|
|
"--base-url",
|
|
default="http://127.0.0.1:8000",
|
|
help="Backend base URL, default: http://127.0.0.1:8000",
|
|
)
|
|
parser.add_argument("--page-size", type=int, default=1)
|
|
args = parser.parse_args()
|
|
|
|
base_url = args.base_url.rstrip("/")
|
|
query = urlencode({"page_size": args.page_size})
|
|
list_url = f"{base_url}/api/v2/organizations/?{query}"
|
|
|
|
print(f"GET list: {list_url}")
|
|
list_payload = request_json(list_url)
|
|
print(json.dumps(list_payload, ensure_ascii=False, indent=2))
|
|
|
|
uid = first_result_uid(list_payload)
|
|
item_url = f"{base_url}/api/v2/organizations/{uid}/"
|
|
|
|
print(f"\nGET item: {item_url}")
|
|
item_payload = request_json(item_url)
|
|
print(json.dumps(item_payload, ensure_ascii=False, indent=2))
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
raise SystemExit(main())
|
|
except RuntimeError as exc:
|
|
print(exc, file=sys.stderr)
|
|
raise SystemExit(1) from exc
|