From 49691b1ae473a054661250374ff42af1622793b8 Mon Sep 17 00:00:00 2001 From: Scott Olson Date: Wed, 25 Aug 2021 15:09:40 -0500 Subject: [PATCH 1/2] Allow url config by env vars --- sentera/configuration.py | 42 +++++++++++++---------------- sentera/tests/test_configuration.py | 18 +++++++++++++ 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/sentera/configuration.py b/sentera/configuration.py index 3e99997..108eaaf 100644 --- a/sentera/configuration.py +++ b/sentera/configuration.py @@ -2,29 +2,8 @@ import os -ENVIRONMENT_CONFIGS = { - "dev": { - "sentera_api_url": "https://apidev.sentera.com", - "weather_api_url": "https://weatherdev.sentera.com", - }, - "prod": { - "sentera_api_url": "https://api.sentera.com", - "weather_api_url": "https://weather.sentera.com", - }, - "staging": { - "sentera_api_url": "https://apistaging.sentera.com", - "weather_api_url": "https://weatherstaging.sentera.com", - }, - "staging2": { - "sentera_api_url": "https://apistaging2.sentera.com", - "weather_api_url": "https://weatherstaging2.sentera.com", - }, - "test": { - # This environment is intended to be used by tests and is not a real environment. - "sentera_api_url": "https://apitest.sentera.com", - "weather_api_url": "https://weathertest.sentera.com", - }, -} +ENV_SENTERA_API_URL = "SENTERA_API_URL" +ENV_WEATHER_API_URL = "WEATHER_API_URL" class Configuration: @@ -47,7 +26,22 @@ def __init__(self, environment=None): environment = environment.lower() self.environment = environment - self.config = ENVIRONMENT_CONFIGS[environment] + if self.environment == "prod": + self.config = { + "sentera_api_url": "https://api.sentera.com", + "weather_api_url": "https://weather.sentera.com", + } + else: + self.config = { + "sentera_api_url": f"https://api{self.environment}.sentera.com", + "weather_api_url": f"https://weather{self.environment}.sentera.com", + } + + if ENV_SENTERA_API_URL in os.environ: + self.config["sentera_api_url"] = os.environ.get(ENV_SENTERA_API_URL) + + if ENV_WEATHER_API_URL in os.environ: + self.config["weather_api_url"] = os.environ.get(ENV_WEATHER_API_URL) def sentera_api_url(self, path): """ diff --git a/sentera/tests/test_configuration.py b/sentera/tests/test_configuration.py index 61fb708..632a4f1 100644 --- a/sentera/tests/test_configuration.py +++ b/sentera/tests/test_configuration.py @@ -8,6 +8,7 @@ def test_default_configuration(monkeypatch): configuration = Configuration() assert configuration.environment == "prod" assert configuration.sentera_api_url("/") == "https://api.sentera.com/" + assert configuration.weather_api_url("/") == "https://weather.sentera.com/" def test_development_configuration(monkeypatch): @@ -15,6 +16,23 @@ def test_development_configuration(monkeypatch): configuration = Configuration() assert configuration.environment == "dev" assert configuration.sentera_api_url("/") == "https://apidev.sentera.com/" + assert configuration.weather_api_url("/") == "https://weatherdev.sentera.com/" + + +def test_env_sentera_api_url_configuration(monkeypatch): + monkeypatch.setenv("SENTERA_API_URL", "https://example.com") + configuration = Configuration() + assert configuration.environment == "test" + assert configuration.sentera_api_url("/") == "https://example.com/" + assert configuration.weather_api_url("/") == "https://weathertest.sentera.com/" + + +def test_env_weather_api_url_configuration(monkeypatch): + monkeypatch.setenv("WEATHER_API_URL", "https://weatherkazoo.sentera.com") + configuration = Configuration() + assert configuration.environment == "test" + assert configuration.sentera_api_url("/") == "https://apitest.sentera.com/" + assert configuration.weather_api_url("/") == "https://weatherkazoo.sentera.com/" def test_argument_configuration(monkeypatch): From ac6493059cbf0e1a46b7ac93d87ac48372bcd23d Mon Sep 17 00:00:00 2001 From: Scott Olson Date: Wed, 25 Aug 2021 15:13:24 -0500 Subject: [PATCH 2/2] Version bump --- sentera/_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentera/_version.py b/sentera/_version.py index 46ac708..af0bd8d 100644 --- a/sentera/_version.py +++ b/sentera/_version.py @@ -1,3 +1,3 @@ """Defines package version. Parsed by setup.py and imported by __init__.py.""" -__version__ = "2.6.2" +__version__ = "2.6.3"