Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions cloudsmith_cli/core/api/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def initialise_api(
values = decoded.decode("utf-8")
config.username, config.password = values.split(":")

set_api_key(config, key)
if key:
config.api_key["X-Api-Key"] = key

if hasattr(cloudsmith_api.Configuration, "set_default"):
cloudsmith_api.Configuration.set_default(config)
return config


Expand All @@ -74,9 +78,14 @@ def get_api_client(cls):
return client


def set_api_key(config, key):
"""Configure a new API key."""
if not key and "X-Api-Key" in config.api_key:
def unset_api_key():
"""Unset the API key."""
config = cloudsmith_api.Configuration()

try:
del config.api_key["X-Api-Key"]
else:
config.api_key["X-Api-Key"] = key
except KeyError:
pass

if hasattr(cloudsmith_api.Configuration, "set_default"):
cloudsmith_api.Configuration.set_default(config)
5 changes: 2 additions & 3 deletions cloudsmith_cli/core/api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .. import ratelimits
from .exceptions import catch_raise_api_exception
from .init import get_api_client, set_api_key
from .init import get_api_client, unset_api_key


def get_user_api():
Expand All @@ -19,8 +19,7 @@ def get_user_token(login, password):
client = get_user_api()

# Never use API key for the token endpoint
config = cloudsmith_api.Configuration()
set_api_key(config, None)
unset_api_key()

with catch_raise_api_exception():
data, _, headers = client.user_token_create_with_http_info(
Expand Down