Skip to content

Commit 3b4e52f

Browse files
Merge pull request #946 from gooddata/snapshot-master-61279e3d-to-rel/dev
[bot] Merge master/61279e3d into rel/dev
2 parents 8a4a6b5 + 61279e3 commit 3b4e52f

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

gooddata-sdk/gooddata_sdk/catalog/entity.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,23 @@ def from_api(cls, entity: dict[str, Any]) -> TokenCredentialsFromFile:
182182
raise NotImplementedError
183183

184184
@staticmethod
185-
def token_from_file(file_path: Union[str, Path]) -> str:
185+
def token_from_file(file_path: Union[str, Path], base64_encode: bool = True) -> str:
186+
"""
187+
Reads a token from a file and optionally base64 encodes it.
188+
189+
Args:
190+
file_path (Union[str, Path]): The path to the file containing the token.
191+
base64_encode (bool): Whether to base64 encode the token. Defaults to True.
192+
193+
Returns:
194+
str: The token, optionally base64 encoded.
195+
196+
Raises:
197+
FileNotFoundError: If the file does not exist.
198+
"""
186199
with open(file_path, "rb") as fp:
187-
return base64.b64encode(fp.read()).decode("utf-8")
200+
content = fp.read()
201+
return base64.b64encode(content).decode("utf-8") if base64_encode else content.decode("utf-8")
188202

189203

190204
@attr.s(auto_attribs=True, kw_only=True)
@@ -208,11 +222,24 @@ def from_api(cls, entity: dict[str, Any]) -> TokenCredentialsFromEnvVar:
208222
raise NotImplementedError
209223

210224
@staticmethod
211-
def token_from_env_var(env_var_name: str) -> str:
225+
def token_from_env_var(env_var_name: str, base64_encode: bool = True) -> str:
226+
"""
227+
Retrieves a token from an environment variable.
228+
229+
Args:
230+
env_var_name (str): The name of the environment variable containing the token.
231+
base64_encode (bool): Whether to base64 encode the token. Defaults to True for backwards compatibility.
232+
233+
Returns:
234+
str: The token, optionally base64 encoded.
235+
236+
Raises:
237+
ValueError: If the environment variable is not set or is empty.
238+
"""
212239
token = os.getenv(env_var_name)
213240
if token is None or token == "":
214241
raise ValueError(f"Environment variable {env_var_name} is not set")
215-
return base64.b64encode(token.encode("utf-8")).decode("utf-8")
242+
return base64.b64encode(token.encode("utf-8")).decode("utf-8") if base64_encode else token
216243

217244

218245
@attr.s(auto_attribs=True, kw_only=True)
@@ -299,6 +326,17 @@ def from_api(cls, entity: dict[str, Any]) -> ClientSecretCredentialsFromFile:
299326
raise NotImplementedError
300327

301328
@staticmethod
302-
def client_secret_from_file(file_path: Union[str, Path]) -> str:
329+
def client_secret_from_file(file_path: Union[str, Path], base64_encode: bool = False) -> str:
330+
"""
331+
Reads the client secret from a file.
332+
333+
Args:
334+
file_path (Union[str, Path]): The path to the file containing the client secret.
335+
base64_encode (bool): Whether to base64 encode the client secret or not. Defaults to False.
336+
337+
Returns:
338+
str: The client secret, optionally base64 encoded.
339+
"""
303340
with open(file_path, "rb") as fp:
304-
return base64.b64encode(fp.read()).decode("utf-8")
341+
content = fp.read()
342+
return base64.b64encode(content).decode("utf-8") if base64_encode else content.decode("utf-8")

0 commit comments

Comments
 (0)