-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* firest commit config file * implementing configfiles part 2 -> added priorit and moved logic to CliUser class * added testing for configfiles * Update src/sxapi/cli/example-config.conf Co-authored-by: Florian Klien <flowolf@klienux.org> * Update src/sxapi/cli/example-config.conf Co-authored-by: Florian Klien <flowolf@klienux.org> * Update tests/cli_tests/test-config.conf Co-authored-by: Florian Klien <flowolf@klienux.org> * Update tests/cli_tests/test-config_param.conf Co-authored-by: Florian Klien <flowolf@klienux.org> * Update tests/cli_tests/test_cli.py Co-authored-by: Florian Klien <flowolf@klienux.org> * Update tests/cli_tests/test_cli.py Co-authored-by: Florian Klien <flowolf@klienux.org> * Update tests/cli_tests/test_cli.py Co-authored-by: Florian Klien <flowolf@klienux.org> * Update tests/cli_tests/test_cli.py Co-authored-by: Florian Klien <flowolf@klienux.org> * renamed cli_user.token to cli_user.api_access_token and renamed env variable name from SMAXTEC_TOKEN to SMAXTEC_API_ACCESS_TOKEN --------- Co-authored-by: Moser Marco Julian <marco.moser@smaxtec.com> Co-authored-by: Florian Klien <flowolf@klienux.org>
- Loading branch information
1 parent
088c1a1
commit 4fa202e
Showing
12 changed files
with
339 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .credentials import UserCredentials | ||
from .cli_user import CliUser | ||
|
||
user_credentials = UserCredentials() | ||
cli_user = CliUser() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import os | ||
|
||
import keyring | ||
|
||
from sxapi.base import ( | ||
IntegrationAPIV2, | ||
PublicAPIV2, | ||
) | ||
|
||
|
||
class CliUser: | ||
""" | ||
CliUser class used for initializing, storing, retrieving and deleting | ||
credentials and creating/holding Instances of supported API | ||
Client. | ||
This class should only be used in the cli package. | ||
""" | ||
|
||
def __init__(self): | ||
""" | ||
Basic User Credentials Constructor | ||
calls self._init_creds() to set available credentials on startup. | ||
""" | ||
|
||
self.api_access_token = None | ||
self.public_v2_api = None | ||
self.integration_v2_api = None | ||
|
||
@staticmethod | ||
def get_token_environment(): | ||
""" | ||
Gets token named 'SMAXTEC_API_ACCESS_TOKEN' from the systems' environment. | ||
""" | ||
|
||
return os.environ.get("SMAXTEC_API_ACCESS_TOKEN", None) | ||
|
||
def set_token_keyring(self, token): | ||
""" | ||
Store the given token in keyring. | ||
""" | ||
keyring.set_password("sxapi", "SMAXTEC_API_ACCESS_TOKEN", token) | ||
self.api_access_token = token | ||
|
||
@staticmethod | ||
def get_token_keyring(): | ||
""" | ||
Gets the token stored in the keyring. | ||
""" | ||
return keyring.get_password("sxapi", "SMAXTEC_API_ACCESS_TOKEN") | ||
|
||
@staticmethod | ||
def clear_token_keyring(): | ||
""" | ||
Deletes the token from the keyring. | ||
""" | ||
keyring.delete_password("sxapi", "SMAXTEC_API_ACCESS_TOKEN") | ||
|
||
# general functions | ||
def check_credentials_set(self): | ||
""" | ||
Checks if token is already set. | ||
""" | ||
if self.api_access_token is not None: | ||
return True | ||
return False | ||
|
||
def init_user(self, config_dict, args_token, args_keyring): | ||
""" | ||
This function retrieves the token from the specified resource | ||
(keyring, environment or args) and initializes clients | ||
of the supported APIs (PublicV2, IntegrationV2). | ||
If no token can be found the token is retrieved via | ||
the username and password. | ||
If username and password are also missing, no credentials get | ||
stored and not API clients are created. | ||
""" | ||
if args_token: | ||
self.api_access_token = args_token | ||
elif args_keyring: | ||
self.api_access_token = self.get_token_keyring() | ||
else: | ||
self.api_access_token = self.get_token_environment() | ||
|
||
if self.api_access_token is None and config_dict["user"] and config_dict["pwd"]: | ||
self.public_v2_api = PublicAPIV2( | ||
base_url=config_dict["api_public_v2_path"], | ||
email=config_dict["user"], | ||
password=config_dict["pwd"], | ||
) | ||
self.integration_v2_api = IntegrationAPIV2( | ||
base_url=config_dict["api_integration_v2_path"], | ||
email=config_dict["user"], | ||
password=config_dict["pwd"], | ||
) | ||
|
||
self.api_access_token = self.public_v2_api.get_token() | ||
|
||
elif self.api_access_token: | ||
self.public_v2_api = PublicAPIV2( | ||
base_url=config_dict["api_public_v2_path"], | ||
api_token=self.api_access_token, | ||
) | ||
|
||
self.integration_v2_api = IntegrationAPIV2( | ||
base_url=config_dict["api_integration_v2_path"], | ||
api_token=self.api_access_token, | ||
) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[SXAPI] | ||
|
||
# smaXtec user name | ||
USER = user.name@example.com | ||
|
||
# smaXtec password | ||
PASSWORD = "userSecret" | ||
|
||
# organisation to retrieve data from | ||
ORGA = smaxtec_organisation_id | ||
|
||
# API url for PublicV2 | ||
API_PUBLIC_V2_PATH = https://api.smaxtec.com/v2/public | ||
|
||
# API url for IntegrationV2 | ||
API_INTEGRATION_V2_PATH = https://api.smaxtec.com/v2/integration |
Oops, something went wrong.