Skip to content

Commit

Permalink
Merge pull request #220 from hkad98/PSDK-123
Browse files Browse the repository at this point in the history
PSDK-123 GoodPandas classmethod to load credentials from config file

Reviewed-by: Jan Soubusta
             https://github.com/jaceksan
  • Loading branch information
gdgate committed Feb 27, 2023
2 parents 1d6fb9d + 568d4c6 commit ba53804
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 9 deletions.
7 changes: 7 additions & 0 deletions gooddata-pandas/gooddata_pandas/good_pandas.py
@@ -1,12 +1,14 @@
# (C) 2021 GoodData Corporation
from __future__ import annotations

from pathlib import Path
from typing import Optional

from gooddata_pandas import __version__
from gooddata_pandas.dataframe import DataFrameFactory
from gooddata_pandas.series import SeriesFactory
from gooddata_sdk import GoodDataSdk
from gooddata_sdk.utils import PROFILES_FILE_PATH, good_pandas_profile_content

USER_AGENT = f"gooddata-pandas/{__version__}"
"""Extra segment of the User-Agent header that will be appended to standard gooddata-sdk user agent."""
Expand All @@ -30,6 +32,11 @@ def __init__(
self._series_per_ws: dict[str, SeriesFactory] = dict()
self._frames_per_ws: dict[str, DataFrameFactory] = dict()

@classmethod
def create_from_profile(cls, profile: str = "default", profiles_path: Path = PROFILES_FILE_PATH) -> GoodPandas:
content, custom_headers = good_pandas_profile_content(profile, profiles_path)
return cls(**content, **custom_headers)

@property
def sdk(self) -> GoodDataSdk:
return self._sdk
Expand Down
1 change: 1 addition & 0 deletions gooddata-pandas/tests/good_pandas/__init__.py
@@ -0,0 +1 @@
# (C) 2023 GoodData Corporation
17 changes: 17 additions & 0 deletions gooddata-pandas/tests/good_pandas/profiles/profiles.yaml
@@ -0,0 +1,17 @@
# (C) 2023 GoodData Corporation
default:
host: http://abc:3000
token: 123
custom_headers:
Host: localhost
extra_user_agent: xyz
custom:
host: http://xyz:3000
token: 456
custom_headers:
Host: localhost123
extra_user_agent: abc
wrong:
custom_headers:
Host: localhost123
extra_user_agent: abc
49 changes: 49 additions & 0 deletions gooddata-pandas/tests/good_pandas/test_good_pandas.py
@@ -0,0 +1,49 @@
# (C) 2023 GoodData Corporation
from __future__ import annotations

from pathlib import Path
from typing import Any

import yaml

from gooddata_pandas import GoodPandas

_current_dir = Path(__file__).parent.absolute()
PROFILES_PATH = _current_dir / "profiles" / "profiles.yaml"


def load_profiles_content() -> dict:
with open(PROFILES_PATH, "r", encoding="utf-8") as f:
return yaml.safe_load(f)


def are_same_check(profile_data: dict[str, Any], good_pandas: GoodPandas):
assert profile_data["host"] == good_pandas.sdk.client._hostname
assert profile_data["token"] == good_pandas.sdk.client._token
assert profile_data["custom_headers"] == good_pandas.sdk.client._custom_headers


def test_default_profile():
profile = "default"
good_pandas = GoodPandas.create_from_profile(profiles_path=PROFILES_PATH)
data = load_profiles_content()

are_same_check(data[profile], good_pandas)


def test_other_profile():
profile = "custom"
good_pandas = GoodPandas.create_from_profile(profile=profile, profiles_path=PROFILES_PATH)
data = load_profiles_content()

are_same_check(data[profile], good_pandas)


def test_wrong_profile():
profile = "wrong"
try:
GoodPandas.create_from_profile(profile=profile, profiles_path=PROFILES_PATH)
except ValueError:
assert True
else:
assert False, "The ValueError was expected to be raised."
16 changes: 15 additions & 1 deletion gooddata-sdk/gooddata_sdk/utils.py
Expand Up @@ -8,7 +8,7 @@
from collections.abc import KeysView
from pathlib import Path
from shutil import rmtree
from typing import Any, Callable, Dict, NamedTuple, Union, cast
from typing import Any, Callable, Dict, NamedTuple, Tuple, Union, cast

import yaml

Expand Down Expand Up @@ -221,3 +221,17 @@ def profile_content(profile: str = "default", profiles_path: Path = PROFILES_FIL
raise ValueError(f"Profiles file does not contain profile {profile}.")
mandatory_profile_content_check(profile, content.get(profile).keys())
return content.get(profile)


def good_pandas_profile_content(
profile: str = "default", profiles_path: Path = PROFILES_FILE_PATH
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
"""
This is workaround for GoodPandas. We should only use profile_content in the future.
For that we need to unify GoodPandas and GoodDataSdk interface.
"""
content = profile_content(profile, profiles_path)
custom_headers = content["custom_headers"]
del content["extra_user_agent"]
del content["custom_headers"]
return content, custom_headers
20 changes: 12 additions & 8 deletions gooddata-sdk/tests/sdk/test_sdk.py
@@ -1,5 +1,8 @@
# (C) 2022 GoodData Corporation
from __future__ import annotations

from pathlib import Path
from typing import Any

import yaml

Expand All @@ -14,26 +17,27 @@ def load_profiles_content() -> dict:
return yaml.safe_load(f)


def are_same_check(profile_data: dict[str, Any], sdk: GoodDataSdk):
assert profile_data["host"] == sdk.client._hostname
assert profile_data["token"] == sdk.client._token
assert profile_data["custom_headers"] == sdk.client._custom_headers
assert profile_data["extra_user_agent"] in sdk.client._api_client.user_agent


def test_default_profile():
profile = "default"
sdk = GoodDataSdk.create_from_profile(profiles_path=PROFILES_PATH)
data = load_profiles_content()

assert data[profile]["host"] == sdk.client._hostname
assert data[profile]["token"] == sdk.client._token
assert data[profile]["custom_headers"] == sdk.client._custom_headers
assert data[profile]["extra_user_agent"] in sdk.client._api_client.user_agent
are_same_check(data[profile], sdk)


def test_other_profile():
profile = "custom"
sdk = GoodDataSdk.create_from_profile(profile=profile, profiles_path=PROFILES_PATH)
data = load_profiles_content()

assert data[profile]["host"] == sdk.client._hostname
assert data[profile]["token"] == sdk.client._token
assert data[profile]["custom_headers"] == sdk.client._custom_headers
assert data[profile]["extra_user_agent"] in sdk.client._api_client.user_agent
are_same_check(data[profile], sdk)


def test_wrong_profile():
Expand Down

0 comments on commit ba53804

Please sign in to comment.