summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gitlab/__init__.py22
-rw-r--r--gitlab/exceptions.py4
-rw-r--r--gitlab/mixins.py20
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