Skip to content

Commit b7af427

Browse files
Add readme intructions on pagination
1 parent 95e40b1 commit b7af427

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,99 @@ rescue Seam::ActionAttemptTimeoutError
215215
end
216216
```
217217

218+
### Pagination
219+
220+
Some Seam API endpoints that return lists of resources support pagination.
221+
Use the `Seam::Paginator` class to fetch and process resources across multiple pages.
222+
223+
#### Manually fetch pages with the next_page_cursor
224+
225+
```ruby
226+
require "seam"
227+
228+
seam = Seam.new
229+
230+
paginator = seam.create_paginator(seam.connected_accounts.method(:list), {limit: 20})
231+
232+
connected_accounts, pagination = paginator.first_page
233+
234+
if pagination.has_next_page?
235+
more_connected_accounts, _ = paginator.next_page(pagination.next_page_cursor)
236+
end
237+
```
238+
239+
#### Resume pagination
240+
241+
Get the first page on initial load and store the state (e.g., in memory or a file):
242+
243+
```ruby
244+
require "seam"
245+
require "json"
246+
247+
seam = Seam.new
248+
249+
params = {limit: 20}
250+
paginator = seam.create_paginator(seam.connected_accounts.method(:list), params)
251+
252+
connected_accounts, pagination = paginator.first_page
253+
254+
# Example: Store state for later use (e.g., in a file or database)
255+
pagination_state = {
256+
"params" => params,
257+
"next_page_cursor" => pagination.next_page_cursor,
258+
"has_next_page" => pagination.has_next_page?
259+
}
260+
File.write("/tmp/seam_connected_accounts_list.json", JSON.dump(pagination_state))
261+
```
262+
263+
Get the next page at a later time using the stored state:
264+
265+
```ruby
266+
require "seam"
267+
require "json"
268+
269+
seam = Seam.new
270+
271+
# Example: Load state from where it was stored
272+
pagination_state_json = File.read("/tmp/seam_connected_accounts_list.json")
273+
pagination_state = JSON.parse(pagination_state_json)
274+
275+
if pagination_state["has_next_page"]
276+
paginator = seam.create_paginator(
277+
seam.connected_accounts.method(:list), pagination_state["params"]
278+
)
279+
more_connected_accounts, _ = paginator.next_page(
280+
pagination_state["next_page_cursor"]
281+
)
282+
end
283+
```
284+
285+
#### Iterate over all resources
286+
287+
```ruby
288+
require "seam"
289+
290+
seam = Seam.new
291+
292+
paginator = seam.create_paginator(seam.connected_accounts.method(:list), {limit: 20})
293+
294+
paginator.flatten.each do |account|
295+
puts account.account_type_display_name
296+
end
297+
```
298+
299+
#### Return all resources across all pages as a list
300+
301+
```ruby
302+
require "seam"
303+
304+
seam = Seam.new
305+
306+
paginator = seam.create_paginator(seam.connected_accounts.method(:list), {limit: 20})
307+
308+
all_connected_accounts = paginator.flatten_to_list
309+
```
310+
218311
### Interacting with Multiple Workspaces
219312

220313
Some Seam API endpoints interact with multiple workspaces. The `Seam::Http::SeamMultiWorkspace` client is not bound to a specific workspace and may use those endpoints with a personal access token authentication method.

0 commit comments

Comments
 (0)