-
Notifications
You must be signed in to change notification settings - Fork 185
feat: bucket pagination #658
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
Merged
bednar
merged 5 commits into
influxdata:master
from
mbenabda:pr/add_paging_to_buckets_api
May 28, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
|
||
class _Page: | ||
def __init__(self, values, has_next, next_after): | ||
self.has_next = has_next | ||
self.values = values | ||
self.next_after = next_after | ||
|
||
@staticmethod | ||
def empty(): | ||
return _Page([], False, None) | ||
|
||
@staticmethod | ||
def initial(after): | ||
return _Page([], True, after) | ||
|
||
|
||
class _PageIterator: | ||
def __init__(self, page: _Page, get_next_page): | ||
self.page = page | ||
self.get_next_page = get_next_page | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
if not self.page.values: | ||
if self.page.has_next: | ||
self.page = self.get_next_page(self.page) | ||
if not self.page.values: | ||
raise StopIteration | ||
return self.page.values.pop(0) | ||
|
||
|
||
class _Paginated: | ||
def __init__(self, paginated_getter, pluck_page_resources_from_response): | ||
self.paginated_getter = paginated_getter | ||
self.pluck_page_resources_from_response = pluck_page_resources_from_response | ||
|
||
def find_iter(self, **kwargs): | ||
"""Iterate over resources with pagination. | ||
|
||
:key str org: The organization name. | ||
:key str org_id: The organization ID. | ||
:key str after: The last resource ID from which to seek from (but not including). | ||
:key int limit: the maximum number of items per page | ||
:return: resources iterator | ||
""" | ||
|
||
def get_next_page(page: _Page): | ||
return self._find_next_page(page, **kwargs) | ||
|
||
return iter(_PageIterator(_Page.initial(kwargs.get('after')), get_next_page)) | ||
|
||
def _find_next_page(self, page: _Page, **kwargs): | ||
if not page.has_next: | ||
return _Page.empty() | ||
|
||
kw_args = {**kwargs, 'after': page.next_after} if page.next_after is not None else kwargs | ||
response = self.paginated_getter(**kw_args) | ||
|
||
resources = self.pluck_page_resources_from_response(response) | ||
has_next = response.links.next is not None | ||
last_id = resources[-1].id if resources else None | ||
|
||
return _Page(resources, has_next, last_id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.