Fixed mapbox requests and responses
This commit is contained in:
@@ -8,6 +8,7 @@ from mapbox import Geocoder
|
|||||||
from cartodb_services.metrics import Traceable
|
from cartodb_services.metrics import Traceable
|
||||||
from cartodb_services.tools.exceptions import ServiceException
|
from cartodb_services.tools.exceptions import ServiceException
|
||||||
from cartodb_services.tools.qps import qps_retry
|
from cartodb_services.tools.qps import qps_retry
|
||||||
|
from cartodb_services.tools.normalize import normalize
|
||||||
|
|
||||||
GEOCODER_NAME = 'geocoder_name'
|
GEOCODER_NAME = 'geocoder_name'
|
||||||
EPHEMERAL_GEOCODER = 'mapbox.places'
|
EPHEMERAL_GEOCODER = 'mapbox.places'
|
||||||
@@ -39,10 +40,16 @@ class MapboxGeocoder(Traceable):
|
|||||||
def _parse_geocoder_response(self, response):
|
def _parse_geocoder_response(self, response):
|
||||||
json_response = json.loads(response)
|
json_response = json.loads(response)
|
||||||
|
|
||||||
if json_response and json_response[ENTRY_FEATURES]:
|
# If Mapbox returns more that one result, take the first one
|
||||||
feature = json_response[ENTRY_FEATURES][0]
|
if json_response:
|
||||||
|
if type(json_response) == list:
|
||||||
|
json_response = json_response[0]
|
||||||
|
|
||||||
return self._extract_lng_lat_from_feature(feature)
|
if json_response[ENTRY_FEATURES]:
|
||||||
|
feature = json_response[ENTRY_FEATURES][0]
|
||||||
|
return self._extract_lng_lat_from_feature(feature)
|
||||||
|
else:
|
||||||
|
return []
|
||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -61,11 +68,11 @@ class MapboxGeocoder(Traceable):
|
|||||||
def geocode(self, searchtext, city=None, state_province=None,
|
def geocode(self, searchtext, city=None, state_province=None,
|
||||||
country=None):
|
country=None):
|
||||||
if searchtext and searchtext.strip():
|
if searchtext and searchtext.strip():
|
||||||
address = [searchtext]
|
address = [normalize(searchtext)]
|
||||||
if city:
|
if city:
|
||||||
address.append(city)
|
address.append(normalize(city))
|
||||||
if state_province:
|
if state_province:
|
||||||
address.append(state_province)
|
address.append(normalize(state_province))
|
||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
def normalize(str_input):
|
||||||
|
return str_input.replace('"', '"') \
|
||||||
|
.replace(';', ',')
|
||||||
@@ -35,6 +35,11 @@ class MapboxGeocoderTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
assert place
|
assert place
|
||||||
|
|
||||||
|
def test_odd_characters(self):
|
||||||
|
place = self.geocoder.geocode(searchtext='Barcelona; "Spain"')
|
||||||
|
|
||||||
|
assert place
|
||||||
|
|
||||||
def test_empty_request(self):
|
def test_empty_request(self):
|
||||||
place = self.geocoder.geocode(searchtext='', country=None, city=None, state_province=None)
|
place = self.geocoder.geocode(searchtext='', country=None, city=None, state_province=None)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user