8000 [WIP] feat(api): Implement a count() method · dibcon/python-gitlab@f404d39 · GitHub
[go: up one dir, main page]

Skip to content

Commit f404d39

Browse files
author
Gauvain Pocentek
committed
[WIP] feat(api): Implement a count() method
1 parent 89679ce commit f404d39

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

gitlab/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,28 @@ def http_get(self, path, query_data={}, streamed=False, **kwargs):
548548
else:
549549
return result
550550

551+
def http_head(self, path, query_data={}, **kwargs):
552+
"""Make a HEAD request to the Gitlab server.
553+
554+
Args:
555+
path (str): Path or full URL to query ('/projects' or
556+
'http://whatever/v4/api/projecs')
557+
query_data (dict): Data to send as query parameters
558+
**kwargs: Extra options to send to the server (e.g. sudo, page,
559+
per_page)
560+
561+
Returns:
562+
requests.structures.CaseInsensitiveDict: A requests.header object
563+
564+
Raises:
565+
GitlabHttpError: When the return code is not 2xx
566+
"""
567+
568+
url = self._build_url(path)
569+
result = self.http_request('head', url, query_data=query_data,
570+
**kwargs)
571+
return result.headers
572+
551573
def http_list(self, path, query_data={}, as_list=None, **kwargs):
552574
"""Make a GET request to the Gitlab server for list-oriented queries.
553575

gitlab/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class GitlabListError(GitlabOperationError):
7070
pass
7171

7272

73+
class GitlabCountError(GitlabOperationError):
74+
pass
75+
76+
7377
class GitlabGetError(GitlabOperationError):
7478
pass
7579

gitlab/mixins.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,26 @@ def list(self, **kwargs):
136136
else:
137137
return base.RESTObjectList(self, self._obj_cls, obj)
138138

139+
@exc.on_http_error(exc.GitlabListError)
140+
def count(self, **kwargs):
141+
"""Retrieve the number of available objects.
142+
143+
Args:
144+
**kwargs: Extra options to send to the server (e.g. sudo)
145+
146+
Returns:
147+
int: The number of objects
148+
149+
Raises:
150+
GitlabAuthenticationError: If authentication is not correct
151+
GitlabCountError: If the server cannot perform the request
152+
"""
153+
# Allow to overwrite the path, handy for custom calls
154+
path = kwargs.pop('path', self.path)
155+
156+
headers = self.gitlab.http_head(path, **kwargs)
157+
return int(headers['x-total'])
158+
139159

140160
class RetrieveMixin(ListMixin, GetMixin):
141161
pass

0 commit comments

Comments
 (0)
0