-
Notifications
You must be signed in to change notification settings - Fork 22
add pagination support to list #26
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
Open
jacob-keller
wants to merge
1
commit into
getpatchwork:main
Choose a base branch
from
jacob-keller:add-pagination-support-to-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -581,6 +581,28 @@ def _detail( | |
data, _ = self._get(url) | ||
return json.loads(data) | ||
|
||
@staticmethod | ||
def _get_next_page(headers): | ||
link_header = next((data for header, data in headers if header == 'Link'), None) | ||
if link_header is None: | ||
return None | ||
|
||
rel = '; rel="next"' | ||
|
||
url = next((l[:-len(rel)] for l in link_header.split(',') if l.endswith(rel)), None) | ||
if url is None: | ||
return None; | ||
|
||
if not (url.startswith('<') and url.endswith('>')): | ||
return None; | ||
|
||
parsed_link = urllib.parse.urlparse(url[1:-1]) | ||
page = next((x for x in parsed_link.query.split('&') if x.startswith('page=')), None) | ||
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. You make an effort here to extract the page number just to reconstruct |
||
if page is None: | ||
return None | ||
|
||
return int(page[5:]) | ||
|
||
def _list( | ||
self, | ||
resource_type, | ||
|
@@ -594,8 +616,23 @@ def _list( | |
url = f'{url}{resource_id}/{subresource_type}/' | ||
if params: | ||
url = f'{url}?{urllib.parse.urlencode(params)}' | ||
data, _ = self._get(url) | ||
return json.loads(data) | ||
data, headers = self._get(url) | ||
|
||
items = json.loads(data) | ||
|
||
page_nr = self._get_next_page(headers) | ||
if page_nr is None: | ||
return items | ||
|
||
if params is None: | ||
params = {} | ||
params['page'] = page_nr | ||
|
||
items += self._list(resource_type, params, | ||
resource_id=resource_id, | ||
subresource_type=subresource_type) | ||
|
||
return items | ||
|
||
# project | ||
|
||
|
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.
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.
According to MDN's documentation about the Link header this is overspecific. There might be other parameters than
rel
andnext
might also be unquoted. I looked up a few implementations for parsing the Link header and found several different ones. All in all this isn't trivial to implement if you also want it to be somewhat robust. My position here would be to better use the implementation from the requests package than to add another imperfect implementation.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.
That was my original thought. However, it was pointed out that we really only have to deal with the link header implementation from patchwork itself.