summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCyril Jouve <jv.cyril@gmail.com>2018-05-22 00:46:17 +0200
committerCyril Jouve <jv.cyril@gmail.com>2018-05-22 02:39:46 +0200
commit589a9aad58383b98b5321db106e77afa0a9a761b (patch)
treeb1bc0ff8cacc33905b4e4ffc48ec5612465866bf
parent97c8619c5b07abc714417d6e5be2f553270b54a6 (diff)
downloadgitlab-589a9aad58383b98b5321db106e77afa0a9a761b.tar.gz
add per_page config option
-rw-r--r--gitlab/__init__.py7
-rw-r--r--gitlab/config.py10
-rw-r--r--gitlab/mixins.py2
-rw-r--r--gitlab/tests/test_config.py16
4 files changed, 32 insertions, 3 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
index c0562da..134cada 100644
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -70,7 +70,7 @@ class Gitlab(object):
def __init__(self, url, private_token=None, oauth_token=None, email=None,
password=None, ssl_verify=True, http_username=None,
http_password=None, timeout=None, api_version='4',
- session=None):
+ session=None, per_page=None):
self._api_version = str(api_version)
self._server_version = self._server_revision = None
@@ -97,6 +97,8 @@ class Gitlab(object):
#: Create a session object for requests
self.session = session or requests.Session()
+ self.per_page = per_page
+
objects = importlib.import_module('gitlab.v%s.objects' %
self._api_version)
self._objects = objects
@@ -177,7 +179,8 @@ class Gitlab(object):
ssl_verify=config.ssl_verify, timeout=config.timeout,
http_username=config.http_username,
http_password=config.http_password,
- api_version=config.api_version)
+ api_version=config.api_version,
+ per_page=config.per_page)
def auth(self):
"""Performs an authentication.
diff --git a/gitlab/config.py b/gitlab/config.py
index c3fcf70..9f4c11d 100644
--- a/gitlab/config.py
+++ b/gitlab/config.py
@@ -140,3 +140,13 @@ class GitlabConfigParser(object):
if self.api_version not in ('4',):
raise GitlabDataError("Unsupported API version: %s" %
self.api_version)
+
+ self.per_page = None
+ for section in ['global', self.gitlab_id]:
+ try:
+ self.per_page = self._config.getint(section, 'per_page')
+ except Exception:
+ pass
+ if self.per_page is not None and not 0 <= self.per_page <= 100:
+ raise GitlabDataError("Unsupported per_page number: %s" %
+ self.per_page)
diff --git a/gitlab/mixins.py b/gitlab/mixins.py
index 581e3d5..013f7b7 100644
--- a/gitlab/mixins.py
+++ b/gitlab/mixins.py
@@ -114,6 +114,8 @@ class ListMixin(object):
# Duplicate data to avoid messing with what the user sent us
data = kwargs.copy()
+ if self.gitlab.per_page:
+ data.setdefault('per_page', self.gitlab.per_page)
# We get the attributes that need some special transformation
types = getattr(self, '_types', {})
diff --git a/gitlab/tests/test_config.py b/gitlab/tests/test_config.py
index 271fa0b..0b585e8 100644
--- a/gitlab/tests/test_config.py
+++ b/gitlab/tests/test_config.py
@@ -45,6 +45,7 @@ timeout = 10
url = https://three.url
private_token = MNOPQR
ssl_verify = /path/to/CA/bundle.crt
+per_page = 50
[four]
url = https://four.url
@@ -66,6 +67,11 @@ private_token = ABCDEF
[three]
meh = hem
+
+[four]
+url = http://four.url
+private_token = ABCDEF
+per_page = 200
"""
@@ -87,13 +93,19 @@ class TestConfigParser(unittest.TestCase):
@mock.patch('six.moves.builtins.open')
def test_invalid_data(self, m_open):
fd = six.StringIO(missing_attr_config)
- fd.close = mock.Mock(return_value=None)
+ fd.close = mock.Mock(return_value=None,
+ side_effect=lambda: fd.seek(0))
m_open.return_value = fd
config.GitlabConfigParser('one')
+ config.GitlabConfigParser('one')
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
gitlab_id='two')
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
gitlab_id='three')
+ with self.assertRaises(config.GitlabDataError) as emgr:
+ config.GitlabConfigParser('four')
+ self.assertEqual('Unsupported per_page number: 200',
+ emgr.exception.args[0])
@mock.patch('six.moves.builtins.open')
def test_valid_data(self, m_open):
@@ -108,6 +120,7 @@ class TestConfigParser(unittest.TestCase):
self.assertEqual(None, cp.oauth_token)
self.assertEqual(2, cp.timeout)
self.assertEqual(True, cp.ssl_verify)
+ self.assertIsNone(cp.per_page)
fd = six.StringIO(valid_config)
fd.close = mock.Mock(return_value=None)
@@ -130,6 +143,7 @@ class TestConfigParser(unittest.TestCase):
self.assertEqual(None, cp.oauth_token)
self.assertEqual(2, cp.timeout)
self.assertEqual("/path/to/CA/bundle.crt", cp.ssl_verify)
+ self.assertEqual(50, cp.per_page)
fd = six.StringIO(valid_config)
fd.close = mock.Mock(return_value=None)