Skip to content

Commit 0b71ba4

Browse files
committed
feat: support keyset pagination globally
1 parent bded2de commit 0b71ba4

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

docs/api-usage.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@ You can define the ``per_page`` value globally to avoid passing it to every
219219
220220
gl = gitlab.Gitlab(url, token, per_page=50)
221221
222+
Gitlab allows to also use keyset pagination. You can supply it to your project listing,
223+
but you can also do so globally. Be aware that GitLab then also requires you to only use supported
224+
order options. At the time of writing, only ``order_by="id"`` works.
225+
226+
.. code-block:: python
227+
228+
gl = gitlab.Gitlab(url, token, pagination="keyset", per_page=100)
229+
gl.projects.list(order_by="id")
230+
231+
Reference:
232+
https://docs.gitlab.com/ce/api/README.html#keyset-based-pagination
233+
222234
``list()`` methods can also return a generator object which will handle the
223235
next calls to the API when required. This is the recommended way to iterate
224236
through a large number of items:

gitlab/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class Gitlab(object):
6969
http_username (str): Username for HTTP authentication
7070
http_password (str): Password for HTTP authentication
7171
api_version (str): Gitlab API version to use (support for 4 only)
72+
pagination (str): Can be set to 'keyset' to use keyset pagination
7273
"""
7374

7475
def __init__(
@@ -84,6 +85,7 @@ def __init__(
8485
api_version="4",
8586
session=None,
8687
per_page=None,
88+
pagination=None,
8789
):
8890

8991
self._api_version = str(api_version)
@@ -109,6 +111,7 @@ def __init__(
109111
self.session = session or requests.Session()
110112

111113
self.per_page = per_page
114+
self.pagination = pagination
112115

113116
objects = importlib.import_module("gitlab.v%s.objects" % self._api_version)
114117
self._objects = objects
@@ -200,6 +203,7 @@ def from_config(cls, gitlab_id=None, config_files=None):
200203
http_password=config.http_password,
201204
api_version=config.api_version,
202205
per_page=config.per_page,
206+
pagination=config.pagination,
203207
)
204208

205209
def auth(self):

gitlab/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,9 @@ def __init__(self, gitlab_id=None, config_files=None):
163163
pass
164164
if self.per_page is not None and not 0 <= self.per_page <= 100:
165165
raise GitlabDataError("Unsupported per_page number: %s" % self.per_page)
166+
167+
self.pagination = None
168+
try:
169+
self.pagination = self._config.get(self.gitlab_id, "pagination")
170+
except Exception:
171+
pass

gitlab/mixins.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def list(self, **kwargs):
120120
if self.gitlab.per_page:
121121
data.setdefault("per_page", self.gitlab.per_page)
122122

123+
# global keyset pagination
124+
if self.gitlab.pagination:
125+
data.setdefault("pagination", self.gitlab.pagination)
126+
123127
# We get the attributes that need some special transformation
124128
types = getattr(self, "_types", {})
125129
if types:

0 commit comments

Comments
 (0)