Module source.apikey

Classes

class apikey (content: str)
Expand source code
class apikey:
    """
    This class represents API key, and it is also creating new apikeys from 
    device random numbers generator.

    Methods
    ------
    get_raw_size() : int
        Return size of bytes which was loaded from device random number 
        generator.

    get_size() : int
        Return lenght of the API key as string.

    get_prefix() : str
        Return prefix of the API keys.

    create() : apikey
        Create new random API key.

    export() : str
        Export API key as string.
    """

    def __init__(self, content: str) -> None:
        """
        It recreate API key object from key, which was previously exported
        to string.

        Raises
        ------
        ValueError
            When API key lenght is not correct.

        Parameters
        ---------
        content : str
            Preiviously exported API key string.
        """
        
        if len(content) != self.get_size():
            raise ValueError("API key lenght is not correct.")

        self.__content = content

    def get_raw_size(self: object | None = None) -> int:
        """
        It return lenght of the random bytes in the API key.

        Returns
        -------
        int
            Lenght of the random bytes in the API key.
        """

        return 128

    def get_size(self: object | None = None) -> int:
        """
        It return lenght of the API key string, with prefix and random bytes
        converted to the hex string.

        Returns
        -------
        int
            Lenght of the API key string.
        """

        return len(apikey.get_prefix()) + apikey.get_raw_size() * 2

    def get_prefix(self: object | None = None) -> str:
        """
        It return API key prefix.

        Returns
        -------
        str
            Prefix for the API key.
        """

        return "cxtk_"

    def create() -> object:
        """
        It create new API key from random number generator device.

        Returns
        -------
        apikey
            New random API key.
        """

        raw = os.urandom(apikey.get_raw_size())
        key = apikey.get_prefix() + raw.hex()

        return apikey(key)

    def export(self) -> str:
        """
        It export API key as string.

        Returns
        -------
        str
            API key string content.
        """

        return self.__content

    def __repr__(self) -> str:
        """
        It return readable API key description (first 32 letters).

        Returns
        -------
        str
            Readable API key description.
        """

        return "API key: \"" + self.__content[0:32] + "...\""

    def __str__(self) -> str:
        """
        It return API key string content.

        Returns
        -------
        str
            API key as string.
        """

        return self.__content

    def __eq__(self, other) -> bool:
        """
        It check that API keys are same.

        Returns
        -------
        bool  
            True when API keys are same.
        """

        return self.__content == other.__content

    def __ne__(self, other) -> bool:
        """
        It check that API keys are not same.

        Returns 
        -------
        bool
            True when API keys are not same.
        """

        return self.__content != other.__content

This class represents API key, and it is also creating new apikeys from device random numbers generator.

Methods

get_raw_size() : int Return size of bytes which was loaded from device random number generator.

get_size() : int Return lenght of the API key as string.

get_prefix() : str Return prefix of the API keys.

create() : apikey Create new random API key.

export() : str Export API key as string.

It recreate API key object from key, which was previously exported to string.

Raises

ValueError
When API key lenght is not correct.

Parameters

content : str
Preiviously exported API key string.

Methods

def create() ‑> object
Expand source code
def create() -> object:
    """
    It create new API key from random number generator device.

    Returns
    -------
    apikey
        New random API key.
    """

    raw = os.urandom(apikey.get_raw_size())
    key = apikey.get_prefix() + raw.hex()

    return apikey(key)

It create new API key from random number generator device.

Returns

apikey
New random API key.
def export(self) ‑> str
Expand source code
def export(self) -> str:
    """
    It export API key as string.

    Returns
    -------
    str
        API key string content.
    """

    return self.__content

It export API key as string.

Returns

str
API key string content.
def get_prefix(self: object | None = None) ‑> str
Expand source code
def get_prefix(self: object | None = None) -> str:
    """
    It return API key prefix.

    Returns
    -------
    str
        Prefix for the API key.
    """

    return "cxtk_"

It return API key prefix.

Returns

str
Prefix for the API key.
def get_raw_size(self: object | None = None) ‑> int
Expand source code
def get_raw_size(self: object | None = None) -> int:
    """
    It return lenght of the random bytes in the API key.

    Returns
    -------
    int
        Lenght of the random bytes in the API key.
    """

    return 128

It return lenght of the random bytes in the API key.

Returns

int
Lenght of the random bytes in the API key.
def get_size(self: object | None = None) ‑> int
Expand source code
def get_size(self: object | None = None) -> int:
    """
    It return lenght of the API key string, with prefix and random bytes
    converted to the hex string.

    Returns
    -------
    int
        Lenght of the API key string.
    """

    return len(apikey.get_prefix()) + apikey.get_raw_size() * 2

It return lenght of the API key string, with prefix and random bytes converted to the hex string.

Returns

int
Lenght of the API key string.