Skip to main content

Get all objects from paginated response

tip

The response of SWAT's REST API will not always return all objects that fit with the conditions but they will be paginated.

To query all objects, customer can use the data in meta section of the response payload. limit is the limit of number of objects in each page, offset is page number (the counting start from zero).

next is the next page, previous is the previous page, total_count is the count of all objects that fit with the conditions/query parameters, for example organization_code = ORGANIZATION_CODE

Below is code sample for get all bookings from paginated response, filtered by simulation id, order by id of the booking:

Request paginated data (bookings)
import time
import requests
import os

BASE_URL = os.environ.get("BASE_URL")
USERNAME = os.environ.get("USERNAME")
PASSWORD = os.environ.get("PASSWORD")

LIMIT = 10
SIMULATION_ID = 378
all_bookings = []
next_page = f"/api/v2/booking?limit={LIMIT}&order_by=id&simulation={SIMULATION_ID}"
while next_page:
url = f"{BASE_URL}{next_page}"
response_body = requests.get(url, auth=(USERNAME, PASSWORD)).json()
print(response_body["objects"])
all_bookings += response_body["objects"]
next_page = response_body["meta"]["next"]
if next_page is None:
break
time.sleep(0.1) # Avoid request too fast

print()
print(all_bookings)