Skip to content

Commit

Permalink
Merge pull request #29 from SenteraLLC/fa-2215-custom-weather-api
Browse files Browse the repository at this point in the history
[FA-2521] Allow url config by env vars
  • Loading branch information
Olson3R authored Sep 7, 2021
2 parents de7a520 + ac64930 commit 84aef7d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion sentera/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Defines package version. Parsed by setup.py and imported by __init__.py."""

__version__ = "2.6.2"
__version__ = "2.6.3"
42 changes: 18 additions & 24 deletions sentera/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
"""
Expand Down
18 changes: 18 additions & 0 deletions sentera/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,31 @@ 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):
monkeypatch.setenv("SENTERA_ENV", "dev")
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):
Expand Down

0 comments on commit 84aef7d

Please sign in to comment.