示例#1
0
 def __init__(
     self,
     manager: "RESTManager",
     attrs: Dict[str, Any],
     *,
     created_from_list: bool = False,
 ) -> None:
     if not isinstance(attrs, dict):
         raise GitlabParsingError(
             "Attempted to initialize RESTObject with a non-dictionary value: "
             "{!r}\nThis likely indicates an incorrect or malformed server "
             "response.".format(attrs))
     self.__dict__.update({
         "manager":
         manager,
         "_attrs":
         attrs,
         "_updated_attrs": {},
         "_module":
         importlib.import_module(self.__module__),
         "_created_from_list":
         created_from_list,
     })
     self.__dict__["_parent_attrs"] = self.manager.parent_attrs
     self._create_managers()
示例#2
0
    def http_put(self,
                 path,
                 query_data=None,
                 post_data=None,
                 files=None,
                 **kwargs):
        query_data = query_data or {}
        post_data = post_data or {}

        result = self.http_request("put",
                                   path,
                                   query_data=query_data,
                                   post_data=post_data,
                                   files=files,
                                   **kwargs)
        try:
            return result.json()
        except Exception:
            raise GitlabParsingError(
                error_message="Failed to parse the server message")
示例#3
0
    async def http_get(
        self, path, query_data=None, streamed=False, raw=False, **kwargs
    ):
        """Make a GET 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
            streamed (bool): Whether the data should be streamed
            raw (bool): If True do not try to parse the output as json
            **kwargs: Extra options to send to the server (e.g. sudo)

        Returns:
            A requests result object is streamed is True or the content type is
            not json.
            The parsed json data otherwise.

        Raises:
            GitlabHttpError: When the return code is not 2xx
            GitlabParsingError: If the json data could not be parsed
        """
        query_data = query_data or {}
        result = await self.http_request(
            "get", path, query_data=query_data, streamed=streamed, **kwargs
        )

        if (
            result.headers["Content-Type"] == "application/json"
            and not streamed
            and not raw
        ):
            try:
                return result.json()
            except Exception:
                raise GitlabParsingError(
                    error_message="Failed to parse the server message"
                )
        else:
            return result
示例#4
0
    async def http_post(self,
                        path,
                        query_data=None,
                        post_data=None,
                        files=None,
                        **kwargs):
        query_data = query_data or {}
        post_data = post_data or {}

        result = await self.http_request("post",
                                         path,
                                         query_data=query_data,
                                         post_data=post_data,
                                         files=files,
                                         **kwargs)
        try:
            if result.headers.get("Content-Type", None) == "application/json":
                return result.json()
        except Exception:
            raise GitlabParsingError(
                error_message="Failed to parse the server message")
        return result
示例#5
0
    async def http_get(self,
                       path,
                       query_data=None,
                       streamed=False,
                       raw=False,
                       **kwargs):
        query_data = query_data or {}
        result = await self.http_request("get",
                                         path,
                                         query_data=query_data,
                                         streamed=streamed,
                                         **kwargs)

        if (result.headers["Content-Type"] == "application/json"
                and not streamed and not raw):
            try:
                return result.json()
            except Exception:
                raise GitlabParsingError(
                    error_message="Failed to parse the server message")
        else:
            return result
示例#6
0
    async def _query(self, url, query_data=None, **kwargs):
        query_data = query_data or {}
        result = await self._gl.http_request(
            "get", url, query_data=query_data, **kwargs
        )
        try:
            self._next_url = result.links["next"]["url"]
        except KeyError:
            self._next_url = None
        self._current_page = result.headers.get("X-Page")
        self._prev_page = result.headers.get("X-Prev-Page")
        self._next_page = result.headers.get("X-Next-Page")
        self._per_page = result.headers.get("X-Per-Page")
        self._total_pages = result.headers.get("X-Total-Pages")
        self._total = result.headers.get("X-Total")

        try:
            self._data = result.json()
        except Exception:
            raise GitlabParsingError(error_message="Failed to parse the server message")

        self._current = 0
示例#7
0
    async def http_post(
        self, path, query_data=None, post_data=None, files=None, **kwargs
    ):
        """Make a POST 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
            post_data (dict): Data to send in the body (will be converted to
                              json)
            files (dict): The files to send to the server
            **kwargs: Extra options to send to the server (e.g. sudo)

        Returns:
            The parsed json returned by the server if json is return, else the
            raw content

        Raises:
            GitlabHttpError: When the return code is not 2xx
            GitlabParsingError: If the json data could not be parsed
        """
        query_data = query_data or {}
        post_data = post_data or {}

        result = await self.http_request(
            "post",
            path,
            query_data=query_data,
            post_data=post_data,
            files=files,
            **kwargs
        )
        try:
            if result.headers.get("Content-Type", None) == "application/json":
                return result.json()
        except Exception:
            raise GitlabParsingError(error_message="Failed to parse the server message")
        return result