Skip to content

Commit 7477b4d

Browse files
committed
api/rest: Handle patchwork pagination
If there are many patches (or other objects) to be listed, patchwork only returns the first 30 by default. In such a case continue fetching objects using the "next" link provided in the Link: header. Note this changes REST._list() to return a generator instead of a list. All callers only use it to generate a list, so this isn't a problem.
1 parent a4034c3 commit 7477b4d

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

pwclient/api.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from . import __version__
2020
from .xmlrpc import xmlrpclib
2121

22+
from requests.utils import parse_header_links
23+
2224

2325
class API(metaclass=abc.ABCMeta):
2426
@abc.abstractmethod
@@ -594,8 +596,19 @@ def _list(
594596
url = f'{url}{resource_id}/{subresource_type}/'
595597
if params:
596598
url = f'{url}?{urllib.parse.urlencode(params)}'
597-
data, _ = self._get(url)
598-
return json.loads(data)
599+
600+
while url:
601+
data, headers = self._get(url)
602+
ret = json.loads(data)
603+
604+
yield from ret
605+
606+
try:
607+
link = next(value for name, value in headers if name == 'Link')
608+
url = next(a['url'] for a in parse_header_links(link) if a['rel'] == "next")
609+
610+
except StopIteration:
611+
url = None
599612

600613
# project
601614

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

0 commit comments

Comments
 (0)