Skip to content

Commit

Permalink
Add MapMaker API langauge support with updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel committed Apr 30, 2024
1 parent 031ff19 commit e333186
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 7 additions & 5 deletions neon_api_proxy/services/map_maker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def handle_query(self, **kwargs) -> dict:
lat = kwargs.get("lat")
lon = kwargs.get("lon", kwargs.get("lng"))
address = kwargs.get('address')

lang = kwargs.get('lang_code', "en")
if not (address or (lat and lon)):
# Missing data for lookup
return {"status_code": -1,
Expand All @@ -83,26 +83,28 @@ 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()
return {"status_code": response.status_code,
"content": response.content,
"encoding": response.encoding}

def _query_geocode(self, address: str) -> Response:
def _query_geocode(self, address: str, lang: str) -> Response:
self.session.headers['Accept-Language'] = lang
query_str = urllib.parse.urlencode({"q": address,
"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['Accept-Language'] = lang
query_str = urllib.parse.urlencode({"lat": lat, "lon": lon,
"api_key": self._api_key})
request_url = f"{self.reverse_url}?{query_str}"
Expand Down
16 changes: 16 additions & 0 deletions tests/test_map_maker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ def test_geocode_lookup(self):
self.assertAlmostEqual(float(valid_location['lon']), -115.17,
delta=0.02)

# Test language
valid_es_location = self.api.handle_query(address=VALID_ADDRESS,
lang_code="es-mx")
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)

invalid_response = self.api.handle_query(address=INVALID_ADDRESS)
self.assertEqual(invalid_response['status_code'], -1)

Expand All @@ -81,6 +89,14 @@ def test_reverse_lookup(self):
self.assertEqual(valid_location['state'], "Washington", valid_location)
self.assertEqual(valid_location['town'], "Renton", valid_location)

# Test language
valid_es_location = self.api.handle_query(lat=VALID_LAT, lon=VALID_LON,
lang_code="es-mx")
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"])['address']
self.assertNotEqual(valid_location, es_location)

invalid_response = self.api.handle_query(lat=VALID_LAT, lon=None)
self.assertEqual(invalid_response['status_code'], -1)

Expand Down

0 comments on commit e333186

Please sign in to comment.