Skip to content

Properly handle pagination in the REST backend #39

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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions pwclient/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from . import __version__
from .xmlrpc import xmlrpclib

from requests.utils import parse_header_links


class API(metaclass=abc.ABCMeta):
@abc.abstractmethod
Expand Down Expand Up @@ -594,8 +596,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)

while url:
data, headers = self._get(url)
ret = json.loads(data)

yield from ret

try:
link = next(value for name, value in headers if name == 'Link')
url = next(
a['url']
for a in parse_header_links(link)
if a['rel'] == "next"
)

except StopIteration:
url = None

# project

Expand Down Expand Up @@ -627,7 +644,7 @@ def project_list(self, search_str=None, max_count=0):
)

projects = self._list('projects')
return [self._project_to_dict(project) for project in projects]
return (self._project_to_dict(project) for project in projects)

def project_get(self, project_id):
project = self._detail('projects', project_id)
Expand Down Expand Up @@ -662,7 +679,7 @@ def person_list(self, search_str=None, max_count=0):
)

people = self._list('people')
return [self._person_to_dict(person) for person in people]
return (self._person_to_dict(person) for person in people)

def person_get(self, person_id):
person = self._detail('people', person_id)
Expand Down Expand Up @@ -747,11 +764,14 @@ def patch_list(
if archived is not None:
filters['archived'] = archived

if submitter is not None:
filters['submitter'] = submitter

if delegate is not None:
filters['delegate'] = delegate

patches = self._list('patches', params=filters)
return [self._patch_to_dict(patch) for patch in patches]
return (self._patch_to_dict(patch) for patch in patches)

def patch_get(self, patch_id):
patch = self._detail('patches', patch_id)
Expand Down
15 changes: 9 additions & 6 deletions pwclient/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import io
import itertools
import operator
import os
import re
import subprocess
Expand Down Expand Up @@ -56,6 +57,10 @@ def patch_field(matchobj):
)


def sortgroupby(iterable, key=None):
return itertools.groupby(sorted(iterable, key=key), key=key)


def action_list(
api,
project=None,
Expand Down Expand Up @@ -89,10 +94,9 @@ def action_list(
filters['submitter'] = submitter

patches = api.patch_list(**filters)
patches.sort(key=lambda x: x['submitter'])

for person, person_patches in itertools.groupby(
patches, key=lambda x: x['submitter']
for person, person_patches in sortgroupby(
patches, key=operator.itemgetter('submitter')
):
print(f'Patches submitted by {person}:')
_list_patches(list(person_patches), format_str)
Expand All @@ -103,10 +107,9 @@ def action_list(
filters['delegate'] = delegate

patches = api.patch_list(**filters)
patches.sort(key=lambda x: x['delegate'])

for delegate, delegate_patches in itertools.groupby(
patches, key=lambda x: x['delegate']
for delegate, delegate_patches in sortgroupby(
patches, key=operator.itemgetter('delegate')
):
print(f'Patches delegated to {delegate}:')
_list_patches(list(delegate_patches), format_str)
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
types-requests