summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Villalovos <john@sodarock.com>2023-02-05 13:10:55 -0800
committerGitHub <noreply@github.com>2023-02-05 22:10:55 +0100
commitf711d9e2bf78f58cee6a7c5893d4acfd2f980397 (patch)
treef3e693cecb8526f09c3eec65a53b3bf0b513672c
parent08675643e6b306d3ae101b173609a6c363c9f3df (diff)
downloadgitlab-f711d9e2bf78f58cee6a7c5893d4acfd2f980397.tar.gz
feat(client): add http_patch method (#2471)
In order to support some new API calls we need to support the HTTP `PATCH` method. Closes: #2469
-rw-r--r--docs/api-levels.rst1
-rw-r--r--gitlab/client.py48
-rw-r--r--pyproject.toml1
-rw-r--r--tests/unit/test_gitlab_http_methods.py50
4 files changed, 100 insertions, 0 deletions
diff --git a/docs/api-levels.rst b/docs/api-levels.rst
index 5a52b78..2fdc42d 100644
--- a/docs/api-levels.rst
+++ b/docs/api-levels.rst
@@ -60,6 +60,7 @@ The available methods are:
* ``http_get()``
* ``http_post()``
* ``http_put()``
+* ``http_patch()``
* ``http_delete()``
* ``http_list()`` (a wrapper around ``http_get`` handling pagination, including with lazy generators)
* ``http_head()`` (only returns the header dictionary)
diff --git a/gitlab/client.py b/gitlab/client.py
index 5e6c71a..c3982f3 100644
--- a/gitlab/client.py
+++ b/gitlab/client.py
@@ -1094,6 +1094,54 @@ class Gitlab:
error_message="Failed to parse the server message"
) from e
+ def http_patch(
+ self,
+ path: str,
+ *,
+ query_data: Optional[Dict[str, Any]] = None,
+ post_data: Optional[Union[Dict[str, Any], bytes]] = None,
+ raw: bool = False,
+ **kwargs: Any,
+ ) -> Union[Dict[str, Any], requests.Response]:
+ """Make a PATCH request to the Gitlab server.
+
+ Args:
+ path: Path or full URL to query ('/projects' or
+ 'http://whatever/v4/api/projecs')
+ query_data: Data to send as query parameters
+ post_data: Data to send in the body (will be converted to
+ json by default)
+ raw: If True, do not convert post_data to json
+ **kwargs: Extra options to send to the server (e.g. sudo)
+
+ Returns:
+ The parsed json returned by the server.
+
+ 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 = self.http_request(
+ "patch",
+ path,
+ query_data=query_data,
+ post_data=post_data,
+ raw=raw,
+ **kwargs,
+ )
+ try:
+ json_result = result.json()
+ if TYPE_CHECKING:
+ assert isinstance(json_result, dict)
+ return json_result
+ except Exception as e:
+ raise gitlab.exceptions.GitlabParsingError(
+ error_message="Failed to parse the server message"
+ ) from e
+
def http_delete(self, path: str, **kwargs: Any) -> requests.Response:
"""Make a DELETE request to the Gitlab server.
diff --git a/pyproject.toml b/pyproject.toml
index 7441130..75b8c1d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -58,6 +58,7 @@ disable = [
"too-many-instance-attributes",
"too-many-lines",
"too-many-locals",
+ "too-many-public-methods",
"too-many-statements",
"unsubscriptable-object",
]
diff --git a/tests/unit/test_gitlab_http_methods.py b/tests/unit/test_gitlab_http_methods.py
index 569df81..d7e375a 100644
--- a/tests/unit/test_gitlab_http_methods.py
+++ b/tests/unit/test_gitlab_http_methods.py
@@ -810,6 +810,56 @@ def test_put_request_invalid_data(gl):
@responses.activate
+def test_patch_request(gl):
+ url = "http://localhost/api/v4/projects"
+ responses.add(
+ method=responses.PATCH,
+ url=url,
+ json={"name": "project1"},
+ status=200,
+ match=helpers.MATCH_EMPTY_QUERY_PARAMS,
+ )
+
+ result = gl.http_patch("/projects")
+ assert isinstance(result, dict)
+ assert result["name"] == "project1"
+ assert responses.assert_call_count(url, 1) is True
+
+
+@responses.activate
+def test_patch_request_404(gl):
+ url = "http://localhost/api/v4/not_there"
+ responses.add(
+ method=responses.PATCH,
+ url=url,
+ json=[],
+ status=404,
+ match=helpers.MATCH_EMPTY_QUERY_PARAMS,
+ )
+
+ with pytest.raises(GitlabHttpError):
+ gl.http_patch("/not_there")
+ assert responses.assert_call_count(url, 1) is True
+
+
+@responses.activate
+def test_patch_request_invalid_data(gl):
+ url = "http://localhost/api/v4/projects"
+ responses.add(
+ method=responses.PATCH,
+ url=url,
+ body='["name": "project1"]',
+ content_type="application/json",
+ status=200,
+ match=helpers.MATCH_EMPTY_QUERY_PARAMS,
+ )
+
+ with pytest.raises(GitlabParsingError):
+ gl.http_patch("/projects")
+ assert responses.assert_call_count(url, 1) is True
+
+
+@responses.activate
def test_delete_request(gl):
url = "http://localhost/api/v4/projects"
responses.add(