Pagination

Most Variational API endpoints that can return multiple results limit the number of objects returned by a single call. This means that a pagination mechanism must be used to retrieve more objects.

Endpoints using pagination can be identified by the presence of the top-level pagination key in the JSON data they return. In the Endpoint Reference their Python SDK return value is marked as ApiPage[T].

Different endpoints return different number of items on a single page. The Endpoint Reference specifies this number for each endpoint.

Example

When requesting the first page of trades, the response will contain query string parameters required to request the next page.

Example first request:

GET https://api.testnet.variational.io/v1/portfolio/trades?pool=daf1b8ef-bac8-47a0-8d41-f18c9aed8442

Example response:

{
    "result": [{ ... }, { ... }, ...],
    "pagination": {
        "next_page": {
            "limit": "1000",
            "offset": "1000"
        }
    }
}

This means that the next page can be retrieved using the URL:

The last page of the results will still contain the pagination key but next_page inside of it will be null:

Python SDK

For flexibility, the Python SDK Client methods don't perform the pagination automatically:

Manual Pagination

If necessary, the next page can be retrieved afterward:

Using the Helper

To avoid boilerplate, the SDK provides a helper generator function paginate() that yields the requested objects making API calls lazily while it's being consumed.

Signature:

This code fetches all trades from the previous example into a list using the helper function:

An example making use of lazy pagination:

Logging

If you have a large amount of activity in your account, it might take multiple requests and a significant amount of time to fetch all resulting objects using pagination. Running multiple pagination requests back to back makes it likely to run into Rate Limits. The SDK Client retries them automatically by default but the entire operation will become slowed down.

It might be useful to enable logging in your application to get an indication of progress.

A simple logging configuration that enables debug-level logging for all facilities:

Advanced configuration enabling only relevant loggers:

Last updated