-
Notifications
You must be signed in to change notification settings - Fork 81
feat: add pagination support to fetch all badge templates #2895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ac89a2f
7b98169
a1d644f
b8daf2d
8541d98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import base64 | ||
| import logging | ||
| import time | ||
| from functools import lru_cache | ||
| from urllib.parse import urljoin | ||
|
|
||
|
|
@@ -86,7 +87,40 @@ def fetch_badge_templates(self): | |
| """ | ||
| Fetches the badge templates from the Credly API. | ||
| """ | ||
| return self.perform_request("get", f"badge_templates/?filter=state::{CredlyBadgeTemplate.STATES.active}") | ||
| results = [] | ||
| url = f"badge_templates/?filter=state::{CredlyBadgeTemplate.STATES.active}" | ||
| response = self.perform_request("get", url) | ||
| results.extend(response.get("data", [])) | ||
|
|
||
| metadata = response.get("metadata", {}) | ||
| total_pages = metadata.get("total_pages", 1) | ||
| next_page_url = metadata.get("next_page_url") | ||
|
|
||
| # Loop through all remaining pages based on the total_pages value. | ||
| # For each iteration, fetch data using the 'next_page_url' provided by the API, | ||
| # append the results to the main list, and update the URL for the next request. | ||
| # The loop stops when there are no more pages to retrieve. | ||
| for _ in range(2, total_pages + 1): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to do them all at once like this? If there's enough to paginate, do you not want to paginate our results? This could be an iterator with
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback! I see your point, but in this case I don’t think we need to introduce a generator. The number of active Credly badge templates is usually small, so pagination is unlikely to produce a large dataset. Since the consumer only needs the full list, handling everything at once keeps the implementation simpler without adding unnecessary complexity. |
||
| if not next_page_url: | ||
| break | ||
|
|
||
| time.sleep(0.2) | ||
|
|
||
| for attempt in range(3): | ||
| try: | ||
| response = self.perform_request("get", next_page_url) | ||
| break | ||
| except (requests.Timeout, requests.ConnectionError) as exc: | ||
| sleep_time = 0.5 * (2**attempt) | ||
| time.sleep(sleep_time) | ||
|
|
||
| if attempt == 2: | ||
| raise CredlyError(f"Failed to fetch page due to network error: {exc}") | ||
|
|
||
| results.extend(response.get("data", [])) | ||
| next_page_url = response.get("metadata", {}).get("next_page_url") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback! I added the retry loop, so transient failures are handled, and if all attempts fail, the exception is still raised. |
||
|
|
||
| return {"data": results} | ||
|
|
||
| def fetch_event_information(self, event_id): | ||
| """ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few robustness questions:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback!