Skip to content

API Key

GoogleApiKey

This class provides methods to get and set the Google AI platform API key.

Source code in src/peter_explains/api_key.py
class GoogleApiKey:
    """
    This class provides methods to get and set the Google AI platform API key.
    """

    def __init__(self) -> None:
        self.api_key_path = GoogleApiKey.get_api_key_file_path()

    @staticmethod
    def get_api_key_file_path() -> str:
        return os.path.join(get_app_data_dir(__app_name__), f"{__app_name__}_api.json")

    def get(self) -> str:
        """
        Retrieves the API key from the specified file.

        Returns:
            str: The API key.

        Raises:
            FileNotFoundError: If the API key file does not exist.
        """
        if not os.path.exists(self.api_key_path):
            show_no_api_key_error()
            sys.exit(1)

        with open(self.api_key_path, "r", encoding="utf-8") as file:
            data = json.load(file)
            return data["api_key"]

    def set(self, api_key: str):
        """
        Sets the API key for the application.

        Args:
            api_key (str): The API key to be set.

        Raises:
            ValueError: If the length of the API key is less than 30 or greater than 50.

        Returns:
            None
        """
        if len(api_key) < 30 or len(api_key) > 50:
            show_crappy_api_key_error()
            sys.exit(1)

        # Create with 0600 up front rather than chmod-ing after: an open() would
        # briefly leave the key world-readable.
        fd = os.open(self.api_key_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
        with os.fdopen(fd, "w", encoding="utf-8") as file:
            json.dump({"api_key": api_key}, file)
        show_api_key_success_message()
        sys.exit(0)

    def clear(self):
        """
        This function clears the stored API key.

        Returns:
            None
        """
        try:
            os.remove(self.api_key_path)
        except FileNotFoundError:
            pass
        print("Retep just deleted your API key. Loser!")

clear()

This function clears the stored API key.

Returns:

Type Description

None

Source code in src/peter_explains/api_key.py
def clear(self):
    """
    This function clears the stored API key.

    Returns:
        None
    """
    try:
        os.remove(self.api_key_path)
    except FileNotFoundError:
        pass
    print("Retep just deleted your API key. Loser!")

get()

Retrieves the API key from the specified file.

Returns:

Name Type Description
str str

The API key.

Raises:

Type Description
FileNotFoundError

If the API key file does not exist.

Source code in src/peter_explains/api_key.py
def get(self) -> str:
    """
    Retrieves the API key from the specified file.

    Returns:
        str: The API key.

    Raises:
        FileNotFoundError: If the API key file does not exist.
    """
    if not os.path.exists(self.api_key_path):
        show_no_api_key_error()
        sys.exit(1)

    with open(self.api_key_path, "r", encoding="utf-8") as file:
        data = json.load(file)
        return data["api_key"]

set(api_key)

Sets the API key for the application.

Parameters:

Name Type Description Default
api_key str

The API key to be set.

required

Raises:

Type Description
ValueError

If the length of the API key is less than 30 or greater than 50.

Returns:

Type Description

None

Source code in src/peter_explains/api_key.py
def set(self, api_key: str):
    """
    Sets the API key for the application.

    Args:
        api_key (str): The API key to be set.

    Raises:
        ValueError: If the length of the API key is less than 30 or greater than 50.

    Returns:
        None
    """
    if len(api_key) < 30 or len(api_key) > 50:
        show_crappy_api_key_error()
        sys.exit(1)

    # Create with 0600 up front rather than chmod-ing after: an open() would
    # briefly leave the key world-readable.
    fd = os.open(self.api_key_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
    with os.fdopen(fd, "w", encoding="utf-8") as file:
        json.dump({"api_key": api_key}, file)
    show_api_key_success_message()
    sys.exit(0)