Skip to content

Commit

Permalink
fix linting & mypy
Browse files Browse the repository at this point in the history
Signed-off-by: bossenti <[email protected]>
  • Loading branch information
bossenti committed May 30, 2023
1 parent 66c088c commit c8a03b6
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions streampipes-client-python/streampipes/client/credential_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,17 @@ def from_env(cls, username_env: str, api_key_env: str) -> StreamPipesApiKeyCrede
return cls(username=username, api_key=api_key)

def __init__(
self,
username: Optional[str] = None,
api_key: Optional[str] = None,
self,
username: Optional[str] = None,
api_key: Optional[str] = None,
):

# if both parameters are passed we can add them directly to the instance
if all({username, api_key}):
self.username = username
self.api_key = api_key

# otherwise we need to check if environment variables are properly set
else:

retrieved_api_key = os.environ.get(self._ENV_KEY_API, None)
retrieved_user_name = os.environ.get(self._ENV_KEY_USERNAME, None)

Expand All @@ -169,27 +167,32 @@ def __init__(
self.username = username

if retrieved_api_key is None:
raise AttributeError("API key not found in the environment variables - please provide the API key "
"via the parameter `api_key` or ensure that it is passed to "
f"the environment variable `{self._ENV_KEY_API}`.")
raise AttributeError(
"API key not found in the environment variables - please provide the API key "
"via the parameter `api_key` or ensure that it is passed to "
f"the environment variable `{self._ENV_KEY_API}`."
)
self.api_key = retrieved_api_key

if api_key:
self.api_key = api_key

if retrieved_user_name is None:
raise AttributeError("Username not found - please provide the username "
"via the parameter `username` or ensure that it is passed to "
f"the environment variable `{self._ENV_KEY_USERNAME}`.")
raise AttributeError(
"Username not found - please provide the username "
"via the parameter `username` or ensure that it is passed to "
f"the environment variable `{self._ENV_KEY_USERNAME}`."
)
self.username = retrieved_user_name

if not all({self.username, self.api_key}):

if None in {retrieved_user_name, retrieved_api_key}:
raise AttributeError("Both parameters not found - please provide both api key and username "
"either via the parameters (`api_key` and `username`) or via "
f"the corresponding environment variables (`{self._ENV_KEY_API}` and "
f"`{self._ENV_KEY_USERNAME}`).")
raise AttributeError(
"Both parameters not found - please provide both api key and username "
"either via the parameters (`api_key` and `username`) or via "
f"the corresponding environment variables (`{self._ENV_KEY_API}` and "
f"`{self._ENV_KEY_USERNAME}`)."
)

@property
def _authentication_headers(self) -> Dict[str, str]:
Expand All @@ -200,7 +203,11 @@ def _authentication_headers(self) -> Dict[str, str]:
Dictionary with authentication headers as string key-value pairs.
"""

user: str = self.username # type: ignore # mypy mistakenly detects this attribute as optional
token: str = self.api_key # type: ignore # mypy mistakenly detects this attribute as optional

return {
"X-API-User": self.username,
"X-API-Key": self.api_key,
"X-API-User": user,
"X-API-Key": token,
}

0 comments on commit c8a03b6

Please sign in to comment.