From 164af6602756db2f6c951744854985ea13a9cd2e Mon Sep 17 00:00:00 2001 From: usaamahahmed <55158743+usaamahahmed101@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:18:56 -0600 Subject: [PATCH] Default base_url from PROOFLINE_BASE_URL ProoflineClient and AsyncProoflineClient now take base_url=None. When it is left out they read a non-empty PROOFLINE_BASE_URL, and otherwise use https://proofline.sh, matching the CLI, the MCP bridge and the TypeScript SDK. An explicit base_url wins; an empty variable counts as unset; a bad value is refused by name without echoing it. check.py drops the variable from the test environment like PROOFLINE_TOKEN. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- proofline/_transport.py | 20 ++++++++++++++++++ proofline/client.py | 14 +++++++------ scripts/check.py | 2 +- tests/test_http.py | 46 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3c7a6ca9..99b8e79b 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ with ProoflineClient(os.environ["PROOFLINE_TOKEN"]) as client: ... ``` -When no token is passed, the clients read `PROOFLINE_TOKEN` from the environment, keep the token in memory, and redact it from diagnostic output. `AsyncProoflineClient` exposes the same methods as coroutines that you call with `await`. The [SDK guide](https://proofline.sh/docs/sdks/) and [API reference](https://proofline.sh/docs/api/) cover every operation. +When no token is passed, the clients read `PROOFLINE_TOKEN` from the environment, keep the token in memory, and redact it from diagnostic output. When no `base_url` is passed, they read `PROOFLINE_BASE_URL` and otherwise use `https://proofline.sh`, as the CLI, the MCP bridge and the TypeScript SDK do. An explicit `base_url` wins over the variable, an empty variable counts as unset, and a value that is not an HTTPS origin or a loopback HTTP origin raises a `ValueError` that names `PROOFLINE_BASE_URL` without repeating the value. `AsyncProoflineClient` exposes the same methods as coroutines that you call with `await`. The [SDK guide](https://proofline.sh/docs/sdks/) and [API reference](https://proofline.sh/docs/api/) cover every operation. ## Errors and refusals diff --git a/proofline/_transport.py b/proofline/_transport.py index 7f490782..a37ae449 100644 --- a/proofline/_transport.py +++ b/proofline/_transport.py @@ -119,6 +119,26 @@ def token_value(token: str | SecretStr | None) -> SecretStr: return secret +DEFAULT_BASE_URL = "https://proofline.sh" + + +def configured_base_url(value: str | None) -> str: + """``base_url``, else a non-empty ``PROOFLINE_BASE_URL``, else the hosted origin.""" + if value is not None: + return base_url(value) + # An empty variable, as a CI template leaves behind, counts as unset. + environment = os.environ.get("PROOFLINE_BASE_URL", "") + if not environment: + return base_url(DEFAULT_BASE_URL) + try: + return base_url(environment) + except ValueError: + # Name the setting to fix, never the value: it may carry credentials. + raise ValueError( + "PROOFLINE_BASE_URL must be an HTTPS origin, or HTTP with a literal loopback address." + ) from None + + def base_url(value: str) -> str: try: parts = urlsplit(value) diff --git a/proofline/client.py b/proofline/client.py index dd5106f6..3ad899e0 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -210,6 +210,8 @@ def _export_next_cursor(page: ExportPage, seen: set[str]) -> str | None: class ProoflineClient(Operations): """Synchronous typed HTTP operations. + ``base_url`` defaults to ``PROOFLINE_BASE_URL``, then to ``https://proofline.sh``. + Name resolution, connection, TLS and socket operations stop at the configured HTTP timeout and at a wait deadline. """ @@ -218,11 +220,11 @@ def __init__( self, token: str | SecretStr | None = None, *, - base_url: str = "https://proofline.sh", + base_url: str | None = None, timeout: float = 30, ) -> None: - self._api = _configuration(token, base_url, timeout) - self._base_url = _transport.base_url(base_url) + self._base_url = _transport.configured_base_url(base_url) + self._api = _configuration(token, self._base_url, timeout) self._http = httpx.Client( base_url=self._base_url, auth=_transport.BearerAuth(self._api.token), @@ -588,11 +590,11 @@ def __init__( self, token: str | SecretStr | None = None, *, - base_url: str = "https://proofline.sh", + base_url: str | None = None, timeout: float = 30, ) -> None: - self._api = _configuration(token, base_url, timeout) - self._base_url = _transport.base_url(base_url) + self._base_url = _transport.configured_base_url(base_url) + self._api = _configuration(token, self._base_url, timeout) self._http = httpx.AsyncClient( base_url=self._base_url, auth=_transport.BearerAuth(self._api.token), diff --git a/scripts/check.py b/scripts/check.py index b98a5a80..73370a82 100644 --- a/scripts/check.py +++ b/scripts/check.py @@ -21,7 +21,7 @@ def python_at(directory: Path) -> Path: def command(*args: str, cwd: Path = ROOT) -> None: env = dict(os.environ) - for name in ("PYTHONPATH", "PYTHONHOME", "PROOFLINE_TOKEN"): + for name in ("PYTHONPATH", "PYTHONHOME", "PROOFLINE_TOKEN", "PROOFLINE_BASE_URL"): env.pop(name, None) for name in tuple(env): if name.startswith("UV_"): diff --git a/tests/test_http.py b/tests/test_http.py index 7b922c34..b491013f 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1734,6 +1734,52 @@ def respond(request: Request, index: int) -> Reply: assert raised.value.type == "urn:proofline:sdk:pagination-limit" +@MODES +async def test_base_url_defaults_to_the_environment_then_the_hosted_origin( + mode: str, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.setenv("PROOFLINE_TOKEN", TOKEN) + with Server(lambda request, index: Reply(body=review())) as server: + monkeypatch.setenv("PROOFLINE_BASE_URL", server.url) + client = ProoflineClient() if mode == "sync" else AsyncProoflineClient() + assert client._base_url == server.url + result = await call(client, "get_review", ORG, REVIEW) + assert isinstance(result, CustomerReviewDetail) + closed = client.close() if mode == "sync" else await client.aclose() + assert closed is None + assert [request.path for request in server.requests] == [ + f"/api/v1/organizations/{ORG}/reviews/{REVIEW}" + ] + assert server.requests[0].headers["Authorization"] == f"Bearer {TOKEN}" + + # An explicit base_url wins over the variable. + with ProoflineClient(base_url="http://127.0.0.1:9") as explicit: + assert explicit._base_url == "http://127.0.0.1:9" + + # Unset or empty (as a CI template leaves it): the hosted origin. + for unset in (None, ""): + if unset is None: + monkeypatch.delenv("PROOFLINE_BASE_URL") + else: + monkeypatch.setenv("PROOFLINE_BASE_URL", unset) + with ProoflineClient() as hosted: + assert hosted._base_url == "https://proofline.sh" + async with AsyncProoflineClient() as hosted_async: + assert hosted_async._base_url == "https://proofline.sh" + + # A bad value is refused by name, without echoing it, before any request. + secret = "https://user:hunter2@proofline.example" + for value in [secret, "http://api.example", "https://proofline.sh/api"]: + monkeypatch.setenv("PROOFLINE_BASE_URL", value) + for factory in (ProoflineClient, AsyncProoflineClient): + with pytest.raises(ValueError, match="^PROOFLINE_BASE_URL must be") as caught: + factory() + assert "hunter2" not in str(caught.value) + # A bad explicit base_url keeps the existing message. + with pytest.raises(ValueError, match="^Use an HTTPS origin"): + ProoflineClient(base_url="http://api.example") + + def test_token_environment_and_https_origin_rejection(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("PROOFLINE_TOKEN", TOKEN) with ProoflineClient() as client: