Skip to content

Commit da52eaa

Browse files
fix: clear before cursor when advancing pagination
1 parent 9bbc82d commit da52eaa

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

‎src/workos/_base_client.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ def request_page(
679679

680680
def _fetch(*, after: Optional[str] = None) -> SyncPage[D]:
681681
next_params = {**(params or {}), "after": after}
682+
next_params.pop("before", None)
682683
return self.request_page(
683684
method=method,
684685
path=path,
@@ -922,6 +923,7 @@ async def request_page(
922923

923924
async def _fetch(*, after: Optional[str] = None) -> AsyncPage[D]:
924925
next_params = {**(params or {}), "after": after}
926+
next_params.pop("before", None)
925927
return await self.request_page(
926928
method=method,
927929
path=path,

‎tests/test_pagination.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,53 @@ async def _fetch(after=None):
111111
class TestPaginationHTTPIntegration:
112112
"""Integration test verifying auto_paging_iter fetches multiple pages via httpx."""
113113

114+
def test_forward_pagination_clears_before(self, workos, httpx_mock):
115+
httpx_mock.add_response(
116+
json={"data": [{"id": "1"}], "list_metadata": {"after": "next"}}
117+
)
118+
httpx_mock.add_response(json={"data": [{"id": "2"}], "list_metadata": {}})
119+
params = {"before": "initial", "limit": 1, "organization_id": "org_1"}
120+
page = workos.request_page("GET", ["items"], model=FakeItem, params=params)
121+
assert [item.id for item in page] == ["1", "2"]
122+
requests = httpx_mock.get_requests()
123+
assert dict(requests[0].url.params) == {
124+
"before": "initial",
125+
"limit": "1",
126+
"organization_id": "org_1",
127+
}
128+
assert dict(requests[1].url.params) == {
129+
"after": "next",
130+
"limit": "1",
131+
"organization_id": "org_1",
132+
}
133+
assert params == {"before": "initial", "limit": 1, "organization_id": "org_1"}
134+
135+
@pytest.mark.asyncio
136+
async def test_async_forward_pagination_clears_before(
137+
self, async_workos, httpx_mock
138+
):
139+
httpx_mock.add_response(
140+
json={"data": [{"id": "1"}], "list_metadata": {"after": "next"}}
141+
)
142+
httpx_mock.add_response(json={"data": [{"id": "2"}], "list_metadata": {}})
143+
params = {"before": "initial", "limit": 1, "organization_id": "org_1"}
144+
page = await async_workos.request_page(
145+
"GET", ["items"], model=FakeItem, params=params
146+
)
147+
assert [item.id async for item in page] == ["1", "2"]
148+
requests = httpx_mock.get_requests()
149+
assert dict(requests[0].url.params) == {
150+
"before": "initial",
151+
"limit": "1",
152+
"organization_id": "org_1",
153+
}
154+
assert dict(requests[1].url.params) == {
155+
"after": "next",
156+
"limit": "1",
157+
"organization_id": "org_1",
158+
}
159+
assert params == {"before": "initial", "limit": 1, "organization_id": "org_1"}
160+
114161
def test_auto_paging_iter_fetches_two_pages(self, workos, httpx_mock):
115162
org_base = {
116163
"object": "organization",

0 commit comments

Comments
 (0)