Skip to content

Commit dea9439

Browse files
authored
Refactor backend services to accept more configuration params in init (#98)
Co-authored-by: Daniel McKnight <daniel@neon.ai>
1 parent ba62d24 commit dea9439

File tree

6 files changed

+13
-13
lines changed

6 files changed

+13
-13
lines changed

neon_api_proxy/controller.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ def _init_config() -> dict:
7171
"ngi_auth_vars.yml")
7272
if isfile(legacy_config_file):
7373
LOG.warning(f"Legacy configuration found at: {legacy_config_file}")
74-
return NGIConfig("ngi_auth_vars").get("api_services", {})
74+
return NGIConfig("ngi_auth_vars").get("api_services") or dict()
7575
else:
76-
return Configuration().get("keys", {}).get("api_services", {})
76+
return Configuration().get("keys", {}).get("api_services") or dict()
7777

7878
def init_service_instances(self, service_class_mapping: dict) -> dict:
7979
"""
@@ -86,14 +86,14 @@ def init_service_instances(self, service_class_mapping: dict) -> dict:
8686
"""
8787
service_mapping = dict()
8888
for item in service_class_mapping:
89-
api_key = self.config.get(item, {}).get("api_key") if self.config \
90-
else None
89+
service_config = self.config.get(item) or dict()
9190
try:
92-
if api_key is None and item != 'api_test_endpoint':
91+
if service_config.get("api_key") is None and item not in \
92+
('api_test_endpoint', "ip_api"):
9393
LOG.warning(f"No API key for {item} in "
9494
f"{list(self.config.keys())}")
9595
service_mapping[item] = \
96-
service_class_mapping[item](api_key=api_key)
96+
service_class_mapping[item](**service_config)
9797
except Exception as e:
9898
LOG.error(e)
9999
return service_mapping

neon_api_proxy/services/alpha_vantage_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ class AlphaVantageAPI(CachedAPI):
4848
API for querying Alpha Vantage.
4949
"""
5050

51-
def __init__(self, api_key: str = None):
51+
def __init__(self, api_key: str = None, cache_seconds: int = 300, **_):
5252
super().__init__("alpha_vantage")
5353
self._api_key = api_key or find_neon_alpha_vantage_key()
54-
self.quote_timeout = timedelta(minutes=5)
54+
self.quote_timeout = timedelta(seconds=cache_seconds)
5555

5656
def _search_symbol(self, query: str) -> dict:
5757
if not query:

neon_api_proxy/services/map_maker_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class MapMakerAPI(CachedAPI):
4242
API for querying My Maps API (geocoder.maps.co).
4343
"""
4444

45-
def __init__(self, api_key: str = None, cache_seconds=604800): # Cache week
45+
def __init__(self, api_key: str = None, cache_seconds: int = 604800, **_): # Cache week
4646
super().__init__("map_maker")
4747
self._api_key = api_key or getenv("MAP_MAKER_KEY")
4848
if not self._api_key:

neon_api_proxy/services/owm_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class OpenWeatherAPI(CachedAPI):
4141
API for querying Open Weather Map.
4242
"""
4343

44-
def __init__(self, api_key: str = None, cache_seconds=180):
44+
def __init__(self, api_key: str = None, cache_seconds: int = 900, **_):
4545
super().__init__("open_weather_map")
4646
self._api_key = api_key or find_neon_owm_key()
4747
self.cache_timeout = timedelta(seconds=cache_seconds)

neon_api_proxy/services/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232
class TestAPI(CachedAPI):
33-
def __init__(self, api_key: str = None):
33+
def __init__(self, api_key: str = None, **_):
3434
super().__init__("Test")
3535

3636
def handle_query(self, **kwargs) -> dict:

neon_api_proxy/services/wolfram_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ class WolframAPI(CachedAPI):
5151
API for querying Wolfram|Alpha.
5252
"""
5353

54-
def __init__(self, api_key: str = None):
54+
def __init__(self, api_key: str = None, cache_seconds: int = 3600, **_):
5555
super().__init__("wolfram")
5656
self._api_key = api_key or find_neon_wolfram_key()
5757
self.session.allowable_codes = (200, 501)
58-
self.cache_time = timedelta(minutes=60)
58+
self.cache_time = timedelta(seconds=cache_seconds)
5959

6060
def _build_query_url(self, query_type: QueryUrl, query_arg: str) -> str:
6161
"""

0 commit comments

Comments
 (0)