Static Client Configuration from Environment Variables

CoreConfig

Source code in client_aic/config/core_config.py
class CoreConfig:
    """CoreConfig"""

    def __init__(self):
        """
        __init__

        contstructor
        """
        self.cfg = {
            "user": get_creds.get_creds(),
            "tls": get_tls.get_tls(),
            "endpoint": get_api_address.get_api_address(),
        }

    def get_cfg(self):
        """
        get_cfg

        get the internal dictionary

        :returns: dictionary **self.cfg**
        :rtype: dict
        """
        if self.cfg:
            return self.cfg
        else:
            log.debug("config={}", self.cfg)
            return {
                "user": get_creds.get_creds(),
                "tls": get_tls.get_tls(),
                "endpoint": get_api_address.get_api_address(),
            }

    def get_endpoint(self):
        """
        get_endpoint

        get the api endpoint address

        :returns: dictionary **self.cfg**
        :rtype: dict
        """
        if self.cfg:
            return self.cfg.get(
                "endpoint",
                get_api_address.get_api_address(),
            )
        else:
            return get_api_address.get_api_address()

    def show(self):
        """
        show

        helper function for debugging
        """
        log.debug("config={}", self.cfg)

__init__()

init

contstructor

client_aic/config/core_config.py
def __init__(self):
    """
    __init__

    contstructor
    """
    self.cfg = {
        "user": get_creds.get_creds(),
        "tls": get_tls.get_tls(),
        "endpoint": get_api_address.get_api_address(),
    }

get_cfg()

get_cfg

get the internal dictionary

Returns:
  • dict

    dictionary self.cfg

client_aic/config/core_config.py
def get_cfg(self):
    """
    get_cfg

    get the internal dictionary

    :returns: dictionary **self.cfg**
    :rtype: dict
    """
    if self.cfg:
        return self.cfg
    else:
        log.debug("config={}", self.cfg)
        return {
            "user": get_creds.get_creds(),
            "tls": get_tls.get_tls(),
            "endpoint": get_api_address.get_api_address(),
        }

get_endpoint()

get_endpoint

get the api endpoint address

Returns:
  • dict

    dictionary self.cfg

client_aic/config/core_config.py
def get_endpoint(self):
    """
    get_endpoint

    get the api endpoint address

    :returns: dictionary **self.cfg**
    :rtype: dict
    """
    if self.cfg:
        return self.cfg.get(
            "endpoint",
            get_api_address.get_api_address(),
        )
    else:
        return get_api_address.get_api_address()

show()

show

helper function for debugging

client_aic/config/core_config.py
def show(self):
    """
    show

    helper function for debugging
    """
    log.debug("config={}", self.cfg)

Helpers

get_api_address

get the api address for the job backend

Returns:
  • str

    string for the api endpoint address

client_aic/config/get_api_address.py
def get_api_address():
    """
    get_api_address

    get the api address for the job backend

    :returns: string for the api endpoint address
    :rtype: str
    """
    if os.getenv("USE_LOCAL", "0") == "1":
        return os.getenv("AI_API", "0.0.0.0:3000")
    else:
        env_name = os.getenv("AI_ENV", "dev")
        base_url = os.getenv("AI_API", "api.redten.io")
        return f"{base_url}/v1/{env_name}"

get_creds

get the rest api credentials as a dictionary

Returns:
  • dict

    dict for the user credentials

client_aic/config/get_creds.py
def get_creds():
    """
    get_creds

    get the rest api credentials
    as a dictionary

    :returns: dict for the user credentials
    :rtype: dict
    """
    return {
        "u": os.getenv("AI_USER", None),
        "p": os.getenv("AI_PASSWORD", None),
        "e": os.getenv("AI_EMAIL", None),
    }

get_tls

return a dictionary for the ca, cert and key from environment variables:

  • AI_API_CA
  • AI_API_CERT
  • AI_API_KEY
Returns:
  • dict

    dict for any self-signed tls assets

client_aic/config/get_tls.py
def get_tls():
    """
    get_tls

    return a dictionary for the ca, cert and key from
    environment variables:

    - AI_API_CA
    - AI_API_CERT
    - AI_API_KEY

    :returns: dict for any self-signed tls assets
    :rtype: dict
    """
    if os.getenv("USE_LOCAL", "0") == "1":
        return {
            "ca": os.getenv("AI_API_CA", None),
            "cert": os.getenv(
                "AI_API_CERT",
                "./tls/api/server.pem",
            ),
            "key": os.getenv(
                "AI_API_KEY",
                ",/tls/api/server-key.pem",
            ),
        }
    else:
        return {
            "ca": os.getenv("AI_API_CA", None),
            "cert": os.getenv("AI_API_CERT", None),
            "key": os.getenv("AI_API_KEY", None),
        }