Escape semicolons at Mapbox batch

This commit is contained in:
Juan Ignacio Sánchez Lara 2018-07-09 18:17:45 +02:00
parent 40ace9cfaa
commit cce5f92312
2 changed files with 21 additions and 1 deletions

View File

@ -113,7 +113,7 @@ class MapboxGeocoder(Traceable):
:return: list of [x, y] on success, [] on error :return: list of [x, y] on success, [] on error
""" """
try: try:
free_search = ';'.join(free_searches) free_search = ';'.join([self._escape(fs) for fs in free_searches])
self._logger.debug('--> free search: {}'.format(free_search)) self._logger.debug('--> free search: {}'.format(free_search))
response = self._geocoder.forward(address=free_search.decode('utf-8'), response = self._geocoder.forward(address=free_search.decode('utf-8'),
country=country, country=country,
@ -139,3 +139,10 @@ class MapboxGeocoder(Traceable):
self._logger.error('Error connecting to Mapbox geocoding server', self._logger.error('Error connecting to Mapbox geocoding server',
exception=ce) exception=ce)
return [] return []
def _escape(self, free_search):
# Semicolon is used to separate batch geocoding; there's no documented
# way to pass actual semicolons, and %3B or &#59; won't work (check
# TestBulkStreetFunctions.test_semicolon and the docs,
# https://www.mapbox.com/api-documentation/#batch-requests)
return free_search.replace(';', ',')

View File

@ -262,6 +262,19 @@ class TestBulkStreetFunctions(TestStreetFunctionsSetUp):
response = self._run_authenticated(query) response = self._run_authenticated(query)
assert_equal(1, len(response['rows'])) assert_equal(1, len(response['rows']))
def test_semicolon(self):
query = "select *, st_x(the_geom), st_y(the_geom) " \
"FROM cdb_dataservices_client.cdb_bulk_geocode_street_point( " \
"'select * from jsonb_to_recordset(''[" \
"{\"cartodb_id\": 1, \"address\": \"1900 amphitheatre parkway; mountain view; ca; us\"}," \
"{\"cartodb_id\": 2, \"address\": \"1900 amphitheatre parkway, mountain view, ca, us\"}" \
"]''::jsonb) as (cartodb_id integer, address text)', " \
"'address', null, null, null)"
response = self._run_authenticated(query)
x_y_by_cartodb_id = self._x_y_by_cartodb_id(response)
assert_equal(x_y_by_cartodb_id[1], x_y_by_cartodb_id[2])
def _run_authenticated(self, query): def _run_authenticated(self, query):
authenticated_query = "{}&api_key={}".format(query, authenticated_query = "{}&api_key={}".format(query,
self.env_variables[ self.env_variables[