Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor backend services to accept more configuration params in init #98

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions neon_api_proxy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions neon_api_proxy/services/alpha_vantage_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, **_):
NeonDaniel marked this conversation as resolved.
Show resolved Hide resolved
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:
Expand Down
2 changes: 1 addition & 1 deletion neon_api_proxy/services/map_maker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion neon_api_proxy/services/owm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion neon_api_proxy/services/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions neon_api_proxy/services/wolfram_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
Loading