Skip to content

feat(client): add http_patch method #2471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api-levels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions gitlab/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,54 @@ def http_put(
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.

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ disable = [
"too-many-instance-attributes",
"too-many-lines",
"too-many-locals",
"too-many-public-methods",
"too-many-statements",
"unsubscriptable-object",
]
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/test_gitlab_http_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,56 @@ def test_put_request_invalid_data(gl):
assert responses.assert_call_count(url, 1) is True


@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"
Expand Down