Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f404d39

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

File tree

Expand file treeCollapse file tree

3 files changed

+46
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+46
-0
lines changed
Open diff view settings
Collapse file

‎gitlab/__init__.py‎

Copy file name to clipboardExpand all lines: gitlab/__init__.py
+22Lines 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
Collapse file

‎gitlab/exceptions.py‎

Copy file name to clipboardExpand all lines: gitlab/exceptions.py
+4Lines 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

Collapse file

‎gitlab/mixins.py‎

Copy file name to clipboardExpand all lines: gitlab/mixins.py
+20Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.