Python v4 SDKs now support pagination, covering the four widely used strategies: Offset, Page, Cursor and Link based pagination. A paginated operation returns a Paginator[Page, Item] on the synchronous client and an AsyncPaginator on the asynchronous one.
Details
The SDK detects paginated operations from the pagination OpenAPI extension and generates the walk for each strategy the operation declares, selecting request and response fields with the JSON Pointers in your extension configuration. Iterate the paginator to receive items across every page, or call iter_pages() to work with one page at a time.
The paginator is lazy. Building one sends nothing, the first request goes out when iteration starts, a break ends the walk with no further request, and each new iteration starts again from the first page. The arguments you pass set the first request only, and the SDK moves through the pages the way the API defines them. Every page is an ordinary request, so authentication, retries and request_options apply to each one.
A page that fails raises ApiError from the loop, after every item before it, so wrap the loop rather than the call. A link walk refuses a next link to a different origin and raises ValueError.
Usage Example
Iterate the items across every page, or go page by page when you need the page itself:
from payments import PaymentsClient
from payments.core import ApiError
client = PaymentsClient(bearer_auth="YOUR_ACCESS_TOKEN")
try:
# Every payment across every page; pages are fetched as you iterate.
for payment in client.payments.list_payments(limit=50):
print(payment.id)
# One page at a time.
for page in client.payments.list_payments(limit=50).iter_pages():
print(f"Fetched {len(page.data)} payments in this page")
except ApiError as e:
print("A page failed with status", e.status_code)
On the async client the call is not awaited. It returns an AsyncPaginator that you iterate with async for:
async for payment in async_client.payments.list_payments(limit=50):
print(payment.id)
This feature is available in the Python v4 SDK. Generate it with version 2.x of the APIMatic CLI:
apimatic sdk generate --language=python
For the full set of flags and options, see the APIMatic CLI documentation.