Skip to content

Commit 245969c

Browse files
committed
Merge pull request pyapi-gitlab#121 from aneroid/getall_func
Add a convenience method to Get All results from paginated calls
2 parents 311e17f + 7472dc7 commit 245969c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

gitlab/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,3 +1854,26 @@ def editlabel(self, project_id, name, new_name=None, color=None):
18541854
return json.loads(request.content.decode("utf-8"))
18551855
else:
18561856
return False
1857+
1858+
@staticmethod
1859+
def getall(fn, *args, **kwargs):
1860+
"""Auto-iterate over the paginated results of various methods of the API.
1861+
Pass the GitLabAPI method as the first argument, followed by the
1862+
other parameters as normal. Include `page` to determine first page to poll.
1863+
Remaining kwargs are passed on to the called method, including `per_page`.
1864+
1865+
:param fn: Actual method to call
1866+
:param *args: Positional arguments to actual method
1867+
:param page: Optional, page number to start at, defaults to 1
1868+
:param **kwargs: Keyword arguments to actual method
1869+
:return: Yields each item in the result until exhausted, and then
1870+
implicit StopIteration; or no elements if error
1871+
"""
1872+
page = kwargs.pop('page', 1)
1873+
while True:
1874+
results = fn(*args, page=page, **kwargs)
1875+
if not results:
1876+
break
1877+
for x in results:
1878+
yield x
1879+
page += 1

0 commit comments

Comments
 (0)