diff options
author | Nejc Habjan <nejc.habjan@siemens.com> | 2022-11-26 15:59:20 +0100 |
---|---|---|
committer | Nejc Habjan <nejc.habjan@siemens.com> | 2022-11-26 15:59:20 +0100 |
commit | 875140296fada3659d224e3a5819ba77a81e36fe (patch) | |
tree | 5ed1344e9b1f265eb673341689a84a39f1527aa7 /gitlab | |
parent | 0ecf3bbe28c92fd26a7d132bf7f5ae9481cbad30 (diff) | |
download | gitlab-feat/decode-to-string.tar.gz |
feat(files): allow decoding project files directly to stringfeat/decode-to-string
Diffstat (limited to 'gitlab')
-rw-r--r-- | gitlab/v4/objects/files.py | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/gitlab/v4/objects/files.py b/gitlab/v4/objects/files.py index d81b711..56c1791 100644 --- a/gitlab/v4/objects/files.py +++ b/gitlab/v4/objects/files.py @@ -7,6 +7,7 @@ from typing import ( Iterator, List, Optional, + overload, TYPE_CHECKING, Union, ) @@ -41,13 +42,30 @@ class ProjectFile(SaveMixin, ObjectDeleteMixin, RESTObject): file_path: str manager: "ProjectFileManager" + @overload def decode(self) -> bytes: + ... + + @overload + def decode(self, encoding: None) -> bytes: + ... + + @overload + def decode(self, encoding: str) -> str: + ... + + def decode(self, encoding: Optional[str] = None) -> Union[bytes, str]: """Returns the decoded content of the file. Returns: - The decoded content. + The decoded content as bytes. + The decoded content as string if a valid encoding is provided. """ - return base64.b64decode(self.content) + decoded_bytes = base64.b64decode(self.content) + + if encoding is not None: + return decoded_bytes.decode(encoding) + return decoded_bytes # NOTE(jlvillal): Signature doesn't match SaveMixin.save() so ignore # type error |