summaryrefslogtreecommitdiff
path: root/gitlab/v4
diff options
context:
space:
mode:
authorNejc Habjan <nejc.habjan@siemens.com>2022-11-26 15:59:20 +0100
committerNejc Habjan <nejc.habjan@siemens.com>2022-11-26 15:59:20 +0100
commit875140296fada3659d224e3a5819ba77a81e36fe (patch)
tree5ed1344e9b1f265eb673341689a84a39f1527aa7 /gitlab/v4
parent0ecf3bbe28c92fd26a7d132bf7f5ae9481cbad30 (diff)
downloadgitlab-feat/decode-to-string.tar.gz
feat(files): allow decoding project files directly to stringfeat/decode-to-string
Diffstat (limited to 'gitlab/v4')
-rw-r--r--gitlab/v4/objects/files.py22
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