Use service params configuration in Mapzen services

This commit is contained in:
Javier Goizueta 2017-03-06 15:25:11 +01:00
parent 1081e81047
commit 89762c1a7f
7 changed files with 57 additions and 16 deletions

View File

@ -31,7 +31,7 @@ RETURNS cdb_dataservices_server.simple_route AS $$
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: try:
client = MapzenRouting(user_routing_config.mapzen_api_key, logger) client = MapzenRouting(user_routing_config.mapzen_api_key, logger, user_routing_config.mapzen_service_params)
if not waypoints or len(waypoints) < 2: if not waypoints or len(waypoints) < 2:
logger.info("Empty origin or destination") logger.info("Empty origin or destination")

View File

@ -20,7 +20,7 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: try:
client = HereMapsRoutingIsoline(user_isolines_routing_config.heremaps_app_id, client = HereMapsRoutingIsoline(user_isolines_routing_config.heremaps_app_id,
user_isolines_routing_config.heremaps_app_code, logger) user_isolines_routing_config.heremaps_app_code, logger)
if source: if source:
@ -81,7 +81,7 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
raise Exception('You have reached the limit of your quota') raise Exception('You have reached the limit of your quota')
try: try:
client = MatrixClient(user_isolines_routing_config.mapzen_matrix_api_key, logger) client = MatrixClient(user_isolines_routing_config.mapzen_matrix_api_key, logger, user_isolines_routing_config.mapzen_matrix_service_params)
mapzen_isolines = MapzenIsolines(client, logger) mapzen_isolines = MapzenIsolines(client, logger)
if source: if source:
@ -151,7 +151,7 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
try: try:
mapzen_isochrones = MapzenIsochrones(user_isolines_routing_config.mapzen_matrix_api_key, mapzen_isochrones = MapzenIsochrones(user_isolines_routing_config.mapzen_matrix_api_key,
logger) logger, user_isolines_routing_config.mapzen_isochrones_service_params)
if source: if source:
lat = plpy.execute("SELECT ST_Y('%s') AS lat" % source)[0]['lat'] lat = plpy.execute("SELECT ST_Y('%s') AS lat" % source)[0]['lat']

View File

@ -20,10 +20,15 @@ class MapzenIsochrones:
"car": "auto" "car": "auto"
} }
def __init__(self, app_key, logger, base_url=BASE_URL): def __init__(self, app_key, logger, service_params={}):
service_params = service_params or {}
self._app_key = app_key self._app_key = app_key
self._url = base_url
self._logger = logger self._logger = logger
self._url = service_params.get('base_url', self.BASE_URL)
self._connect_timeout = service_params.get('connect_timeout', self.CONNECT_TIMEOUT)
self._read_timeout = service_params.get('read_timeout', self.READ_TIMEOUT)
self._max_retries = service_params.get('max_retries', self.MAX_RETRIES)
@qps_retry(qps=7) @qps_retry(qps=7)
def isochrone(self, locations, costing, ranges): def isochrone(self, locations, costing, ranges):
@ -32,10 +37,10 @@ class MapzenIsochrones:
try: try:
# TODO Extract HTTP client wrapper # TODO Extract HTTP client wrapper
session = requests.Session() session = requests.Session()
session.mount(self._url, HTTPAdapter(max_retries=self.MAX_RETRIES)) session.mount(self._url, HTTPAdapter(max_retries=self._max_retries))
response = session.get(self._url, params=request_params, response = session.get(self._url, params=request_params,
timeout=(self.CONNECT_TIMEOUT, timeout=(self._connect_timeout,
self.READ_TIMEOUT)) self._read_timeout))
if response.status_code is requests.codes.ok: if response.status_code is requests.codes.ok:
return self._parse_response(response) return self._parse_response(response)

View File

@ -23,9 +23,14 @@ class MatrixClient(Traceable):
READ_TIMEOUT = 60 READ_TIMEOUT = 60
CONNECT_TIMEOUT = 10 CONNECT_TIMEOUT = 10
def __init__(self, matrix_key, logger): def __init__(self, matrix_key, logger, service_params={}):
service_params = service_params or {}
self._matrix_key = matrix_key self._matrix_key = matrix_key
self._logger = logger self._logger = logger
self._url = service_params.get('one_to_many_url', self.ONE_TO_MANY_URL)
self._connect_timeout = service_params.get('connect_timeout', self.CONNECT_TIMEOUT)
self._read_timeout = service_params.get('read_timeout', self.READ_TIMEOUT)
"""Get distances and times to a set of locations. """Get distances and times to a set of locations.
See https://mapzen.com/documentation/matrix/api-reference/ See https://mapzen.com/documentation/matrix/api-reference/
@ -44,8 +49,8 @@ class MatrixClient(Traceable):
'costing': costing, 'costing': costing,
'api_key': self._matrix_key 'api_key': self._matrix_key
} }
response = requests.get(self.ONE_TO_MANY_URL, params=request_params, response = requests.get(self._url, params=request_params,
timeout=(self.CONNECT_TIMEOUT, self.READ_TIMEOUT)) timeout=(self._connect_timeout, self._read_timeout))
self.add_response_data(response, self._logger) self.add_response_data(response, self._logger)
if response.status_code != requests.codes.ok: if response.status_code != requests.codes.ok:

