From 9039c328dea9446b8ae50bc24bc505395d05b517 Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Wed, 7 Aug 2024 18:12:03 -0700 Subject: [PATCH] Refactor backend services to accept more configuration params in init --- neon_api_proxy/controller.py | 12 ++++++------ neon_api_proxy/services/alpha_vantage_api.py | 4 ++-- neon_api_proxy/services/map_maker_api.py | 2 +- neon_api_proxy/services/owm_api.py | 2 +- neon_api_proxy/services/test_api.py | 2 +- neon_api_proxy/services/wolfram_api.py | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/neon_api_proxy/controller.py b/neon_api_proxy/controller.py index 172e4cd..9e6414a 100644 --- a/neon_api_proxy/controller.py +++ b/neon_api_proxy/controller.py @@ -71,9 +71,9 @@ def _init_config() -> dict: "ngi_auth_vars.yml") if isfile(legacy_config_file): LOG.warning(f"Legacy configuration found at: {legacy_config_file}") - return NGIConfig("ngi_auth_vars").get("api_services", {}) + return NGIConfig("ngi_auth_vars").get("api_services") or dict() else: - return Configuration().get("keys", {}).get("api_services", {}) + return Configuration().get("keys", {}).get("api_services") or dict() def init_service_instances(self, service_class_mapping: dict) -> dict: """ @@ -86,14 +86,14 @@ def init_service_instances(self, service_class_mapping: dict) -> dict: """ service_mapping = dict() for item in service_class_mapping: - api_key = self.config.get(item, {}).get("api_key") if self.config \ - else None + service_config = self.config.get(item) or dict() try: - if api_key is None and item != 'api_test_endpoint': + if service_config.get("api_key") is None and item not in \ + ('api_test_endpoint', "ip_api"): LOG.warning(f"No API key for {item} in " f"{list(self.config.keys())}") service_mapping[item] = \ - service_class_mapping[item](api_key=api_key) + service_class_mapping[item](**service_config) except Exception as e: LOG.error(e) return service_mapping diff --git a/neon_api_proxy/services/alpha_vantage_api.py b/neon_api_proxy/services/alpha_vantage_api.py index 4549967..f4cb4c9 100644 --- a/neon_api_proxy/services/alpha_vantage_api.py +++ b/neon_api_proxy/services/alpha_vantage_api.py @@ -48,10 +48,10 @@ class AlphaVantageAPI(CachedAPI): API for querying Alpha Vantage. """ - def __init__(self, api_key: str = None): + def __init__(self, api_key: str = None, cache_seconds: int = 300, **_): super().__init__("alpha_vantage") self._api_key = api_key or find_neon_alpha_vantage_key() - self.quote_timeout = timedelta(minutes=5) + self.quote_timeout = timedelta(seconds=cache_seconds) def _search_symbol(self, query: str) -> dict: if not query: diff --git a/neon_api_proxy/services/map_maker_api.py b/neon_api_proxy/services/map_maker_api.py index 96dc404..13dad14 100644 --- a/neon_api_proxy/services/map_maker_api.py +++ b/neon_api_proxy/services/map_maker_api.py @@ -42,7 +42,7 @@ class MapMakerAPI(CachedAPI): API for querying My Maps API (geocoder.maps.co). """ - def __init__(self, api_key: str = None, cache_seconds=604800): # Cache week + def __init__(self, api_key: str = None, cache_seconds: int = 604800, **_): # Cache week super().__init__("map_maker") self._api_key = api_key or getenv("MAP_MAKER_KEY") if not self._api_key: diff --git a/neon_api_proxy/services/owm_api.py b/neon_api_proxy/services/owm_api.py index f25fa30..dc915ff 100644 --- a/neon_api_proxy/services/owm_api.py +++ b/neon_api_proxy/services/owm_api.py @@ -41,7 +41,7 @@ class OpenWeatherAPI(CachedAPI): API for querying Open Weather Map. """ - def __init__(self, api_key: str = None, cache_seconds=180): + def __init__(self, api_key: str = None, cache_seconds: int = 900, **_): super().__init__("open_weather_map") self._api_key = api_key or find_neon_owm_key() self.cache_timeout = timedelta(seconds=cache_seconds) diff --git a/neon_api_proxy/services/test_api.py b/neon_api_proxy/services/test_api.py index 1e118b4..14af156 100644 --- a/neon_api_proxy/services/test_api.py +++ b/neon_api_proxy/services/test_api.py @@ -30,7 +30,7 @@ class TestAPI(CachedAPI): - def __init__(self, api_key: str = None): + def __init__(self, api_key: str = None, **_): super().__init__("Test") def handle_query(self, **kwargs) -> dict: diff --git a/neon_api_proxy/services/wolfram_api.py b/neon_api_proxy/services/wolfram_api.py index 175c536..518d7cc 100644 --- a/neon_api_proxy/services/wolfram_api.py +++ b/neon_api_proxy/services/wolfram_api.py @@ -51,11 +51,11 @@ class WolframAPI(CachedAPI): API for querying Wolfram|Alpha. """ - def __init__(self, api_key: str = None): + def __init__(self, api_key: str = None, cache_seconds: int = 3600, **_): super().__init__("wolfram") self._api_key = api_key or find_neon_wolfram_key() self.session.allowable_codes = (200, 501) - self.cache_time = timedelta(minutes=60) + self.cache_time = timedelta(seconds=cache_seconds) def _build_query_url(self, query_type: QueryUrl, query_arg: str) -> str: """