Skip to content

Commit

Permalink
Simplify handling of client initialization
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvantellingen committed Apr 6, 2024
1 parent 658a839 commit eb684c8
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 16 deletions.
10 changes: 1 addition & 9 deletions src/commercetools/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand All @@ -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,
Expand All @@ -52,15 +50,14 @@ 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,
"token_url": token_url,
"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"])
Expand Down Expand Up @@ -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")

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/commercetools/contrib/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down
4 changes: 2 additions & 2 deletions src/commercetools/frontend/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions tests/platform/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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",
)
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/platform/test_mock_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down Expand Up @@ -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",
)
Expand Down

0 comments on commit eb684c8

Please sign in to comment.