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 get_method(self, name): """ Returns the servicemethod given by name """ try: return self.methods[name] except KeyError: raise ServiceException('Service method "%s" not registered' % name)
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) 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: 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 geocode(self, searchtext, city=None, state_province=None, country=None, search_type=None): request_params = self._build_requests_parameters( searchtext, city, state_province, country, search_type) try: 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: 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 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 add_piece(self, x, piece): """ Adds a piece to the board :param x: Coord on the x axis :param piece: Piece value :return: None """ for y in range(6): if self.__board.get_position(x, y) == 0: self.__board.set_position(x, y, piece) break else: raise ServiceException("Column full")
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 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 signal_handler(signum, frame): raise ServiceException("Program is interrupted!")