Skip to content

Commit

Permalink
Troubleshooting response language handling
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel committed Apr 30, 2024
1 parent a204694 commit 3fa8e9d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
17 changes: 10 additions & 7 deletions neon_api_proxy/services/map_maker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 3 additions & 3 deletions tests/test_map_maker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 3fa8e9d

Please sign in to comment.