diff options
author | Gauvain Pocentek <gauvain@pocentek.net> | 2019-01-14 07:16:43 +0100 |
---|---|---|
committer | Gauvain Pocentek <gauvain@pocentek.net> | 2019-01-14 07:30:03 +0100 |
commit | f404d394960683d68ccaf3c129f75a2582a2fb77 (patch) | |
tree | d1303b44b3291db3088c9ac978db7c8efc23d0ca | |
parent | 89679ce5ee502e57dbe7cec2b78f4f70b85fd3a5 (diff) | |
download | gitlab-feat/count.tar.gz |
[WIP] feat(api): Implement a count() methodfeat/count
-rw-r--r-- | gitlab/__init__.py | 22 | ||||
-rw-r--r-- | gitlab/exceptions.py | 4 | ||||
-rw-r--r-- | gitlab/mixins.py | 20 |
3 files changed, 46 insertions, 0 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py index c280974..d37fb11 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -548,6 +548,28 @@ class Gitlab(object): else: return result + def http_head(self, path, query_data={}, **kwargs): + """Make a HEAD request to the Gitlab server. + + Args: + path (str): Path or full URL to query ('/projects' or + 'http://whatever/v4/api/projecs') + query_data (dict): Data to send as query parameters + **kwargs: Extra options to send to the server (e.g. sudo, page, + per_page) + + Returns: + requests.structures.CaseInsensitiveDict: A requests.header object + + Raises: + GitlabHttpError: When the return code is not 2xx + """ + + url = self._build_url(path) + result = self.http_request('head', url, query_data=query_data, + **kwargs) + return result.headers + def http_list(self, path, query_data={}, as_list=None, **kwargs): """Make a GET request to the Gitlab server for list-oriented queries. diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index 0822d3e..317f243 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -70,6 +70,10 @@ class GitlabListError(GitlabOperationError): pass +class GitlabCountError(GitlabOperationError): + pass + + class GitlabGetError(GitlabOperationError): pass diff --git a/gitlab/mixins.py b/gitlab/mixins.py index ca68658..98c3185 100644 --- a/gitlab/mixins.py +++ b/gitlab/mixins.py @@ -136,6 +136,26 @@ class ListMixin(object): else: return base.RESTObjectList(self, self._obj_cls, obj) + @exc.on_http_error(exc.GitlabListError) + def count(self, **kwargs): + """Retrieve the number of available objects. + + Args: + **kwargs: Extra options to send to the server (e.g. sudo) + + Returns: + int: The number of objects + + Raises: + GitlabAuthenticationError: If authentication is not correct + GitlabCountError: If the server cannot perform the request + """ + # Allow to overwrite the path, handy for custom calls + path = kwargs.pop('path', self.path) + + headers = self.gitlab.http_head(path, **kwargs) + return int(headers['x-total']) + class RetrieveMixin(ListMixin, GetMixin): pass |