Pagination
To limit the size of the API Response, most of the GET API Requests returning a list of object will paginate the response.
Except for a few number of API Calls, the following applies:
- By Default, the API Response will return the first 100 entries in the list
- The number of entries can be defined with the query parameter
limit, with limit <= 1000
example:
/api/v1/orgs/{org_id}/logs?limit=100Depending on the type of data, and to "stability" of the returned data, MIst can use two different sort of pagination:
- for "static" data, the navigation between the different pages is using the
pagequery parameter and all the pagination information are provided in the HTTP Headers. - for data updated freqently, or highly depending on the
startandendparameters, Mist is return the URL for the next page to query in thenextparameter, until the last page.
Pagination by HTTP Headers
Many APIs provides an array as response for simplicity. When the amount of data is huge and pagination is desired, Mist will provide the pagination information in the HTTP Response Headers:
| Header Name | Type | Description |
|---|---|---|
X-Page-Limit | int | the limit |
X-Page-Page | int | the page number |
X-Page-Total | int | the total number of entries without pagination |
To retrieve the full list, it is then required to retrieve each page one by one, unti X-Page-Limit * X-Page-Page > X-Page-Total
To navigate between the pages, the API Request must add the page query parameter:
| Query Parameter | Type | Description |
|---|---|---|
limit | int | max number of results, default = 100, max is 1000 |
page | int | page number (> 1), default is 1 |
Python script example
import requests
URL = "https://..."
HEADERS = { "Authorization": "Token XXXXXXXXXXXXXXXXXXX" }
PAGE = 1
LIMIT = 100
TOTAL = None
DATA = []
while TOTAL and PAGE * LIMIT < TOTAL:
response = requests.get(URL, headers=HEADERS)
DATA += response.json()
TOTAL = response.headers.get("X-Page-Total")
LIMIT = response.headers.get("X-Page-Limit")
PAGE = response.headers.get("X-Page-Page")
print(DATA)Pagination by "next" attribute
When the requested data is highly dependant on the context, or may change between two requests (espcially if the start and end query parameter are not defined), the API Response will include additional attributes in the body response to simplify the navigation between the different pages:
| Attribute Name | Type | Description |
|---|---|---|
limit | int | the limit |
page | int | the page number |
total | int | total number of results |
next | string | URL to query to get the next page. The Generated URL will automatically set the required query parameters to keep the same context as the initial request |
results | list | list of results, len(results) <= limit |
To get the full list of entries, it is then required to query each next page until the next attribute is not present.
Python script example
import requests
HOST = "https://api.mist.com"
NEXT = "/api/v1/self/logs"
HEADERS = { "Authorization": "Token XXXXXXXXXXXXXXXXXXX" }
NEXT = None
DATA = []
while NEXT:
response = requests.get(f"{HOST}{NEXT}", headers=HEADERS)
response_json = response.json()
DATA += response_json.get("results", [])
NEXT = response_json.get("next")
print(DATA)