Skip to main content

credentialsManager

This code will:

  1. Generate a configuration template (if not already present).
  2. Attempt to load and initialize the YetiForceClient if the configuration file exists and is valid.

Parameters

load_from_config Method:

  • config_path (str): Path to the configuration file containing YetiForce credentials. Default is yetiforce_credentials.ini.

create_config_template Method:

  • output_path (str): Path where the template configuration file will be saved. Default is yetiforce_credentials.ini.

Initialization Steps

load_from_config:

  1. Logging Setup: Ensures logging is configured to capture error and info messages.
  2. File Validation: Checks if the configuration file exists. If not, raises a FileNotFoundError.
  3. Parse Configuration File:
    • Reads the .ini file using configparser.
    • Ensures the [YetiForce] section is present; otherwise, raises a ValueError.
  4. Extract Credentials:
    • Retrieves the following fields:
      • base_url: The API's base URL.
      • api_key_username and api_key_password: API Key credentials.
      • x_api_key: Additional security key.
      • api_user_email and api_user_password: User login credentials.
  5. Create Authentication Objects:
    • Generates ApiKey and AuthUser objects using the retrieved credentials.
  6. Initialize Client:
    • Returns a YetiForceClient instance configured with the extracted credentials.

create_config_template:

  1. Check File Existence: Verifies if the template already exists at the specified path.
  2. 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 YetiForceClient instance 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.ini file.
  • Validate and extract credentials.
  • Return an authenticated YetiForceClient instance 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()