Skip to content

Commit eb684c8

Browse files
committed
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.
1 parent 658a839 commit eb684c8

5 files changed

Lines changed: 10 additions & 16 deletions

File tree

src/commercetools/base_client.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def refresh_token(self, token_url, **kwargs):
2525
class BaseClient:
2626
"""The Commercetools Client, used to interact with the Commercetools API.
2727
28-
:param project_key: the key for the project with which you want to interact
2928
:param client_id: the oauth2 client id
3029
:param client_secret: the oauth2 client secret
3130
:param scope: the oauth2 scope. If None then 'manage_project:{project_key}'
@@ -41,7 +40,6 @@ class BaseClient:
4140

4241
def __init__(
4342
self,
44-
project_key: str = None,
4543
client_id: str = None,
4644
client_secret: str = None,
4745
scope: typing.List[str] = None,
@@ -52,15 +50,14 @@ def __init__(
5250
) -> None:
5351
# Use environment variables as fallback
5452
config = {
55-
"project_key": project_key or "example-project",
5653
"client_id": client_id,
5754
"client_secret": client_secret,
5855
"url": url,
5956
"token_url": token_url,
6057
"scope": scope,
6158
}
6259
# Make sure we use the config vars
63-
del project_key, client_id, client_secret, url, token_url, scope
60+
del client_id, client_secret, url, token_url, scope
6461

6562
self._config = self._read_env_vars(config)
6663
self._config["token_url"] = fix_token_url(self._config["token_url"])
@@ -192,9 +189,6 @@ def _create_exception(self, obj, response) -> CommercetoolsError:
192189
return CommercetoolsError(obj.message, errors_raw, obj, correlation_id)
193190

194191
def _read_env_vars(self, config: dict) -> dict:
195-
if not config.get("project_key"):
196-
config["project_key"] = os.environ.get("CTP_PROJECT_KEY")
197-
198192
if not config.get("client_id"):
199193
config["client_id"] = os.environ.get("CTP_CLIENT_ID")
200194

@@ -211,8 +205,6 @@ def _read_env_vars(self, config: dict) -> dict:
211205
config["scope"] = os.environ.get("CTP_SCOPES")
212206
if config["scope"]:
213207
config["scope"] = config["scope"].split(",")
214-
else:
215-
config["scope"] = ["manage_project:%s" % config["project_key"]]
216208

217209
for key, value in config.items():
218210
if value is None:

src/commercetools/contrib/pytest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def ct_platform_client(
3030
yield PlatformClient(
3131
client_id="client-id",
3232
client_secret="client-secret",
33-
scope=[],
33+
scope=["manage_project:test"],
3434
url="https://api.europe-west1.gcp.commercetools.com",
3535
token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token",
3636
)

src/commercetools/frontend/client/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# contribute to this project then please do not edit this file directly
55
# but send a pull request to the Lab Digital fork of rmf-codegen at
66
# https://github.com/labd/rmf-codegen
7-
from commercetools.client import BaseClient
7+
from commercetools.base_client import BaseClient
88

99
from .api.api_request_builder import ApiRequestBuilder
1010
from .frontastic.frontastic_request_builder import FrontasticRequestBuilder
@@ -14,7 +14,7 @@ class Client(BaseClient):
1414

1515
def __init__(self, *args, **kwargs):
1616
kwargs.setdefault("url", "https://{project}-{customer}.frontastic.io")
17-
super().__init__(self, **kwargs)
17+
super().__init__(**kwargs)
1818

1919
def frontastic(self) -> FrontasticRequestBuilder:
2020
return FrontasticRequestBuilder(

tests/platform/test_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
def client_environment_settings(monkeypatch):
1414
monkeypatch.setenv("CTP_PROJECT_KEY", "project_key")
1515
monkeypatch.setenv("CTP_CLIENT_ID", "client_id")
16+
monkeypatch.setenv("CTP_SCOPES", "manage_project:project_key")
1617
monkeypatch.setenv("CTP_CLIENT_SECRET", "client_secret")
1718
monkeypatch.setenv("CTP_CLIENT_SCOPES", "some_scope")
1819
monkeypatch.setenv("CTP_API_URL", "https://api.europe-west1.gcp.commercetools.com")
@@ -34,6 +35,7 @@ def test_auto_refresh(commercetools_api):
3435
client = Client(
3536
client_id="unittest",
3637
client_secret="mysecret",
38+
scope=["manage_project:test"],
3739
url="https://api.europe-west1.gcp.commercetools.com",
3840
token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token",
3941
).with_project_key("unittest")
@@ -64,7 +66,7 @@ def test_cache_token(commercetools_api):
6466
Client(
6567
client_id="unittest",
6668
client_secret="none",
67-
project_key="test",
69+
scope=["manage_project:test"],
6870
url="https://api.europe-west1.gcp.commercetools.com",
6971
token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token",
7072
)
@@ -81,7 +83,7 @@ def test_allows_passing_custom_http_adapter():
8183
Client(
8284
client_id="unittest",
8385
client_secret="none",
84-
project_key="test",
86+
scope=["manage_project:test"],
8587
url="https://api.europe-west1.gcp.commercetools.com",
8688
token_url="https://auth.europe-west1.gcp.commercetools.com/oauth/token",
8789
http_adapter=my_adapter,

tests/platform/test_mock_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_http_server(ct_platform_client, commercetools_http_server):
2020
client = Client(
2121
client_id="client-id",
2222
client_secret="client-secret",
23-
scope=[],
23+
scope=["manage_project:test"],
2424
url=commercetools_http_server.api_url,
2525
token_url=f"{commercetools_http_server.api_url}/oauth/token",
2626
)
@@ -55,7 +55,7 @@ def test_http_server_expanding(ct_platform_client: Client, commercetools_http_se
5555
client = Client(
5656
client_id="client-id",
5757
client_secret="client-secret",
58-
scope=[],
58+
scope=["manage_project:test"],
5959
url=commercetools_http_server.api_url,
6060
token_url=f"{commercetools_http_server.api_url}/oauth/token",
6161
)

0 commit comments

Comments
 (0)