nanosdk.pagination
¶
Pagination: the Page record and the built-in next-page strategies.
Make a route iterable with Route.paginate, passing a
strategy that picks the next request from the current page:
list_posts = nanosdk.get("/posts", request=ListPosts, response=list[Post]).paginate(
nanosdk.Offset("offset", limit="limit")
)
for post in api.list_posts.iter(limit=50): ...
A strategy is any callable (Page) -> next, where next is one of:
- a new request model: sent through the route as usual;
- an
httpx.URL: the same request re-sent to that URL, whose query string replaces the model's query fields (for APIs that hand you a "next" link); None: stop.
Strategies that change a request field refer to it by its Python field name and need the route to have a request model.
Page
dataclass
¶
Page(
request: ReqT,
data: RespT,
items: list[ItemT],
response: Response,
)
One fetched page, as passed to strategies and yielded by .pages().
Attributes:
| Name | Type | Description |
|---|---|---|
request |
ReqT
|
The request model that produced this page ( |
data |
RespT
|
The decoded response body, after any |
items |
list[ItemT]
|
The page's items, as extracted by the route's |
response |
Response
|
The raw |
NextPage
¶
A pagination strategy: given the current page, the next request model, URL or None.
Strategies¶
Cursor
dataclass
¶
Read a cursor from each response and send it back in a request field.
Stops when the cursor is missing, None or empty.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
str | Callable[[Any], Any]
|
Where the cursor is in the decoded response: a dotted path through attributes or dict keys, or a function of the response. |
required |
param
|
str
|
The request-model field to put the cursor in. |
'cursor'
|
Methods:
| Name | Description |
|---|---|
__call__ |
Return the request for the page after |
Offset
dataclass
¶
Advance an offset request field by the number of items received.
Stops on an empty page, or on one shorter than the page size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param
|
str
|
The request-model field holding the offset (missing or |
'offset'
|
limit
|
str | None
|
The request-model field holding the page size, used to detect a short
last page; |
'limit'
|
Methods:
| Name | Description |
|---|---|
__call__ |
Return the request for the page after |
PageNumber
dataclass
¶
Increment a page-number request field.
Stops on an empty page, or on one shorter than the page size.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
param
|
str
|
The request-model field holding the page number. |
'page'
|
start
|
int
|
The number of the first page, assumed when the field is unset or |
1
|
size
|
str | None
|
The request-model field holding the page size, used to detect a short
last page; |
None
|
Methods:
| Name | Description |
|---|---|
__call__ |
Return the request for the page after |
LinkHeader
dataclass
¶
LinkHeader(rel: str = 'next')
Follow the Link: <url>; rel="next" response header (RFC 8288).
Used by GitHub, GitLab and many others. The linked URL (resolved against the response URL) replaces the query string; the route needs no request model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rel
|
str
|
The link relation to follow. |
'next'
|
Methods:
| Name | Description |
|---|---|
__call__ |
Return the URL of the page after |