| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b0f02fa commit ce9216c
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,6 +82,10 @@ class GitlabGetError(GitlabOperationError): | |||
| 82 | 82 | pass | |
| 83 | 83 | ||
| 84 | 84 | ||
| 85 | + class GitlabHeadError(GitlabOperationError): | ||
| 86 | + pass | ||
| 87 | + | ||
| 88 | + | ||
| 85 | 89 | class GitlabCreateError(GitlabOperationError): | |
| 86 | 90 | pass | |
| 87 | 91 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -68,7 +68,35 @@ | |||
| 68 | 68 | _RestObjectBase = object | |
| 69 | 69 | ||
| 70 | 70 | ||
| 71 | - class GetMixin(_RestManagerBase): | ||
| 71 | + class HeadMixin(_RestManagerBase): | ||
| 72 | + @exc.on_http_error(exc.GitlabHeadError) | ||
| 73 | + def head( | ||
| 74 | + self, id: Optional[Union[str, int]] = None, **kwargs: Any | ||
| 75 | + ) -> requests.structures.CaseInsensitiveDict: | ||
| 76 | + """Retrieve headers from an endpoint. | ||
| 77 | + | ||
| 78 | + Args: | ||
| 79 | + id: ID of the object to retrieve | ||
| 80 | + **kwargs: Extra options to send to the server (e.g. sudo) | ||
| 81 | + | ||
| 82 | + Returns: | ||
| 83 | + A requests header object. | ||
| 84 | + | ||
| 85 | + Raises: | ||
| 86 | + GitlabAuthenticationError: If authentication is not correct | ||
| 87 | + GitlabHeadError: If the server cannot perform the request | ||
| 88 | + """ | ||
| 89 | + if TYPE_CHECKING: | ||
| 90 | + assert self.path is not None | ||
| 91 | + | ||
| 92 | + path = self.path | ||
| 93 | + if id is not None: | ||
| 94 | + path = f"{path}/{utils.EncodedId(id)}" | ||
| 95 | + | ||
| 96 | + return self.gitlab.http_head(path, **kwargs) | ||
| 97 | + | ||
| 98 | + | ||
| 99 | + class GetMixin(HeadMixin, _RestManagerBase): | ||
| 72 | 100 | _computed_path: Optional[str] | |
| 73 | 101 | _from_parent_attrs: Dict[str, Any] | |
| 74 | 102 | _obj_cls: Optional[Type[base.RESTObject]] | |
@@ -113,7 +141,7 @@ def get( | |||
| 113 | 141 | return self._obj_cls(self, server_data) | |
| 114 | 142 | ||
| 115 | 143 | ||
| 116 | - class GetWithoutIdMixin(_RestManagerBase): | ||
| 144 | + class GetWithoutIdMixin(HeadMixin, _RestManagerBase): | ||
| 117 | 145 | _computed_path: Optional[str] | |
| 118 | 146 | _from_parent_attrs: Dict[str, Any] | |
| 119 | 147 | _obj_cls: Optional[Type[base.RESTObject]] | |
@@ -181,7 +209,7 @@ def refresh(self, **kwargs: Any) -> None: | |||
| 181 | 209 | self._update_attrs(server_data) | |
| 182 | 210 | ||
| 183 | 211 | ||
| 184 | - class ListMixin(_RestManagerBase): | ||
| 212 | + class ListMixin(HeadMixin, _RestManagerBase): | ||
| 185 | 213 | _computed_path: Optional[str] | |
| 186 | 214 | _from_parent_attrs: Dict[str, Any] | |
| 187 | 215 | _list_filters: Tuple[str, ...] = () | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,16 @@ | |||
| 6 | 6 | from gitlab.v4.objects.projects import ProjectStorage | |
| 7 | 7 | ||
| 8 | 8 | ||
| 9 | + def test_projects_head(gl): | ||
| 10 | + headers = gl.projects.head() | ||
| 11 | + assert headers["x-total"] | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + def test_project_head(gl, project): | ||
| 15 | + headers = gl.projects.head(project.id) | ||
| 16 | + assert headers["content-type"] == "application/json" | ||
| 17 | + | ||
| 18 | + | ||
| 9 | 19 | def test_create_project(gl, user): | |
| 10 | 20 | # Moved from group tests chunk in legacy tests, TODO cleanup | |
| 11 | 21 | admin_project = gl.projects.create({"name": "admin_project"}) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,6 +40,9 @@ def test_repository_files(project): | |||
| 40 | 40 | # object method | |
| 41 | 41 | assert readme.decode().decode() == "Initial content" | |
| 42 | 42 | ||
| 43 | + headers = project.files.head("README.rst", ref="main") | ||
| 44 | + assert headers["X-Gitlab-File-Path"] == "README.rst" | ||
| 45 | + | ||
| 43 | 46 | blame = project.files.blame(file_path="README.rst", ref="main") | |
| 44 | 47 | assert blame | |
| 45 | 48 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | import pytest | |
| 2 | + import requests | ||
| 2 | 3 | import responses | |
| 3 | 4 | ||
| 4 | 5 | from gitlab import base | |
@@ -47,6 +48,26 @@ class M(GetMixin, FakeManager): | |||
| 47 | 48 | assert responses.assert_call_count(url, 1) is True | |
| 48 | 49 | ||
| 49 | 50 | ||
| 51 | + @responses.activate | ||
| 52 | + def test_head_mixin(gl): | ||
| 53 | + class M(GetMixin, FakeManager): | ||
| 54 | + pass | ||
| 55 | + | ||
| 56 | + url = "http://localhost/api/v4/tests/42" | ||
| 57 | + responses.add( | ||
| 58 | + method=responses.HEAD, | ||
| 59 | + url=url, | ||
| 60 | + headers={"X-GitLab-Header": "test"}, | ||
| 61 | + status=200, | ||
| 62 | + match=[responses.matchers.query_param_matcher({})], | ||
| 63 | + ) | ||
| 64 | + | ||
| 65 | + manager = M(gl) | ||
| 66 | + result = manager.head(42) | ||
| 67 | + assert isinstance(result, requests.structures.CaseInsensitiveDict) | ||
| 68 | + assert result["x-gitlab-header"] == "test" | ||
| 69 | + | ||
| 70 | + | ||
| 50 | 71 | @responses.activate | |
| 51 | 72 | def test_refresh_mixin(gl): | |
| 52 | 73 | class TestClass(RefreshMixin, FakeObject): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments