Skip to content

Commit

Permalink
Implement changes from code review
Browse files Browse the repository at this point in the history
Convert asserts to raised ValueErrors, use f-strings, etc.
  • Loading branch information
malexmave committed Dec 16, 2024
1 parent a14a700 commit 2fcafc6
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions kcwarden/subcommands/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def get_password(user):


def get_client_secret():
if "KCWARDEN_CLIENT_SECRET" in os.environ:
return os.environ["KCWARDEN_CLIENT_SECRET"]
return ""
return os.environ.get("KCWARDEN_CLIENT_SECRET", "")


def get_totp():
Expand All @@ -60,13 +58,12 @@ def get_token_password_grant(base_url, auth_realm, user, totp_required, client_i

req = requests.post(token_url, data=auth_data)
try:
req.json()
json_response = req.json()
except requests.RequestException:
assert False, "Could not parse JSON. Response was: {}".format(req.content)
assert "access_token" in req.json(), "Did not receive an access token in response. Response was: {}".format(
req.json()
)
return req.json()["access_token"]
raise ValueError(f"Could not parse JSON. Response was: {req.content}")
if "access_token" not in json_response:
raise ValueError(f"Did not receive an access token in response. Response was: {json_response}")
return json_response["access_token"]


def get_token_client_credential_grant(base_url, auth_realm, client_id, client_secret):
Expand All @@ -76,13 +73,12 @@ def get_token_client_credential_grant(base_url, auth_realm, client_id, client_se
token_url, data={"grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret}
)
try:
req.json()
json_response = req.json()
except requests.RequestException:
assert False, "Could not parse JSON. Response was: {}".format(req.content)
assert "access_token" in req.json(), "Did not receive an access token in response. Response was: {}".format(
req.json()
)
return req.json()["access_token"]
raise ValueError(f"Could not parse JSON. Response was: {req.content}")
if "access_token" not in json_response:
raise ValueError(f"Did not receive an access token in response. Response was: {json_response}")
return json_response["access_token"]


### Main Loop
Expand Down

0 comments on commit 2fcafc6

Please sign in to comment.