Skip to content

Commit 589a9aa

Browse files
committed
add per_page config option
1 parent 97c8619 commit 589a9aa

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

gitlab/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Gitlab(object):
7070
def __init__(self, url, private_token=None, oauth_token=None, email=None,
7171
password=None, ssl_verify=True, http_username=None,
7272
http_password=None, timeout=None, api_version='4',
73-
session=None):
73+
session=None, per_page=None):
7474

7575
self._api_version = str(api_version)
7676
self._server_version = self._server_revision = None
@@ -97,6 +97,8 @@ def __init__(self, url, private_token=None, oauth_token=None, email=None,
9797
#: Create a session object for requests
9898
self.session = session or requests.Session()
9999

100+
self.per_page = per_page
101+
100102
objects = importlib.import_module('gitlab.v%s.objects' %
101103
self._api_version)
102104
self._objects = objects
@@ -177,7 +179,8 @@ def from_config(gitlab_id=None, config_files=None):
177179
ssl_verify=config.ssl_verify, timeout=config.timeout,
178180
http_username=config.http_username,
179181
http_password=config.http_password,
180-
api_version=config.api_version)
182+
api_version=config.api_version,
183+
per_page=config.per_page)
181184

182185
def auth(self):
183186
"""Performs an authentication.

gitlab/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,13 @@ def __init__(self, gitlab_id=None, config_files=None):
140140
if self.api_version not in ('4',):
141141
raise GitlabDataError("Unsupported API version: %s" %
142142
self.api_version)
143+
144+
self.per_page = None
145+
for section in ['global', self.gitlab_id]:
146+
try:
147+
self.per_page = self._config.getint(section, 'per_page')
148+
except Exception:
149+
pass
150+
if self.per_page is not None and not 0 <= self.per_page <= 100:
151+
raise GitlabDataError("Unsupported per_page number: %s" %
152+
self.per_page)

gitlab/mixins.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def list(self, **kwargs):
114114

115115
# Duplicate data to avoid messing with what the user sent us
116116
data = kwargs.copy()
117+
if self.gitlab.per_page:
118+
data.setdefault('per_page', self.gitlab.per_page)
117119

118120
# We get the attributes that need some special transformation
119121
types = getattr(self, '_types', {})

gitlab/tests/test_config.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
url = https://three.url
4646
private_token = MNOPQR
4747
ssl_verify = /path/to/CA/bundle.crt
48+
per_page = 50
4849
4950
[four]
5051
url = https://four.url
@@ -66,6 +67,11 @@
6667
6768
[three]
6869
meh = hem
70+
71+
[four]
72+
url = http://four.url
73+
private_token = ABCDEF
74+
per_page = 200
6975
"""
7076

7177

@@ -87,13 +93,19 @@ def test_invalid_id(self, m_open):
8793
@mock.patch('six.moves.builtins.open')
8894
def test_invalid_data(self, m_open):
8995
fd = six.StringIO(missing_attr_config)
90-
fd.close = mock.Mock(return_value=None)
96+
fd.close = mock.Mock(return_value=None,
97+
side_effect=lambda: fd.seek(0))
9198
m_open.return_value = fd
9299
config.GitlabConfigParser('one')
100+
config.GitlabConfigParser('one')
93101
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
94102
gitlab_id='two')
95103
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
96104
gitlab_id='three')
105+
with self.assertRaises(config.GitlabDataError) as emgr:
106+
config.GitlabConfigParser('four')
107+
self.assertEqual('Unsupported per_page number: 200',
108+
emgr.exception.args[0])
97109

98110
@mock.patch('six.moves.builtins.open')
99111
def test_valid_data(self, m_open):
@@ -108,6 +120,7 @@ def test_valid_data(self, m_open):
108120
self.assertEqual(None, cp.oauth_token)
109121
self.assertEqual(2, cp.timeout)
110122
self.assertEqual(True, cp.ssl_verify)
123+
self.assertIsNone(cp.per_page)
111124

112125
fd = six.StringIO(valid_config)
113126
fd.close = mock.Mock(return_value=None)
@@ -130,6 +143,7 @@ def test_valid_data(self, m_open):
130143
self.assertEqual(None, cp.oauth_token)
131144
self.assertEqual(2, cp.timeout)
132145
self.assertEqual("/path/to/CA/bundle.crt", cp.ssl_verify)
146+
self.assertEqual(50, cp.per_page)
133147

134148
fd = six.StringIO(valid_config)
135149
fd.close = mock.Mock(return_value=None)

0 commit comments

Comments
 (0)