From eb684c86c8659688886ee0e437b4c14027c51129 Mon Sep 17 00:00:00 2001 From: Michael van Tellingen Date: Sat, 6 Apr 2024 08:58:09 +0200 Subject: [PATCH] Simplify handling of client initialization Remove magic surrounding passing of project key and automatically generating the scope, instead just follow the api specs whereby the project key need to be passed as separate action. --- src/commercetools/base_client.py | 10 +--------- src/commercetools/contrib/pytest.py | 2 +- src/commercetools/frontend/client/__init__.py | 4 ++-- tests/platform/test_client.py | 6 ++++-- tests/platform/test_mock_server.py | 4 ++-- 5 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/commercetools/base_client.py b/src/commercetools/base_client.py index a8821983..6fbb62dd 100644 --- a/src/commercetools/base_client.py +++ b/src/commercetools/base_client.py @@ -25,7 +25,6 @@ def refresh_token(self, token_url, **kwargs): class BaseClient: """The Commercetools Client, used to interact with the Commercetools API. - :param project_key: the key for the project with which you want to interact :param client_id: the oauth2 client id :param client_secret: the oauth2 client secret :param scope: the oauth2 scope. If None then 'manage_project:{project_key}' @@ -41,7 +40,6 @@ class BaseClient: def __init__( self, - project_key: str = None, client_id: str = None, client_secret: str = None, scope: typing.List[str] = None, @@ -52,7 +50,6 @@ def __init__( ) -> None: # Use environment variables as fallback config = { - "project_key": project_key or "example-project", "client_id": client_id, "client_secret": client_secret, "url": url, @@ -60,7 +57,7 @@ def __init__( "scope": scope, } # Make sure we use the config vars - del project_key, client_id, client_secret, url, token_url, scope + del client_id, client_secret, url, token_url, scope self._config = self._read_env_vars(config) self._config["token_url"] = fix_token_url(self._config["token_url"]) @@ -192,9 +189,6 @@ def _create_exception(self, obj, response) -> CommercetoolsError: return CommercetoolsError(obj.message, errors_raw, obj, correlation_id) def _read_env_vars(self, config: dict) -> dict: - if not config.get("project_key"): - config["project_key"] = os.environ.get("CTP_PROJECT_KEY") - if not config.get("client_id"): config["client_id"] = os.environ.get("CTP_CLIENT_ID") @@ -211,8 +205,6 @@ def _read_env_vars(self, config: dict) -> dict: config["scope"] = os.environ.get("CTP_SCOPES") if config["scope"]: config["scope"] = config["scope"].split(",") - else: - config["scope"] = ["manage_project:%s" % config["project_key"]] for key, value in config.items(): if value is None: diff --git a/src/commercetools/contrib/pytest.py b/src/commercetools/contrib/pytest.py index 1abfba2b..e5ddfbcc 100644 --- a/src/commercetools/contrib/pytest.py +++ b/src/commercetools/contrib/pytest.py @@ -30,7 +30,7 @@ def ct_platform_client( yield PlatformClient( client_id="client-id", client_secret="client-secret", - scope=[], + scope=["manage_project:test"], url="https://api.europe-west1.gcp.commercetools.com", token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token", ) diff --git a/src/commercetools/frontend/client/__init__.py b/src/commercetools/frontend/client/__init__.py index f32ec09a..325a7678 100644 --- a/src/commercetools/frontend/client/__init__.py +++ b/src/commercetools/frontend/client/__init__.py @@ -4,7 +4,7 @@ # contribute to this project then please do not edit this file directly # but send a pull request to the Lab Digital fork of rmf-codegen at # https://github.com/labd/rmf-codegen -from commercetools.client import BaseClient +from commercetools.base_client import BaseClient from .api.api_request_builder import ApiRequestBuilder from .frontastic.frontastic_request_builder import FrontasticRequestBuilder @@ -14,7 +14,7 @@ class Client(BaseClient): def __init__(self, *args, **kwargs): kwargs.setdefault("url", "https://{project}-{customer}.frontastic.io") - super().__init__(self, **kwargs) + super().__init__(**kwargs) def frontastic(self) -> FrontasticRequestBuilder: return FrontasticRequestBuilder( diff --git a/tests/platform/test_client.py b/tests/platform/test_client.py index 4273a220..392ef9b1 100644 --- a/tests/platform/test_client.py +++ b/tests/platform/test_client.py @@ -13,6 +13,7 @@ def client_environment_settings(monkeypatch): monkeypatch.setenv("CTP_PROJECT_KEY", "project_key") monkeypatch.setenv("CTP_CLIENT_ID", "client_id") + monkeypatch.setenv("CTP_SCOPES", "manage_project:project_key") monkeypatch.setenv("CTP_CLIENT_SECRET", "client_secret") monkeypatch.setenv("CTP_CLIENT_SCOPES", "some_scope") monkeypatch.setenv("CTP_API_URL", "https://api.europe-west1.gcp.commercetools.com") @@ -34,6 +35,7 @@ def test_auto_refresh(commercetools_api): client = Client( client_id="unittest", client_secret="mysecret", + scope=["manage_project:test"], url="https://api.europe-west1.gcp.commercetools.com", token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token", ).with_project_key("unittest") @@ -64,7 +66,7 @@ def test_cache_token(commercetools_api): Client( client_id="unittest", client_secret="none", - project_key="test", + scope=["manage_project:test"], url="https://api.europe-west1.gcp.commercetools.com", token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token", ) @@ -81,7 +83,7 @@ def test_allows_passing_custom_http_adapter(): Client( client_id="unittest", client_secret="none", - project_key="test", + scope=["manage_project:test"], url="https://api.europe-west1.gcp.commercetools.com", token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token", http_adapter=my_adapter, diff --git a/tests/platform/test_mock_server.py b/tests/platform/test_mock_server.py index 58b4813c..d3f841f5 100644 --- a/tests/platform/test_mock_server.py +++ b/tests/platform/test_mock_server.py @@ -20,7 +20,7 @@ def test_http_server(ct_platform_client, commercetools_http_server): client = Client( client_id="client-id", client_secret="client-secret", - scope=[], + scope=["manage_project:test"], url=commercetools_http_server.api_url, token_url=f"{commercetools_http_server.api_url}/oauth/token", ) @@ -55,7 +55,7 @@ def test_http_server_expanding(ct_platform_client: Client, commercetools_http_se client = Client( client_id="client-id", client_secret="client-secret", - scope=[], + scope=["manage_project:test"], url=commercetools_http_server.api_url, token_url=f"{commercetools_http_server.api_url}/oauth/token", )