diff --git a/src/workos/_base_client.py b/src/workos/_base_client.py index 7c6f7e64..4ace8d78 100644 --- a/src/workos/_base_client.py +++ b/src/workos/_base_client.py @@ -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, @@ -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, diff --git a/tests/test_pagination.py b/tests/test_pagination.py index 6d7fcdb1..29e49bbe 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -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",