"""Read public health and schema without credentials or creating a call."""
import json
from urllib.request import Request, urlopen


def read(path):
    request = Request(
        "https://api.cleolabs.com" + path,
        headers={"User-Agent": "CleoDocsVerification/1.0"},
        method="GET",
    )
    with urlopen(request, timeout=30) as response:
        return {"http_status": response.status, "body": json.load(response)}


if __name__ == "__main__":
    observations = {path: read(path) for path in ["/health", "/health/ready", "/openapi.json"]}
    schema = observations["/openapi.json"]["body"]
    schemas = schema.get("components", {}).get("schemas", {})
    call = schemas.get("CallView", {})
    request = schemas.get("CallCreate", {})
    observations["/openapi.json"]["body"] = {
        "paths": sorted(schema["paths"]),
        "create_returns_202": "202" in schema.get("paths", {}).get("/v1/calls", {}).get("post", {}).get("responses", {}),
        "call_fields": sorted(call.get("properties", {})),
        "task_id_required": "task_id" in call.get("required", []),
        "execution_schema": call.get("properties", {}).get("execution"),
        "protected_context_schema": request.get("properties", {}).get("protected_context"),
    }
    print(json.dumps(observations, indent=2))
