Skip to content

Commit

Permalink
Add language handling to OWM proxy service with unit test (#93)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel McKnight <[email protected]>
  • Loading branch information
NeonDaniel and NeonDaniel authored Apr 9, 2024
1 parent 39ddf44 commit c4ac862
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions neon_api_proxy/api_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def handle_api_input(self,
LOG.info(f"request={request}")

respond = self.proxy.resolve_query(request)
LOG.info(f"message={message_id} "
f"status={respond.get('status_code')}")
LOG.debug(f"response message={message_id} "
f"status={respond.get('status_code')}")

try:
respond['content'] = bytes(respond.get('content', b'')).\
Expand Down Expand Up @@ -121,7 +121,7 @@ def extract_agent_tokens(msg_data: dict) -> dict:
tokens['message_id'] = tokens['replied_message'] = \
msg_data.get('messageID', None)
else:
LOG.warning('Failed to resolve an agent from the message data')
LOG.debug('No valid agent specified in the message data')
return tokens

def handle_error(self, thread, exception):
Expand Down
8 changes: 5 additions & 3 deletions neon_api_proxy/services/owm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ def handle_query(self, **kwargs) -> dict:
lat = kwargs.get("lat")
lng = kwargs.get("lng", kwargs.get("lon"))
api = kwargs.get('api') or "onecall"
lang = kwargs.get('lang') or "en"
units = "metric" if kwargs.get("units") == "metric" else "imperial"

if not all((lat, lng, units)):
return {"status_code": -1,
"content": f"Missing required args in: {kwargs}",
"encoding": None}
try:
resp = self._get_api_response(lat, lng, units, api)
resp = self._get_api_response(lat, lng, units, api, lang)
except Exception as e:
return {"status_code": -1,
"content": repr(e),
Expand All @@ -79,14 +80,15 @@ def handle_query(self, **kwargs) -> dict:
"encoding": resp.encoding}

def _get_api_response(self, lat: str, lng: str, units: str,
api: str = "onecall") -> Response:
api: str = "onecall", lang: str = "en") -> Response:
str(float(lat))
str(float(lng))
assert units in ("metric", "imperial", "standard")
query_params = {"lat": lat,
"lon": lng,
"appid": self._api_key,
"units": units}
"units": units,
"lang": lang}
query_str = urllib.parse.urlencode(query_params)
base_url = "http://api.openweathermap.org/data/2.5"
resp = self.get_with_cache_timeout(f"{base_url}/{api}?{query_str}",
Expand Down
7 changes: 7 additions & 0 deletions tests/test_owm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ def test_handle_query_valid_onecall(self):
self.assertEqual(resp["encoding"], "utf-8")
self.assertIsInstance(json.loads(resp["content"]), dict)

spanish = self.api.handle_query(**VALID_QUERY_ONECALL, lang="es")
self.assertIsInstance(spanish, dict)
self.assertEqual(spanish["status_code"], 200)
self.assertEqual(spanish["encoding"], "utf-8")
self.assertIsInstance(json.loads(spanish["content"]), dict)
self.assertNotEqual(spanish, resp)

def test_handle_query_valid_current(self):
resp = self.api.handle_query(**VALID_QUERY_CURRENT)
self.assertIsInstance(resp, dict)
Expand Down

0 comments on commit c4ac862

Please sign in to comment.