View File

@ -33,10 +33,14 @@ class MapzenRouting(Traceable):
METRICS_UNITS = 'kilometers' METRICS_UNITS = 'kilometers'
IMPERIAL_UNITS = 'miles' IMPERIAL_UNITS = 'miles'
def __init__(self, app_key, logger, base_url=PRODUCTION_ROUTING_BASE_URL): def __init__(self, app_key, logger, service_params={}):
service_params = service_params or {}
self._app_key = app_key self._app_key = app_key
self._url = base_url
self._logger = logger self._logger = logger
self._url = service_params.get('base_url', self.PRODUCTION_ROUTING_BASE_URL)
self._connect_timeout = service_params.get('connect_timeout', self.CONNECT_TIMEOUT)
self._read_timeout = service_params.get('read_timeout', self.READ_TIMEOUT)
self._max_retries = service_params.get('max_retries', self.MAX_RETRIES)
@qps_retry @qps_retry
def calculate_route_point_to_point(self, waypoints, mode, def calculate_route_point_to_point(self, waypoints, mode,
@ -50,9 +54,9 @@ class MapzenRouting(Traceable):
request_params = self.__parse_request_parameters(json_request_params) request_params = self.__parse_request_parameters(json_request_params)
# TODO Extract HTTP client wrapper # TODO Extract HTTP client wrapper
session = requests.Session() session = requests.Session()
session.mount(self._url, HTTPAdapter(max_retries=self.MAX_RETRIES)) session.mount(self._url, HTTPAdapter(max_retries=self._max_retries))
response = session.get(self._url, params=request_params, response = session.get(self._url, params=request_params,
timeout=(self.CONNECT_TIMEOUT, self.READ_TIMEOUT)) timeout=(self._connect_timeout, self._read_timeout))
self.add_response_data(response, self._logger) self.add_response_data(response, self._logger)
if response.status_code == requests.codes.ok: if response.status_code == requests.codes.ok:
return self.__parse_routing_response(response.text) return self.__parse_routing_response(response.text)

View File

@ -52,3 +52,16 @@ class MapzenIsochronesTestCase(unittest.TestCase):
with self.assertRaises(ServiceException): with self.assertRaises(ServiceException):
self.mapzen_isochrones.isochrone([-41.484375, 28.993727], self.mapzen_isochrones.isochrone([-41.484375, 28.993727],
'walk', [300, 900]) 'walk', [300, 900])
def test_nonstandard_url(self, req_mock):
url = 'http://serviceurl.com'
req_mock.register_uri('GET', url, text=self.GOOD_RESPONSE)
mapzen_isochrones = MapzenIsochrones('matrix-xxxxx', Mock(), {'base_url': url})
response = mapzen_isochrones.isochrone([-41.484375, 28.993727],
'walk', [300, 900])
self.assertEqual(len(response), 2)
self.assertEqual(response[0].coordinates, [[-3.702579,40.430893],[-3.702193,40.430122],[-3.702579,40.430893]])
self.assertEqual(response[0].duration, 15)
self.assertEqual(response[1].coordinates, [[-3.703050,40.424995],[-3.702546,40.424694],[-3.703050,40.424995]])
self.assertEqual(response[1].duration, 5)

View File

@ -142,3 +142,17 @@ class MapzenRoutingTestCase(unittest.TestCase):
self.assertEqual(response.length, 1.261) self.assertEqual(response.length, 1.261)
self.assertEqual(response.duration, 913) self.assertEqual(response.duration, 913)
self.assertEqual(response.shape, self.GOOD_SHAPE_MULTI) self.assertEqual(response.shape, self.GOOD_SHAPE_MULTI)
def test_nonstandard_url(self, req_mock):
url = 'http://serviceurl.com'
routing = MapzenRouting('api_key', Mock(), {'base_url': url})
req_mock.register_uri('GET', url, text=self.GOOD_RESPONSE_SIMPLE)
origin = Coordinate('-120.2', '38.5')
destination = Coordinate('-126.4', '43.2')
waypoints = [origin, destination]
response = routing.calculate_route_point_to_point(waypoints,
'car')
self.assertEqual(response.shape, self.GOOD_SHAPE_SIMPLE)
self.assertEqual(response.length, 444.59)
self.assertEqual(response.duration, 16969)