Ahoy there,
I need pagination working correctly and looking around for a good example. I have 15 records that need to be retrieved. This is to verify pagination. Currently only retrieving the first three records. next_link is equal to None. How can I get pagination working?
def get_event_attendees(event_id, track_id):
"""Fetches all attendees for a specific event ID."""
attendees = []
URL = f"{BASE_URL}/events/{event_id}/tracks/{track_id}/registrations"
params = {"limit": 3}
while URL:
print(f"Fetching: {URL}")
response = requests.get(URL, headers=HEADERS, params=params)
if response.status_code == 200:
data = response.json()
# Add event_id to each attendee for easy merging later
for attendee in data.get('records', []):
attendee['event_id'] = event_id
attendees.append(attendee)
next_link = data.get('_links', {}).get('next', {}).get('href')
print (f"{next_link=}")
params = None # next links already include parameters
if next_link:
# IMPORTANT: Prepend the base URL to the relative path
URL = f"https://api.cc.email{next_link}"
else:
URL = None # Stop the loop
# Optional: rate limiting protection
time.sleep(0.5)
else:
print(f"Error fetching attendees for event {event_id}: {response.status_code}")
break
return attendees