Skip to content

Commit 9346a0c

Browse files
Update readme
1 parent c1fe18b commit 9346a0c

1 file changed

Lines changed: 87 additions & 1 deletion

File tree

README.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,92 @@ $access_code->status; // 'setting' (it will go to 'set' when active on the devic
6666
$seam->access_codes->delete($access_code->access_code_id);
6767
```
6868

69+
### Pagination
70+
71+
Some Seam API endpoints that return lists of resources support pagination.
72+
Use the `Paginator` class to fetch and process resources across multiple pages.
73+
74+
#### Manually fetch pages with the next_page_cursor
75+
76+
```php
77+
$pages = $seam->createPaginator(
78+
fn($params) => $seam->connected_accounts->list(...$params),
79+
["limit" => 2]
80+
);
81+
82+
[$connectedAccounts, $pagination] = $pages->firstPage();
83+
84+
if ($pagination->has_next_page) {
85+
[$moreConnectedAccounts] = $pages->nextPage(
86+
$pagination->next_page_cursor
87+
);
88+
}
89+
```
90+
91+
#### Resume pagination
92+
93+
Get the first page on initial load:
94+
95+
```php
96+
$params = ["limit" => 20];
97+
98+
$pages = $seam->createPaginator(
99+
fn($p) => $seam->connected_accounts->list(...$p),
100+
$params
101+
);
102+
103+
[$connectedAccounts, $pagination] = $pages->firstPage();
104+
105+
// Store pagination state for later use
106+
file_put_contents(
107+
'/tmp/seam_connected_accounts_list.json',
108+
json_encode([$params, $pagination])
109+
);
110+
```
111+
112+
Get the next page at a later time:
113+
114+
```php
115+
$stored_data = json_decode(
116+
file_get_contents('/tmp/seam_connected_accounts_list.json') ?: '[]',
117+
false
118+
);
119+
120+
$params = $stored_data[0] ?? [];
121+
$pagination = $stored_data[1] ?? (object)['has_next_page' => false, 'next_page_cursor' => null];
122+
123+
if ($pagination->has_next_page) {
124+
$pages = $seam->createPaginator(
125+
fn($p) => $seam->connected_accounts->list(...$p),
126+
$params
127+
);
128+
[$moreConnectedAccounts] = $pages->nextPage($pagination->next_page_cursor);
129+
}
130+
```
131+
#### Iterate over all resources
132+
133+
```php
134+
$pages = $seam->createPaginator(
135+
fn($p) => $seam->connected_accounts->list(...$p),
136+
["limit" => 20]
137+
);
138+
139+
foreach ($pages->flatten() as $connectedAccount) {
140+
print $connectedAccount->account_type_display_name . "\n";
141+
}
142+
```
143+
144+
#### Return all resources across all pages as an array
145+
146+
```php
147+
$pages = $seam->createPaginator(
148+
fn($p) => $seam->connected_accounts->list(...$p),
149+
["limit" => 20]
150+
);
151+
152+
$connectedAccounts = $pages->flattenToArray();
153+
```
154+
69155
## Installation
70156

71157
To install the latest version of the automatically generated SDK, run:
@@ -87,4 +173,4 @@ If you want to install our previous handwritten version, run:
87173
88174
### Running Tests
89175

90-
You'll need to export `SEAM_API_KEY` to a sandbox workspace API key.
176+
You'll need to export `SEAM_API_KEY` to a sandbox workspace API key.

0 commit comments

Comments
 (0)