"""BusinessObjects connection settings loaded from ``.env`` files."""
import os
from pathlib import Path
from typing import Optional, Dict
from dotenv import load_dotenv
[docs]
def load_bo_config(env: Optional[str] = None) -> Dict[str, Optional[str]]:
"""Load BusinessObjects connection settings from ``.env`` files.
Values are read from ``~/.env`` then the local ``.env``, the latter taking
precedence. Expected variables: ``BO_BASE_URL``, ``BO_USER``,
``BO_PASSWORD`` and optionally ``BO_AUTH_TYPE`` (default ``secEnterprise``).
When ``env`` is given, the suffixed variant (e.g. ``BO_BASE_URL_PROD``) is
used if present, otherwise the unsuffixed variable.
:param env: Optional environment suffix (e.g. 'dev', 'prod')
:type env: Optional[str]
:return: Mapping with keys base_url, user, password, auth_type
:rtype: Dict[str, Optional[str]]
"""
load_dotenv(Path.home() / '.env')
load_dotenv()
def _get(key: str, default: Optional[str] = None) -> Optional[str]:
if env:
suffixed = os.getenv(f"{key}_{env.upper()}")
if suffixed is not None:
return suffixed
return os.getenv(key, default)
return {
'base_url': _get('BO_BASE_URL'),
'user': _get('BO_USER'),
'password': _get('BO_PASSWORD'),
'auth_type': _get('BO_AUTH_TYPE', 'secEnterprise'),
}