Added CR suggestions

This commit is contained in:
Antonio
2018-01-09 16:21:55 +01:00
parent 3bbb3c6bcc
commit dd6ad0119c
10 changed files with 170 additions and 64 deletions

View File

@@ -38,9 +38,13 @@ class MapboxGeocoder(Traceable):
def _parse_geocoder_response(self, response):
json_response = json.loads(response)
feature = json_response[ENTRY_FEATURES][0]
return self._extract_lng_lat_from_feature(feature)
if json_response:
feature = json_response[ENTRY_FEATURES][0]
return self._extract_lng_lat_from_feature(feature)
else:
return []
def _extract_lng_lat_from_feature(self, feature):
geometry = feature[ENTRY_GEOMETRY]
@@ -62,11 +66,26 @@ class MapboxGeocoder(Traceable):
if state_province:
address.append(state_province)
response = self._geocoder.forward(address=', '.join(address),
country=country,
limit=1)
try:
response = self._geocoder.forward(address=', '.join(address),
country=country,
limit=1)
if response.status_code == requests.codes.ok:
return self._parse_geocoder_response(response.text)
else:
raise ServiceException(response.status_code, response)
if response.status_code == requests.codes.ok:
return self._parse_geocoder_response(response.text)
elif response.status_code == requests.codes.bad_request:
return []
else:
raise ServiceException(response.status_code, response)
except requests.Timeout as te:
# In case of timeout we want to stop the job because the server
# could be down
self._logger.error('Timeout connecting to Mapbox geocoding server',
te)
raise ServiceException('Error geocoding {0} using Mapbox'.format(
searchtext), None)
except requests.ConnectionError as ce:
# Don't raise the exception to continue with the geocoding job
self._logger.error('Error connecting to Mapbox geocoding server',
exception=ce)
return []

View File

@@ -64,9 +64,25 @@ class MapboxMatrixClient(Traceable):
coords = marshall_coordinates(coordinates)
uri = self._uri(coords, profile)
response = requests.get(uri)
if response.status_code == requests.codes.ok:
return response.text
else:
raise ServiceException(response.status_code, response)
try:
response = requests.get(uri)
if response.status_code == requests.codes.ok:
return response.text
elif response.status_code == requests.codes.bad_request:
return '{}'
else:
raise ServiceException(response.status_code, response)
except requests.Timeout as te:
# In case of timeout we want to stop the job because the server
# could be down
self._logger.error('Timeout connecting to Mapbox matrix service',
te)
raise ServiceException('Error getting matrix data from Mapbox',
None)
except requests.ConnectionError as ce:
# Don't raise the exception to continue with the geocoding job
self._logger.error('Error connecting to Mapbox matrix service',
exception=ce)
return '{}'

View File

@@ -63,13 +63,17 @@ class MapboxRouting(Traceable):
def _parse_routing_response(self, response):
json_response = json.loads(response)
route = json_response[ENTRY_ROUTES][0] # Force the first route
geometry = PolyLine().decode(route[ENTRY_GEOMETRY])
distance = route[ENTRY_DISTANCE]
duration = route[ENTRY_DURATION]
if json_response:
route = json_response[ENTRY_ROUTES][0] # Force the first route
return MapboxRoutingResponse(geometry, distance, duration)
geometry = PolyLine().decode(route[ENTRY_GEOMETRY])
distance = route[ENTRY_DISTANCE]
duration = route[ENTRY_DURATION]
return MapboxRoutingResponse(geometry, distance, duration)
else:
return MapboxRoutingResponse(None, None, None)
@qps_retry(qps=1)
def directions(self, waypoints, profile=DEFAULT_PROFILE):
@@ -79,14 +83,28 @@ class MapboxRouting(Traceable):
coordinates = marshall_coordinates(waypoints)
uri = self._uri(coordinates, profile)
response = requests.get(uri)
if response.status_code == requests.codes.ok:
return self._parse_routing_response(response.text)
elif response.status_code == requests.codes.bad_request:
try:
response = requests.get(uri)
if response.status_code == requests.codes.ok:
return self._parse_routing_response(response.text)
elif response.status_code == requests.codes.bad_request:
return MapboxRoutingResponse(None, None, None)
else:
raise ServiceException(response.status_code, response)
except requests.Timeout as te:
# In case of timeout we want to stop the job because the server
# could be down
self._logger.error('Timeout connecting to Mapbox routing service',
te)
raise ServiceException('Error getting routing data from Mapbox',
None)
except requests.ConnectionError as ce:
# Don't raise the exception to continue with the geocoding job
self._logger.error('Error connecting to Mapbox routing service',
exception=ce)
return MapboxRoutingResponse(None, None, None)
else:
raise ServiceException(response.status_code, response)
class MapboxRoutingResponse:

View File

@@ -1,4 +1,4 @@
MODE_TO_MAPBOX_PROFILE = {
TRANSPORT_MODE_TO_MAPBOX = {
'car': 'driving',
'walk': 'walking',
}

View File

@@ -166,6 +166,10 @@ class RoutingConfig(ServiceConfig):
def provider(self):
return self._routing_provider
@property
def mapzen_provider(self):
return self._routing_provider == self.MAPZEN_PROVIDER
@property
def mapzen_api_key(self):
return self._mapzen_api_key
@@ -174,6 +178,10 @@ class RoutingConfig(ServiceConfig):
def mapzen_service_params(self):
return self._mapzen_service_params
@property
def mapbox_provider(self):
return self._routing_provider == self.MAPBOX_PROVIDER
@property
def mapbox_api_key(self):
return self._mapbox_api_key