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
20 changes: 18 additions & 2 deletions proofline/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ def _pagination_limit(resource: str = "Review") -> ProoflineError:
)


def _export_path(organization_id: UUID, dataset: ExportDataset) -> str:
# This path is built by hand rather than by a generated operation, so the
# organization must become one canonical UUID segment. Any other text could
# add segments, leave the organization through dot segments, or replace the
# dataset through "?" or "#", and the page would still decode as `dataset`.
try:
organization = (
organization_id if isinstance(organization_id, UUID) else UUID(organization_id)
)
except (TypeError, ValueError, AttributeError):
raise ValueError("Export organization_id must be a UUID.") from None
return f"/api/v1/organizations/{organization}/exports/{dataset}"


def _export_page_limit(max_pages: int) -> None:
if isinstance(max_pages, bool) or not isinstance(max_pages, int) or not 1 <= max_pages <= 1_000:
raise ValueError("Export pagination requires max_pages 1..1000.")
Expand Down Expand Up @@ -290,14 +304,15 @@ def export_reviews(
raise ValueError("Unknown export dataset.")
if format not in ("csv", "ndjson"):
raise ValueError("Export format must be csv or ndjson.")
path = _export_path(organization_id, dataset)
params = {"from": from_time, "to": to_time, "format": format}
if cursor is not None:
params["cursor"] = cursor
if group_by is not None:
params["group_by"] = group_by
try:
response = self._http.get(
f"/api/v1/organizations/{organization_id}/exports/{dataset}",
path,
params=params,
headers={"Accept": "text/csv" if format == "csv" else "application/x-ndjson"},
)
Expand Down Expand Up @@ -660,14 +675,15 @@ async def export_reviews(
raise ValueError("Unknown export dataset.")
if format not in ("csv", "ndjson"):
raise ValueError("Export format must be csv or ndjson.")
path = _export_path(organization_id, dataset)
params = {"from": from_time, "to": to_time, "format": format}
if cursor is not None:
params["cursor"] = cursor
if group_by is not None:
params["group_by"] = group_by
try:
response = await self._http.get(
f"/api/v1/organizations/{organization_id}/exports/{dataset}",
path,
params=params,
headers={"Accept": "text/csv" if format == "csv" else "application/x-ndjson"},
)
Expand Down
75 changes: 75 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,81 @@ async def test_export_iterator_validates_bound_before_requests(mode: str, bound:
assert not server.requests


EXPORT_WINDOW: dict[str, Any] = {
"from_time": "2026-10-01T00:00:00Z",
"to_time": "2026-10-25T00:00:00Z",
}


# Each value would otherwise change the request target: dot segments leave the
# organization, a slash adds path segments, and "?" or "#" replace the dataset.
@MODES
@pytest.mark.parametrize(
"organization_id",
[
"..",
".",
"",
f"{ORG}/../../reviews",
f"{ORG}/exports/spend?x=",
f"{ORG}#",
"not-a-uuid",
ORG.int,
None,
],
)
async def test_export_refuses_an_organization_id_that_is_not_a_uuid(
mode: str, organization_id: Any
) -> None:
with Server(lambda request, index: Reply(status=404, body={"detail": "no"})) as server:
async with session(mode, server.url) as client:
with pytest.raises(ValueError, match="organization_id"):
await call(client, "export_reviews", organization_id, "reviews", **EXPORT_WINDOW)
iterator = client.iter_export_pages(organization_id, "reviews", **EXPORT_WINDOW)
with pytest.raises(ValueError, match="organization_id"):
await next_export(iterator)
assert not server.requests


# ORG has no hex letters, so its upper-case spelling would equal its string.
LETTERED_ORG = UUID("3fa85f64-5717-4562-b3fc-2c963f66afa6")
ORG_SPELLINGS = [
LETTERED_ORG,
str(LETTERED_ORG),
str(LETTERED_ORG).upper(),
f"{{{LETTERED_ORG}}}",
LETTERED_ORG.hex.upper(),
]


def test_organization_spellings_are_distinct() -> None:
assert len({repr(spelling) for spelling in ORG_SPELLINGS}) == len(ORG_SPELLINGS)


@MODES
@pytest.mark.parametrize("organization_id", ORG_SPELLINGS)
async def test_export_sends_the_canonical_organization_path(
mode: str, organization_id: Any
) -> None:
def respond(request: Request, index: int) -> Reply:
return Reply(
body=b'{"dataset_version":1}\n',
headers={
"Content-Type": "application/x-ndjson",
"X-Proofline-Export-Version": "1",
"X-Proofline-Export-Settled-Until": "2026-10-25T00:00:00Z",
},
)

with Server(respond) as server:
async with session(mode, server.url) as client:
page = await call(client, "export_reviews", organization_id, "reviews", **EXPORT_WINDOW)
assert page.rows == [{"dataset_version": 1}]
assert urlsplit(server.requests[0].path).path == (
f"/api/v1/organizations/{LETTERED_ORG}/exports/reviews"
)


@MODES
@pytest.mark.parametrize(
("cursors", "resume", "yielded"),
Expand Down