From 3fa8e9d3db0cfdf27206c247b009852c4b71c501 Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Tue, 30 Apr 2024 13:00:03 -0700 Subject: [PATCH] Troubleshooting response language handling --- neon_api_proxy/services/map_maker_api.py | 17 ++++++++++------- tests/test_map_maker_api.py | 6 +++--- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/neon_api_proxy/services/map_maker_api.py b/neon_api_proxy/services/map_maker_api.py index a146e27..f655d08 100644 --- a/neon_api_proxy/services/map_maker_api.py +++ b/neon_api_proxy/services/map_maker_api.py @@ -84,31 +84,34 @@ def handle_query(self, **kwargs) -> dict: if lat and lon: # Lookup address for coordinates try: - response = self._query_reverse(float(lat), float(lon)) + response = self._query_reverse(float(lat), float(lon), lang) except ValueError as e: return {"status_code": -1, "content": repr(e), "encoding": None} else: # Lookup coordinates for search term/address - response = self._query_geocode(address) + response = self._query_geocode(address, lang) self._last_query = time() - language = response.headers.get('Content-Language') - if language != lang: + resp_lang = response.headers.get('Content-Language') + if resp_lang != lang: # TODO: Translate? LOG.warning(f"Response not translated to {lang}") return {"status_code": response.status_code, "content": response.content, "encoding": response.encoding} - def _query_geocode(self, address: str) -> Response: - query_str = urllib.parse.urlencode({"q": address, + def _query_geocode(self, address: str, lang: str) -> Response: + self.session.headers["Content-Lanuage"] = lang + query_str = urllib.parse.urlencode({"q": address, "lang": lang, "api_key": self._api_key}) request_url = f"{self.geocode_url}?{query_str}" return self.get_with_cache_timeout(request_url, self.cache_timeout) - def _query_reverse(self, lat: float, lon: float): + def _query_reverse(self, lat: float, lon: float, lang: str): + self.session.headers["Content-Lanuage"] = lang query_str = urllib.parse.urlencode({"lat": lat, "lon": lon, + "lang": lang, "api_key": self._api_key}) request_url = f"{self.reverse_url}?{query_str}" return self.get_with_cache_timeout(request_url, self.cache_timeout) diff --git a/tests/test_map_maker_api.py b/tests/test_map_maker_api.py index a38ddda..7cc3c83 100644 --- a/tests/test_map_maker_api.py +++ b/tests/test_map_maker_api.py @@ -71,12 +71,12 @@ def test_geocode_lookup(self): delta=0.02) # Test language - valid_es_location = self.api.handle_query(address=VALID_ADDRESS, - lang_code="es-us") + valid_es_location = self.api.handle_query(address=VALID_ADDRESS_2, + lang_code="es") self.assertEqual(valid_es_location['status_code'], 200) self.assertEqual(valid_es_location["encoding"].lower(), "utf-8") es_location = json.loads(valid_es_location["content"])[0] - # self.assertNotEqual(valid_location, es_location) + self.assertNotEqual(valid_location, es_location) self.assertEqual(valid_location['lat'], es_location['lat'], es_location) self.assertEqual(valid_location['lon'], es_location['lon'], es_location)