Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions proofline/_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions proofline/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion scripts/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_"):
Expand Down
46 changes: 46 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down