credentialsManager
This code will:
- Generate a configuration template (if not already present).
- Attempt to load and initialize the
YetiForceClientif the configuration file exists and is valid.
Parameters
load_from_config Method:
config_path(str): Path to the configuration file containing YetiForce credentials. Default isyetiforce_credentials.ini.
create_config_template Method:
output_path(str): Path where the template configuration file will be saved. Default isyetiforce_credentials.ini.
Initialization Steps
load_from_config:
- Logging Setup: Ensures logging is configured to capture error and info messages.
- File Validation: Checks if the configuration file exists. If not, raises a
FileNotFoundError. - Parse Configuration File:
- Reads the
.inifile usingconfigparser. - Ensures the
[YetiForce]section is present; otherwise, raises aValueError.
- Reads the
- Extract Credentials:
- Retrieves the following fields:
base_url: The API's base URL.api_key_usernameandapi_key_password: API Key credentials.x_api_key: Additional security key.api_user_emailandapi_user_password: User login credentials.
- Retrieves the following fields:
- Create Authentication Objects:
- Generates
ApiKeyandAuthUserobjects using the retrieved credentials.
- Generates
- Initialize Client:
- Returns a
YetiForceClientinstance configured with the extracted credentials.
- Returns a
create_config_template:
- Check File Existence: Verifies if the template already exists at the specified path.
- Write Template:
- Creates a file with placeholders for the required credentials if it doesn't exist.
- Displays success or error messages based on the operation.
Purpose
The YetiForceClientCredentials class provides utilities to:
- Simplify Credential Management: Load YetiForce API credentials from a secure configuration file.
- Enhance Security: Isolate sensitive credentials from source code.
- Streamline Initialization: Quickly set up a
YetiForceClientinstance with minimal effort. - Provide Guidance: Generate a pre-defined configuration template to guide users in setting up credentials.
Example Code Usage
Create a Configuration Template
YetiForceClientCredentials.create_config_template()
This will generate a file yetiforce_credentials.ini (default path) containing placeholders for the required credentials:
[YetiForce]
# Base URL of the YetiForce API instance
base_url = https://example.yetiforce.com
# API Key Credentials
api_key_username = $APPLICATION_NAME$
api_key_password = $APPLICATION_PASSWORD$
# X-API-Key for additional security
x_api_key = $APPLICATION_KEY$
# API User Credentials
api_user_email = $DATABASE_USER_MAIL$
api_user_password = $APPLICATION_PASSWORD$
Load Credentials and Initialize Client
try:
client = YetiForceClientCredentials.load_from_config('yetiforce_credentials.ini')
print("YetiForce client successfully initialized!")
except FileNotFoundError as e:
print(f"Configuration file not found: {e}")
except ValueError as e:
print(f"Configuration error: {e}")
This will:
- Read the
yetiforce_credentials.inifile. - Validate and extract credentials.
- Return an authenticated
YetiForceClientinstance for making API calls.
Combined Example (Main Function)
def main():
# Option 1: Create a template file
YetiForceClientCredentials.create_config_template()
# Option 2: Load client from the configuration file
try:
client = YetiForceClientCredentials.load_from_config('yetiforce_credentials.ini')
print("YetiForce client successfully initialized!")
except (FileNotFoundError, ValueError) as e:
print(f"Error initializing YetiForce client: {e}")
if __name__ == "__main__":
main()