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: 2 additions & 0 deletions src/workos/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ def request_page(

def _fetch(*, after: Optional[str] = None) -> SyncPage[D]:
next_params = {**(params or {}), "after": after}
next_params.pop("before", None)
return self.request_page(
method=method,
path=path,
Expand Down Expand Up @@ -922,6 +923,7 @@ async def request_page(

async def _fetch(*, after: Optional[str] = None) -> AsyncPage[D]:
next_params = {**(params or {}), "after": after}
next_params.pop("before", None)
return await self.request_page(
method=method,
path=path,
Expand Down
47 changes: 47 additions & 0 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,53 @@ async def _fetch(after=None):
class TestPaginationHTTPIntegration:
"""Integration test verifying auto_paging_iter fetches multiple pages via httpx."""

def test_forward_pagination_clears_before(self, workos, httpx_mock):
httpx_mock.add_response(
json={"data": [{"id": "1"}], "list_metadata": {"after": "next"}}
)
httpx_mock.add_response(json={"data": [{"id": "2"}], "list_metadata": {}})
params = {"before": "initial", "limit": 1, "organization_id": "org_1"}
page = workos.request_page("GET", ["items"], model=FakeItem, params=params)
assert [item.id for item in page] == ["1", "2"]
requests = httpx_mock.get_requests()
assert dict(requests[0].url.params) == {
"before": "initial",
"limit": "1",
"organization_id": "org_1",
}
assert dict(requests[1].url.params) == {
"after": "next",
"limit": "1",
"organization_id": "org_1",
}
assert params == {"before": "initial", "limit": 1, "organization_id": "org_1"}

@pytest.mark.asyncio
async def test_async_forward_pagination_clears_before(
self, async_workos, httpx_mock
):
httpx_mock.add_response(
json={"data": [{"id": "1"}], "list_metadata": {"after": "next"}}
)
httpx_mock.add_response(json={"data": [{"id": "2"}], "list_metadata": {}})
params = {"before": "initial", "limit": 1, "organization_id": "org_1"}
page = await async_workos.request_page(
"GET", ["items"], model=FakeItem, params=params
)
assert [item.id async for item in page] == ["1", "2"]
requests = httpx_mock.get_requests()
assert dict(requests[0].url.params) == {
"before": "initial",
"limit": "1",
"organization_id": "org_1",
}
assert dict(requests[1].url.params) == {
"after": "next",
"limit": "1",
"organization_id": "org_1",
}
assert params == {"before": "initial", "limit": 1, "organization_id": "org_1"}

def test_auto_paging_iter_fetches_two_pages(self, workos, httpx_mock):
org_base = {
"object": "organization",
Expand Down
Loading