Skip to content

Commit 760e80a

Browse files
Add readme section, rename Paginator to SeamPaginator
1 parent 42ad7de commit 760e80a

File tree

3 files changed

+108
-4
lines changed

3 files changed

+108
-4
lines changed

README.rst

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,110 @@ For example:
257257
except SeamActionAttemptTimeoutError as e:
258258
print("Door took too long to unlock")
259259
260+
Pagination
261+
~~~~~~~~~~
262+
263+
Some Seam API endpoints that return lists of resources support pagination.
264+
Use the ``SeamPaginator`` class to fetch and process resources across multiple pages.
265+
266+
Manually fetch pages with the nextPageCursor
267+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
268+
269+
.. code-block:: python
270+
271+
from seam import Seam
272+
273+
seam = Seam()
274+
275+
paginator = seam.create_paginator(seam.connected_accounts.list, {"limit": 20})
276+
277+
connected_accounts, pagination = paginator.first_page()
278+
279+
if pagination.has_next_page:
280+
more_connected_accounts, _ = paginator.next_page(pagination.next_page_cursor)
281+
282+
Resume pagination
283+
^^^^^^^^^^^^^^^^^
284+
285+
Get the first page on initial load and store the state (e.g., in memory or a file):
286+
287+
.. code-block:: python
288+
289+
import json
290+
from seam import Seam
291+
292+
seam = Seam()
293+
294+
params = {"limit": 20}
295+
paginator = seam.create_paginator(seam.connected_accounts.list, params)
296+
297+
connected_accounts, pagination = paginator.first_page()
298+
299+
# Example: Store state for later use (e.g., in a file or database)
300+
pagination_state = {
301+
"params": params,
302+
"next_page_cursor": pagination.next_page_cursor,
303+
"has_next_page": pagination.has_next_page,
304+
}
305+
# with open("pagination_state.json", "w") as f:
306+
# with open("/tmp/seam_connected_accounts_list.json", "w") as f:
307+
# json.dump(pagination_state, f)
308+
309+
Get the next page at a later time using the stored state:
310+
311+
.. code-block:: python
312+
313+
import json
314+
from seam import Seam
315+
316+
seam = Seam()
317+
318+
# Example: Load state from where it was stored
319+
# with open("/tmp/seam_connected_accounts_list.json", "r") as f:
320+
# pagination_state = json.load(f)
321+
# Placeholder for loaded state:
322+
pagination_state = {
323+
"params": {"limit": 20},
324+
"next_page_cursor": "some_cursor_value",
325+
"has_next_page": True,
326+
}
327+
328+
329+
if pagination_state.get("has_next_page"):
330+
paginator = seam.create_paginator(
331+
seam.connected_accounts.list, pagination_state["params"]
332+
)
333+
more_connected_accounts, _ = paginator.next_page(
334+
pagination_state["next_page_cursor"]
335+
)
336+
337+
Iterate over all resources
338+
^^^^^^^^^^^^^^^^^^^^^^^^^^
339+
340+
.. code-block:: python
341+
342+
from seam import Seam
343+
344+
seam = Seam()
345+
346+
paginator = seam.create_paginator(seam.connected_accounts.list, {"limit": 20})
347+
348+
for account in paginator.flatten():
349+
print(account.account_type_display_name)
350+
351+
Return all resources across all pages as a list
352+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
353+
354+
.. code-block:: python
355+
356+
from seam import Seam
357+
358+
seam = Seam()
359+
360+
paginator = seam.create_paginator(seam.connected_accounts.list, {"limit": 20})
361+
362+
all_connected_accounts = paginator.flatten_to_list()
363+
260364
Interacting with Multiple Workspaces
261365
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
262366

seam/paginator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .pagination import Pagination
55

66

7-
class Paginator:
7+
class SeamPaginator:
88
"""
99
Handles pagination for API list endpoints.
1010

seam/seam.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .routes import Routes
88
from .models import AbstractSeam
99
from .client import SeamHttpClient
10-
from .paginator import Paginator
10+
from .paginator import SeamPaginator
1111

1212

1313
class Seam(AbstractSeam):
@@ -92,7 +92,7 @@ def __init__(
9292

9393
def create_paginator(
9494
self, request: Callable, params: Optional[Dict[str, Any]] = None, /
95-
) -> Paginator:
95+
) -> SeamPaginator:
9696
"""
9797
Creates a Paginator instance for iterating through list endpoints.
9898
@@ -113,7 +113,7 @@ def create_paginator(
113113
>>> for connected_account in connected_accounts_paginator.flatten():
114114
>>> print(connected_account.account_type_display_name)
115115
"""
116-
return Paginator(self.client, request, params)
116+
return SeamPaginator(self.client, request, params)
117117

118118
@classmethod
119119
def from_api_key(

0 commit comments

Comments
 (0)