Skip to content

Commit

Permalink
Merge pull request #505 from jouve/config_per_page
Browse files Browse the repository at this point in the history
add per_page config option
  • Loading branch information
Gauvain Pocentek committed May 26, 2018
2 parents 97c8619 + 589a9aa commit d981904
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
7 changes: 5 additions & 2 deletions gitlab/__init__.py
Expand Up @@ -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
Expand All @@ -97,6 +97,8 @@ def __init__(self, url, private_token=None, oauth_token=None, email=None,
#: 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
Expand Down Expand Up @@ -177,7 +179,8 @@ def from_config(gitlab_id=None, config_files=None):
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.
Expand Down
10 changes: 10 additions & 0 deletions gitlab/config.py
Expand Up @@ -140,3 +140,13 @@ def __init__(self, gitlab_id=None, config_files=None):
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)
2 changes: 2 additions & 0 deletions gitlab/mixins.py
Expand Up @@ -114,6 +114,8 @@ def list(self, **kwargs):

# 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', {})
Expand Down
16 changes: 15 additions & 1 deletion gitlab/tests/test_config.py
Expand Up @@ -45,6 +45,7 @@
url = https://three.url
private_token = MNOPQR
ssl_verify = /path/to/CA/bundle.crt
per_page = 50
[four]
url = https://four.url
Expand All @@ -66,6 +67,11 @@
[three]
meh = hem
[four]
url = http://four.url
private_token = ABCDEF
per_page = 200
"""


Expand All @@ -87,13 +93,19 @@ def test_invalid_id(self, m_open):
@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):
Expand All @@ -108,6 +120,7 @@ def test_valid_data(self, m_open):
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)
Expand All @@ -130,6 +143,7 @@ def test_valid_data(self, m_open):
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)
Expand Down

0 comments on commit d981904

Please sign in to comment.