diff options
| author | Ben Brown <ben@demerara.io> | 2022-11-07 22:22:26 +0000 |
|---|---|---|
| committer | Nejc Habjan <hab.nejc@gmail.com> | 2022-11-10 13:16:57 +0100 |
| commit | d0a034878fabfd8409134aa8b7ffeeb40219683c (patch) | |
| tree | d3291921754730426ee36a659596fa151d665d25 /gitlab/v4/objects | |
| parent | fcd72fe243daa0623abfde267c7ab1c6866bcd52 (diff) | |
| download | gitlab-d0a034878fabfd8409134aa8b7ffeeb40219683c.tar.gz | |
feat: implement secure files API
Diffstat (limited to 'gitlab/v4/objects')
| -rw-r--r-- | gitlab/v4/objects/__init__.py | 1 | ||||
| -rw-r--r-- | gitlab/v4/objects/projects.py | 2 | ||||
| -rw-r--r-- | gitlab/v4/objects/secure_files.py | 69 |
3 files changed, 72 insertions, 0 deletions
diff --git a/gitlab/v4/objects/__init__.py b/gitlab/v4/objects/__init__.py index f1012a0..56c17d5 100644 --- a/gitlab/v4/objects/__init__.py +++ b/gitlab/v4/objects/__init__.py @@ -53,6 +53,7 @@ from .push_rules import * from .releases import * from .repositories import * from .runners import * +from .secure_files import * from .settings import * from .sidekiq import * from .snippets import * diff --git a/gitlab/v4/objects/projects.py b/gitlab/v4/objects/projects.py index daba896..fad21eb 100644 --- a/gitlab/v4/objects/projects.py +++ b/gitlab/v4/objects/projects.py @@ -82,6 +82,7 @@ from .push_rules import ProjectPushRulesManager # noqa: F401 from .releases import ProjectReleaseManager # noqa: F401 from .repositories import RepositoryMixin from .runners import ProjectRunnerManager # noqa: F401 +from .secure_files import SecureFileManager # noqa: F401 from .snippets import ProjectSnippetManager # noqa: F401 from .statistics import ( # noqa: F401 ProjectAdditionalStatisticsManager, @@ -209,6 +210,7 @@ class Project(RefreshMixin, SaveMixin, ObjectDeleteMixin, RepositoryMixin, RESTO remote_mirrors: "ProjectRemoteMirrorManager" repositories: ProjectRegistryRepositoryManager runners: ProjectRunnerManager + secure_files: SecureFileManager services: ProjectServiceManager snippets: ProjectSnippetManager storage: "ProjectStorageManager" diff --git a/gitlab/v4/objects/secure_files.py b/gitlab/v4/objects/secure_files.py new file mode 100644 index 0000000..ce8d12d --- /dev/null +++ b/gitlab/v4/objects/secure_files.py @@ -0,0 +1,69 @@ +""" +GitLab API: +https://docs.gitlab.com/ee/api/secure_files.html +""" +from typing import Any, Callable, cast, Iterator, Optional, TYPE_CHECKING, Union + +import requests + +from gitlab import cli +from gitlab import exceptions as exc +from gitlab import utils +from gitlab.base import RESTManager, RESTObject +from gitlab.mixins import NoUpdateMixin, ObjectDeleteMixin +from gitlab.types import FileAttribute, RequiredOptional + +__all__ = ["SecureFile", "SecureFileManager"] + + +class SecureFile(ObjectDeleteMixin, RESTObject): + @cli.register_custom_action("SecureFile") + @exc.on_http_error(exc.GitlabGetError) + def download( + self, + streamed: bool = False, + action: Optional[Callable[[bytes], None]] = None, + chunk_size: int = 1024, + *, + iterator: bool = False, + **kwargs: Any, + ) -> Optional[Union[bytes, Iterator[Any]]]: + """Download the secure file. + + Args: + streamed: If True the data will be processed by chunks of + `chunk_size` and each chunk is passed to `action` for + treatment + iterator: If True directly return the underlying response + iterator + action: Callable responsible of dealing with chunk of + data + chunk_size: Size of each chunk + **kwargs: Extra options to send to the server (e.g. sudo) + + Raises: + GitlabAuthenticationError: If authentication is not correct + GitlabGetError: If the artifacts could not be retrieved + + Returns: + The artifacts if `streamed` is False, None otherwise.""" + path = f"{self.manager.path}/{self.id}/download" + result = self.manager.gitlab.http_get( + path, streamed=streamed, raw=True, **kwargs + ) + if TYPE_CHECKING: + assert isinstance(result, requests.Response) + return utils.response_content( + result, streamed, action, chunk_size, iterator=iterator + ) + + +class SecureFileManager(NoUpdateMixin, RESTManager): + _path = "/projects/{project_id}/secure_files" + _obj_cls = SecureFile + _from_parent_attrs = {"project_id": "id"} + _create_attrs = RequiredOptional(required=("name", "file")) + _types = {"file": FileAttribute} + + def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> SecureFile: + return cast(SecureFile, super().get(id=id, lazy=lazy, **kwargs)) |
