def _calculate_isoline(self, origin, time_range, profile=DEFAULT_PROFILE, date_time=DEFAULT_DEPARTAT): origin = '{lat},{lon}'.format(lat=origin.latitude, lon=origin.longitude) uri = self._uri(origin, time_range, profile, date_time) try: response = requests.get(uri) if response.status_code == requests.codes.ok: return self._parse_reachablerange_response(response.text) elif response.status_code == requests.codes.bad_request: return [] elif response.status_code == requests.codes.unprocessable_entity: 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 TomTom calculateReachableRange service', te) raise ServiceException( 'Error getting calculateReachableRange data from TomTom', None) except requests.ConnectionError as ce: # Don't raise the exception to continue with the geocoding job self._logger.error( 'Error connecting to TomTom calculateReachableRange service', exception=ce) return []
def matrix(self, coordinates, profile=DEFAULT_PROFILE): validate_profile(profile) validate_coordinates(coordinates, NUM_COORDINATES_MIN, NUM_COORDINATES_MAX) coords = marshall_coordinates(coordinates) uri = self._uri(coords, profile) try: response = requests.get(uri) if response.status_code == requests.codes.ok: return response.text elif response.status_code == requests.codes.bad_request: return '{}' elif response.status_code == requests.codes.unprocessable_entity: 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 '{}'
def directions(self, waypoints, profile=DEFAULT_PROFILE): self._validate_profile(profile) validate_coordinates(waypoints, NUM_WAYPOINTS_MIN, NUM_WAYPOINTS_MAX) coordinates = marshall_coordinates(waypoints) uri = self._uri(coordinates, profile) 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) elif response.status_code == requests.codes.unprocessable_entity: 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)
def geocode(self, searchtext, city=None, state_province=None, country=None, search_type=None): # Remove the search_type if its address from the params sent to mapzen if search_type and search_type.lower() == 'address': search_type = None request_params = self._build_requests_parameters( searchtext, city, state_province, country, search_type) try: # TODO Extract HTTP client wrapper session = requests.Session() session.mount(self._url, HTTPAdapter(max_retries=self._max_retries)) response = session.get(self._url, params=request_params, timeout=(self._connect_timeout, self._read_timeout)) self.add_response_data(response, self._logger) if response.status_code == requests.codes.ok: return self.__parse_response(response.text) elif response.status_code == requests.codes.bad_request: return [] else: self._logger.error('Error trying to geocode using mapzen', data={ "response_status": response.status_code, "response_reason": response.reason, "response_content": response.text, "reponse_url": response.url, "response_headers": response.headers, "searchtext": searchtext, "city": city, "country": country, "state_province": state_province }) raise ServiceException( 'Error trying to geocode {0} using mapzen'.format( searchtext), 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 Mapzen geocoding server', te) raise ServiceException( 'Error trying to geocode {0} using mapzen'.format(searchtext), None) except requests.ConnectionError as e: # Don't raise the exception to continue with the geocoding job self._logger.error('Error connecting to Mapzen geocoding server', exception=e) return []
def geocode(self, searchtext, city=None, state_province=None, country=None): if searchtext: searchtext = searchtext.decode('utf-8') if city: city = city.decode('utf-8') if state_province: state_province = state_province.decode('utf-8') if country: country = country.decode('utf-8') if not self._validate_input(searchtext, city, state_province, country): return [] address = [] if searchtext and searchtext.strip(): address.append(normalize(searchtext)) if city: address.append(normalize(city)) if state_province: address.append(normalize(state_province)) uri = self._uri(searchtext=', '.join(address), countries=country) try: response = requests.get(uri) if response.status_code == requests.codes.ok: return self._parse_geocoder_response(response.text) elif response.status_code == requests.codes.bad_request: return [] elif response.status_code == requests.codes.unprocessable_entity: 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 TomTom geocoding server', te) raise ServiceException('Error geocoding {0} using TomTom'.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 TomTom geocoding server', exception=ce) return []
def geocode(self, searchtext, city=None, state_province=None, country=None): response = self.geocode_meta(searchtext, city, state_province, country) error_message = response[1].get('error', None) if error_message: raise ServiceException(error_message, None) else: return response[0]
def _calculate_isoline(self, origin, time_ranges, profile=DEFAULT_PROFILE): self._validate_time_ranges(time_ranges) origin = '{lon},{lat}'.format(lat=origin.latitude, lon=origin.longitude) time_ranges.sort() time_ranges_seconds = ','.join( [str(round(t / 60)) for t in time_ranges]) uri = self._uri(origin, time_ranges_seconds, profile) try: response = requests.get(uri) if response.status_code == requests.codes.ok: isolines = [] coordinates = self._parse_isochrone_service(response.text) for t, c in zip(time_ranges, coordinates): isolines.append(MapboxIsochronesResponse(c, t)) return isolines elif response.status_code == requests.codes.bad_request: return [] elif response.status_code == requests.codes.unprocessable_entity: return [] else: raise ServiceException( 'Unexpected response (' + str(response.status_code) + '): ' + str(response.content), 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 isochrone service', te) raise ServiceException('Error getting isochrone 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 isochrone service', exception=ce) return []
def isochrone(self, locations, costing, ranges): request_params = self._parse_request_params(locations, costing, ranges) try: # TODO Extract HTTP client wrapper session = requests.Session() session.mount(self._url, HTTPAdapter(max_retries=self._max_retries)) response = session.get(self._url, params=request_params, timeout=(self._connect_timeout, self._read_timeout)) if response.status_code is requests.codes.ok: return self._parse_response(response) elif response.status_code == requests.codes.bad_request: return [] else: self._logger.error( 'Error trying to get isochrones from mapzen', data={ "response_status": response.status_code, "response_reason": response.reason, "response_content": response.text, "reponse_url": response.url, "response_headers": response.headers, "locations": locations, "costing": costing }) raise ServiceException( 'Error trying to get isochrones from mapzen', 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 Mapzen isochrones server', exception=te) raise ServiceException( 'Error trying to calculate isochrones using mapzen', None) except requests.ConnectionError as e: # Don't raise the exception to continue with the geocoding job self._logger.error('Error connecting to Mapzen isochrones server', exception=e) return []
def _send_batch(self, searches): body = {'batchItems': [{'query': self._query(s)} for s in searches]} request_params = {'key': self._apikey} response = self.session.post(self.BATCH_URL, data=json.dumps(body), allow_redirects=False, params=request_params, timeout=(self.connect_timeout, self.read_timeout)) if response.status_code == 303: return response.headers['Location'] else: msg = "Error sending batch: {}; Headers: {}".format( response.text.encode('utf-8'), response.headers) self._logger.error(msg) raise ServiceException(msg, response)
def one_to_many(self, locations, costing): request_params = { 'json': json.dumps({'locations': locations}), 'costing': costing, 'api_key': self._matrix_key } response = requests.get(self._url, params=request_params, timeout=(self._connect_timeout, self._read_timeout)) self.add_response_data(response, self._logger) if response.status_code != requests.codes.ok: self._logger.error( 'Error trying to get matrix distance from mapzen', data={ "response_status": response.status_code, "response_reason": response.reason, "response_content": response.text, "reponse_url": response.url, "response_headers": response.headers, "locations": locations, "costing": costing }) # In case 4xx error we return empty because the error comes from # the provided info by the user and we don't want to top the # isolines generation if response.status_code == requests.codes.bad_request: return {} elif response.status_code == 504: # Due to some unsolved problems in the Mapzen Matrix API we're # getting randomly 504, probably timeouts. To avoid raise an # exception in all the jobs, for now we're going to return # empty in that case return {} else: raise ServiceException( "Error trying to get matrix distance from mapzen", response) # response could return with empty json try: return response.json() except: return {}
def _download_results(self, location): stalled_retries = 0 while True: response = self.session.get(self.BASE_URL + location) if response.status_code == 200: return self._parse_results(response.json()) elif response.status_code == 202: stalled_retries += 1 if stalled_retries > self.MAX_STALLED_RETRIES: raise Exception( 'Too many retries for job {}'.format(location)) location = response.headers['Location'] time.sleep(self.BATCH_RETRY_SLEEP_S) else: msg = "Error downloading batch: {}; Headers: {}".format( response.text.encode('utf-8'), response.headers) self._logger.error(msg) raise ServiceException(msg, response)
def geocode(self, searchtext, city=None, state_province=None, country=None): """ :param searchtext: :param city: :param state_province: :param country: Country ISO 3166 code :return: [x, y] on success, raises ServiceException on error """ response = self.geocode_meta(searchtext, city, state_province, country) if response: error_message = response[1].get('error', None) if error_message: raise ServiceException(error_message, None) else: return response[0] else: return EMPTY_RESPONSE
def _send_batch(self, data): cols = 'displayLatitude,displayLongitude,' + ','.join(self.META_COLS) request_params = self.credentials_params.copy() request_params.update({ 'gen': 8, 'action': 'run', 'header': 'true', 'inDelim': '|', 'outDelim': '|', 'outCols': cols, 'outputcombined': 'true' }) response = self.session.post(self.BATCH_URL, data=data, params=request_params, timeout=(self.connect_timeout, self.read_timeout)) if response.status_code == 200: root = ET.fromstring(response.text) return root.find('./Response/MetaInfo/RequestId').text else: raise ServiceException("Error sending HERE batch", response)
def calculate_route_point_to_point(self, waypoints, mode, options=[], units=METRICS_UNITS): parsed_options = self.__parse_options(options) mode_param = self.__parse_mode_param(mode, parsed_options) directions = self.__parse_directions(waypoints) json_request_params = self.__parse_json_parameters( directions, mode_param, units) request_params = self.__parse_request_parameters(json_request_params) # TODO Extract HTTP client wrapper session = requests.Session() session.mount(self._url, HTTPAdapter(max_retries=self._max_retries)) response = session.get(self._url, params=request_params, timeout=(self._connect_timeout, self._read_timeout)) self.add_response_data(response, self._logger) if response.status_code == requests.codes.ok: return self.__parse_routing_response(response.text) elif response.status_code == requests.codes.bad_request: return MapzenRoutingResponse(None, None, None) else: self._logger.error('Error trying to calculate route using Mapzen', data={ "response_status": response.status_code, "response_reason": response.reason, "response_content": response.text, "reponse_url": response.url, "response_headers": response.headers, "waypoints": waypoints, "mode": mode, "options": options }) raise ServiceException( 'Error trying to calculate route using Mapzen', response)
def test(self): response = requests.get('http://localhost/test_qps') if response.status_code == 429: raise ServiceException('Error 429', response)