Codex
Replace the dataset with the current testing tree and the three UT docs.
d26f22a
Raw History Blame Contribute Delete
1.8 kB
"""Native implementations of ExperimentBackend and TraceStore."""
from .experiment_backend import NativeExperimentBackend
from .store import NativeStore
from .trace_store import NativeTraceStore
from dataclasses import dataclass
from pathlib import Path
@dataclass(frozen=True)
class NativeAdapters:
experiment_backend: NativeExperimentBackend
trace_store: NativeTraceStore
def create_native_adapters(database: str | Path, endpoint: str) -> NativeAdapters:
"""Compose native adapters and verify that the collector is the intended store."""
import json
from urllib.request import build_opener, ProxyHandler
from urllib.parse import urlsplit
parsed = urlsplit(endpoint)
try:
parsed.port # force malformed-port validation
except ValueError as exc:
raise ValueError("native backend endpoint must be a valid HTTP origin") from exc
if (parsed.scheme != "http" or parsed.hostname not in {"127.0.0.1", "localhost"}
or parsed.username or parsed.password or parsed.path not in {"", "/"}
or parsed.query or parsed.fragment):
raise ValueError("native backend endpoint must be a loopback HTTP origin")
store = NativeStore(database)
with build_opener(ProxyHandler({})).open(endpoint.rstrip("/") + "/api/health", timeout=5) as response:
health = json.load(response)
if health.get("backend") != "native" or health.get("database_id") != store.database_id:
raise ValueError("collector and evaluator must use the same --database")
trace = NativeTraceStore(store)
return NativeAdapters(experiment_backend=NativeExperimentBackend(store, trace_store=trace, ui_url=endpoint),
trace_store=trace)
__all__ = ["NativeExperimentBackend", "NativeStore", "NativeTraceStore"]