Objective: api.py manages API keys, allowing for their addition, removal, and retrieval from a .env file using the python-dotenv package.
Components:
APIManager: A class that handles the loading, adding, removing, and listing of API keys stored in a .env file.
Detailed Breakdown
Initialization (__init__ method):
Loads the environment variables from the specified .env file.
Initializes a dictionary to store the API keys by reading the .env file.
Load API Keys (load_api_keys method):
Loads API keys from the .env file where keys end with _API_KEY.
Returns a dictionary of API keys.
Add API Key (add_api_key method):
Prompts the user to enter the name and key for a new API.
Validates and adds the API key to the .env file and the internal dictionary.
Provides an option to remove an API key.
Get API Key (get_api_key method):
Retrieves an API key by its name from the internal dictionary.
Remove API Key (remove_api_key method):
Removes an API key from both the .env file and the internal dictionary.
List API Keys (list_api_keys method):
Lists all loaded API keys.
Ensure API Keys (ensure_api_keys method):
Ensures that there are API keys loaded, if not, it prompts the user to add them.
Usage
The APIManager class provides a convenient way to manage API keys within a Python application, handling the storage and retrieval of keys securely. Example Usage:
if __name__ == "__main__":
api_manager = APIManager()
api_manager.ensure_api_keys()
# Example of how to get an API key
openai_api_key = api_manager.get_api_key('openai')
if openai_api_key:
print(f"OpenAI API key: {openai_api_key}")
else:
print("OpenAI API key not found.")
# Example of how to remove an API key
api_manager.remove_api_key('openai')
api_manager.list_api_keys()
Key Methods and Functions:
__init__(self, env_file='.env'): Initializes the APIManager with the specified .env file and loads API keys.
load_api_keys(self): Loads API keys from the .env file into a dictionary.
add_api_key(self): Prompts the user to add a new API key or remove an existing one.
get_api_key(self, api_name): Retrieves an API key by its name.
remove_api_key(self, api_name): Removes an API key from the .env file and the internal dictionary.
list_api_keys(self): Lists all API keys currently loaded.
ensure_api_keys(self): Ensures that API keys are loaded, prompting the user to add keys if none are found.
This script is essential for securely managing API keys in applications, providing a simple interface for key management operations.