diff options
| author | Gauvain Pocentek <gauvain@pocentek.net> | 2016-02-18 18:17:06 +0000 |
|---|---|---|
| committer | Gauvain Pocentek <gauvain@pocentek.net> | 2016-02-18 18:19:48 +0000 |
| commit | 453224aaf64c37196b7211de8dd4a60061954987 (patch) | |
| tree | a77a85c92cc3f50b6ce05345d24fed5c8d76782d /gitlab | |
| parent | 44d0dc54fb7edf7de4e50ca34d430fe734c0e8cc (diff) | |
| download | gitlab-453224aaf64c37196b7211de8dd4a60061954987.tar.gz | |
Re-implement _custom_list in the Gitlab class
Rename the method _raw_list. This adds support for the ``all=True``
option to enable automatic recursion and avoid pagination if requested
by the user.
Fixes #93
Diffstat (limited to 'gitlab')
| -rw-r--r-- | gitlab/__init__.py | 25 | ||||
| -rw-r--r-- | gitlab/objects.py | 37 |
2 files changed, 42 insertions, 20 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py index a947084..ef39cd5 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -269,6 +269,31 @@ class Gitlab(object): raise GitlabConnectionError( "Can't connect to GitLab server (%s)" % e) + def _raw_list(self, path, cls, **kwargs): + r = self._raw_get(path, **kwargs) + raise_error_from_response(r, GitlabListError) + + cls_kwargs = kwargs.copy() + + # Add _from_api manually, because we are not creating objects + # through normal path + cls_kwargs['_from_api'] = True + get_all_results = kwargs.get('all', False) + + # Remove parameters from kwargs before passing it to constructor + for key in ['all', 'page', 'per_page', 'sudo']: + if key in cls_kwargs: + del cls_kwargs[key] + + results = [cls(self, item, **cls_kwargs) for item in r.json() + if item is not None] + if ('next' in r.links and 'url' in r.links['next'] + and get_all_results is True): + args = kwargs.copy() + args['next_url'] = r.links['next']['url'] + results.extend(self.list(cls, **args)) + return results + def _raw_post(self, path, data=None, content_type=None, **kwargs): url = '%s%s' % (self._url, path) headers = self._create_headers(content_type) diff --git a/gitlab/objects.py b/gitlab/objects.py index 8146816..c453daf 100644 --- a/gitlab/objects.py +++ b/gitlab/objects.py @@ -155,18 +155,6 @@ class BaseManager(object): raise NotImplementedError self.gitlab.delete(self.obj_cls, id, **args) - def _custom_list(self, url, cls, **kwargs): - r = self.gitlab._raw_get(url, **kwargs) - raise_error_from_response(r, GitlabListError) - - l = [] - for j in r.json(): - o = cls(self.gitlab, j) - o._from_api = True - l.append(o) - - return l - class GitlabObject(object): """Base class for all classes that interface with GitLab.""" @@ -569,6 +557,7 @@ class UserManager(BaseManager): Args: query (str): The query string to send to GitLab for the search. + all (bool): If True, return all the items, without pagination **kwargs: Additional arguments to send to GitLab. Returns: @@ -579,7 +568,7 @@ class UserManager(BaseManager): GitlabListError: If the server fails to perform the request. """ url = self.obj_cls._url + '?search=' + query - return self._custom_list(url, self.obj_cls, **kwargs) + return self.gitlab._raw_list(url, self.obj_cls, **kwargs) def get_by_username(self, username, **kwargs): """Get a user by its username. @@ -596,7 +585,7 @@ class UserManager(BaseManager): GitlabGetError: If the server fails to perform the request. """ url = self.obj_cls._url + '?username=' + username - results = self._custom_list(url, self.obj_cls, **kwargs) + results = self.gitlab._raw_list(url, self.obj_cls, **kwargs) assert len(results) in (0, 1) try: return results[0] @@ -712,10 +701,15 @@ class GroupManager(BaseManager): def search(self, query, **kwargs): """Searches groups by name. - Returns a list of matching groups. + Args: + query (str): The search string + all (bool): If True, return all the items, without pagination + + Returns: + list(Group): a list of matching groups. """ url = '/groups?search=' + query - return self._custom_list(url, Group, **kwargs) + return self.gitlab._raw_list(url, self.obj_cls, **kwargs) class Hook(GitlabObject): @@ -1524,35 +1518,38 @@ class ProjectManager(BaseManager): Args: query (str): The query string to send to GitLab for the search. + all (bool): If True, return all the items, without pagination **kwargs: Additional arguments to send to GitLab. Returns: list(Project): A list of matching projects. """ - return self._custom_list("/projects/search/" + query, Project, - **kwargs) + return self.gitlab._raw_list("/projects/search/" + query, Project, + **kwargs) def all(self, **kwargs): """List all the projects (need admin rights). Args: + all (bool): If True, return all the items, without pagination **kwargs: Additional arguments to send to GitLab. Returns: list(Project): The list of projects. """ - return self._custom_list("/projects/all", Project, **kwargs) + return self.gitlab._raw_list("/projects/all", Project, **kwargs) def owned(self, **kwargs): """List owned projects. Args: + all (bool): If True, return all the items, without pagination **kwargs: Additional arguments to send to GitLab. Returns: list(Project): The list of owned projects. """ - return self._custom_list("/projects/owned", Project, **kwargs) + return self.gitlab._raw_list("/projects/owned", Project, **kwargs) class UserProjectManager(BaseManager): |
