|
| 1 | +from __future__ import ( |
| 2 | + annotations, |
| 3 | +) |
| 4 | + |
| 5 | +import abc |
| 6 | +import collections |
| 7 | +import os |
| 8 | +import typing as t |
| 9 | +from distutils import ( |
| 10 | + util, |
| 11 | +) |
| 12 | +from pathlib import ( |
| 13 | + Path, |
| 14 | +) |
| 15 | + |
| 16 | +import yaml |
| 17 | + |
| 18 | +from .exceptions import ( |
| 19 | + ApiGatewayConfigException, |
| 20 | +) |
| 21 | + |
| 22 | +REST = collections.namedtuple("Rest", "host port cors") |
| 23 | +DISCOVERY = collections.namedtuple("Discovery", "host port") |
| 24 | +CORS = collections.namedtuple("Cors", "enabled") |
| 25 | + |
| 26 | +_ENVIRONMENT_MAPPER = { |
| 27 | + "rest.host": "API_GATEWAY_REST_HOST", |
| 28 | + "rest.port": "API_GATEWAY_REST_PORT", |
| 29 | + "rest.cors.enabled": "API_GATEWAY_REST_CORS_ENABLED", |
| 30 | + "discovery.host": "API_GATEWAY_DISCOVERY_HOST", |
| 31 | + "discovery.port": "API_GATEWAY_DISCOVERY_PORT", |
| 32 | +} |
| 33 | + |
| 34 | +_PARAMETERIZED_MAPPER = { |
| 35 | + "rest.host": "api_gateway_rest_host", |
| 36 | + "rest.port": "api_gateway_rest_port", |
| 37 | + "rest.cors.enabled": "api_gateway_rest_cors_enabled", |
| 38 | + "discovery.host": "api_gateway_discovery_host", |
| 39 | + "discovery.port": "api_gateway_discovery_port", |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +class ApiGatewayConfig(abc.ABC): |
| 44 | + """Api Gateway config class.""" |
| 45 | + |
| 46 | + __slots__ = ("_services", "_path", "_data", "_with_environment", "_parameterized") |
| 47 | + |
| 48 | + def __init__(self, path: t.Union[Path, str], with_environment: bool = True, **kwargs): |
| 49 | + if isinstance(path, Path): |
| 50 | + path = str(path) |
| 51 | + self._services = {} |
| 52 | + self._path = path |
| 53 | + self._load(path) |
| 54 | + self._with_environment = with_environment |
| 55 | + self._parameterized = kwargs |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def _file_exit(path: str) -> bool: |
| 59 | + if os.path.isfile(path): |
| 60 | + return True |
| 61 | + return False |
| 62 | + |
| 63 | + def _load(self, path): |
| 64 | + if self._file_exit(path): |
| 65 | + with open(path) as f: |
| 66 | + self._data = yaml.load(f, Loader=yaml.FullLoader) |
| 67 | + else: |
| 68 | + raise ApiGatewayConfigException(f"Check if this path: {path} is correct") |
| 69 | + |
| 70 | + def _get(self, key: str, **kwargs: t.Any) -> t.Any: |
| 71 | + if key in _PARAMETERIZED_MAPPER and _PARAMETERIZED_MAPPER[key] in self._parameterized: |
| 72 | + return self._parameterized[_PARAMETERIZED_MAPPER[key]] |
| 73 | + |
| 74 | + if self._with_environment and key in _ENVIRONMENT_MAPPER and _ENVIRONMENT_MAPPER[key] in os.environ: |
| 75 | + if os.environ[_ENVIRONMENT_MAPPER[key]] in ["true", "True", "false", "False"]: |
| 76 | + return bool(util.strtobool(os.environ[_ENVIRONMENT_MAPPER[key]])) |
| 77 | + return os.environ[_ENVIRONMENT_MAPPER[key]] |
| 78 | + |
| 79 | + def _fn(k: str, data: dict[str, t.Any]) -> t.Any: |
| 80 | + current, _, following = k.partition(".") |
| 81 | + |
| 82 | + part = data[current] |
| 83 | + if not following: |
| 84 | + return part |
| 85 | + |
| 86 | + return _fn(following, part) |
| 87 | + |
| 88 | + return _fn(key, self._data) |
| 89 | + |
| 90 | + @property |
| 91 | + def rest(self) -> REST: |
| 92 | + """Get the rest config. |
| 93 | +
|
| 94 | + :return: A ``REST`` NamedTuple instance. |
| 95 | + """ |
| 96 | + return REST(host=self._get("rest.host"), port=int(self._get("rest.port")), cors=self._cors) |
| 97 | + |
| 98 | + @property |
| 99 | + def _cors(self) -> CORS: |
| 100 | + """Get the cors config. |
| 101 | +
|
| 102 | + :return: A ``CORS`` NamedTuple instance. |
| 103 | + """ |
| 104 | + return CORS(enabled=self._get("rest.cors.enabled")) |
| 105 | + |
| 106 | + @property |
| 107 | + def discovery(self) -> DISCOVERY: |
| 108 | + """Get the rest config. |
| 109 | +
|
| 110 | + :return: A ``REST`` NamedTuple instance. |
| 111 | + """ |
| 112 | + return DISCOVERY(host=self._get("discovery.host"), port=int(self._get("discovery.port"))) |
0 commit comments