#!/usr/bin/env python3
"""Observable data loader: fetches /sbom/ and /sbom/report/licences/ from the API."""
import json
import os
import urllib.request
import urllib.error

API_BASE = os.environ.get("API_BASE", "http://127.0.0.1:8000").rstrip("/")

result = {"entries": [], "licence_report": {"groups": [], "copyleft_direct_count": 0}}

try:
    with urllib.request.urlopen(f"{API_BASE}/sbom/", timeout=10) as resp:
        result["entries"] = json.loads(resp.read())
except urllib.error.URLError as e:
    result["error_entries"] = str(e)

try:
    with urllib.request.urlopen(f"{API_BASE}/sbom/report/licences/", timeout=10) as resp:
        result["licence_report"] = json.loads(resp.read())
except urllib.error.URLError as e:
    result["error_licences"] = str(e)

print(json.dumps(result))
