import os class bad_apikey_exception(Exception): """ This error would be raised, when bad apikey occur. """ def __init__(self) -> None: super("Provided ApiKey not exists in database.") class apikey: """ This create new random API key. """ def __new__(cls, lenght: int = 128) -> str: """ This create new random API key. lenght: int - Lenght os the api key (default: 128) return: str - API key """ byte = cls.__get_random(lenght) string = cls.__to_hex(byte) return string def __to_hex(byte: bytes) -> str: """ This convert random bytes to string. byte: bytes - Bytes array to convert return: str - Result as string """ return byte.hex() def __get_random(lenght: int) -> bytes: """ Get random count of hex chars from os. lenght: int - Lenght of the bytes return: bytes - Random bytes from os """ return os.urandom(lenght)