"""Where the OPNsense REST API listens, read from the driver's ``base_url``.""" from __future__ import annotations from urllib.parse import urlsplit #: The port the API listens on when ``base_url`` names none, per scheme. DEFAULT_PORTS = {"https": 443, "http": 80} #: Seconds the driver's reachability probe waits for the API port to accept. ALIVE_TIMEOUT = 5 def api_endpoint(base_url: str, fallback_host: str) -> tuple[str, int]: """``https://fw:8443`` -> ``("fw", 8443)``: the host and port of the REST API. Without a port the scheme's default applies; without a host, *fallback_host*. """ parts = urlsplit(base_url) port = parts.port or DEFAULT_PORTS.get(parts.scheme.lower(), DEFAULT_PORTS["https"]) return parts.hostname or fallback_host, port