Skip to content
Closed
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
24 changes: 24 additions & 0 deletions proofline/_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,30 @@ def base_url(value: str) -> str:
return value.rstrip("/")


HOSTED_ORIGIN = "https://proofline.sh"


def configured_base_url(value: str | None) -> str:
"""Resolve the origin: the argument, then PROOFLINE_BASE_URL, then the hosted origin.

An empty ``PROOFLINE_BASE_URL`` counts as unset, so a CI template that expands a
missing variable to an empty string still reaches the hosted origin. A bad value
read from the environment is reported by name and never echoed, because it can
carry credentials.
"""
if value is not None:
return base_url(value)
configured = os.environ.get("PROOFLINE_BASE_URL", "")
if not configured:
return HOSTED_ORIGIN
try:
return base_url(configured)
except ValueError:
raise ValueError(
"PROOFLINE_BASE_URL must be an HTTPS origin, or HTTP with a literal loopback address."
) from None


def invalid_response() -> ProoflineError:
return ProoflineError(
0, "urn:proofline:sdk:invalid-response", "Proofline returned an invalid API response."
Expand Down
12 changes: 6 additions & 6 deletions proofline/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,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 +588,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 @@ -2534,3 +2534,49 @@ def respond(request: Request, index: int) -> Reply:
assert len(exported.rows) == 1
assert exported.rows[0]["claim"] == row["claim"]
assert exported.next_cursor == "next-page"


@MODES
async def test_base_url_defaults_to_the_environment_then_the_hosted_origin(
mode: str, monkeypatch: pytest.MonkeyPatch
) -> None:
def respond(request: Request, index: int) -> Reply:
return Reply(body=page())

with Server(respond) as server:
monkeypatch.setenv("PROOFLINE_BASE_URL", server.url)
if mode == "sync":
with ProoflineClient(SecretStr(TOKEN)) as client:
await call(client, "list_reviews", ORG)
else:
async with AsyncProoflineClient(SecretStr(TOKEN)) as client:
await call(client, "list_reviews", ORG)
assert server.requests[0].path.startswith(f"/api/v1/organizations/{ORG}/reviews")
assert server.requests[0].headers["Authorization"] == f"Bearer {TOKEN}"

with Server(respond) as explicit_server:
monkeypatch.setenv("PROOFLINE_BASE_URL", "http://127.0.0.1:9")
async with session(mode, explicit_server.url) as client:
await call(client, "list_reviews", ORG)
assert len(explicit_server.requests) == 1

monkeypatch.delenv("PROOFLINE_BASE_URL", raising=False)
assert _transport.configured_base_url(None) == "https://proofline.sh"
monkeypatch.setenv("PROOFLINE_BASE_URL", "")
assert _transport.configured_base_url(None) == "https://proofline.sh"
assert _transport.configured_base_url("https://staging.example") == "https://staging.example"

for bad in (
"http://example.com",
"https://user:secret@example.com",
"https://example.com/path",
"https://example.com?query=1",
):
monkeypatch.setenv("PROOFLINE_BASE_URL", bad)
with pytest.raises(ValueError) as refused:
_transport.configured_base_url(None)
assert str(refused.value).startswith("PROOFLINE_BASE_URL must be")
assert "secret" not in str(refused.value)

with pytest.raises(ValueError):
_transport.configured_base_url("http://example.com")