Skip to content

Commit ac89a2f

Browse files
committed
feat: add pagination support to fetch all badge templates
1 parent 0a2b1fb commit ac89a2f

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

credentials/apps/badges/credly/api_client.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,29 @@ def fetch_badge_templates(self):
8686
"""
8787
Fetches the badge templates from the Credly API.
8888
"""
89-
return self.perform_request("get", f"badge_templates/?filter=state::{CredlyBadgeTemplate.STATES.active}")
89+
results = []
90+
url = f"badge_templates/?filter=state::{CredlyBadgeTemplate.STATES.active}"
91+
response = self.perform_request("get", url)
92+
results.extend(response.get("data", []))
93+
94+
metadata = response.get("metadata", {})
95+
total_pages = metadata.get("total_pages", 1)
96+
next_page_url = metadata.get("next_page_url")
97+
98+
# Loop through all remaining pages based on the total_pages value.
99+
# For each iteration, fetch data using the 'next_page_url' provided by the API,
100+
# append the results to the main list, and update the URL for the next request.
101+
# The loop stops when there are no more pages to retrieve.
102+
for _ in range(2, total_pages + 1):
103+
if not next_page_url:
104+
break
105+
106+
response = self.perform_request("get", next_page_url)
107+
results.extend(response.get("data", []))
108+
109+
next_page_url = response.get("metadata", {}).get("next_page_url")
110+
111+
return {"data": results}
90112

91113
def fetch_event_information(self, event_id):
92114
"""

0 commit comments

Comments
 (0)