Commit f404d394 authored by Gauvain Pocentek's avatar Gauvain Pocentek
Browse files

[WIP] feat(api): Implement a count() method

parent 89679ce5
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -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.

+4 −0
Original line number Diff line number Diff line
@@ -70,6 +70,10 @@ class GitlabListError(GitlabOperationError):
    pass


class GitlabCountError(GitlabOperationError):
    pass


class GitlabGetError(GitlabOperationError):
    pass

+20 −0
Original line number Diff line number Diff line
@@ -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