From c0374f5800331e622c146eef4dd7ab619711c0dd Mon Sep 17 00:00:00 2001 From: Javier Goizueta Date: Thu, 2 Mar 2017 17:55:08 +0100 Subject: [PATCH] Add service configuration (URL, etc) for Here Routing --- .../cartodb_services/here/routing.py | 15 ++++++++++----- .../test/test_heremapsrouting.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/here/routing.py b/server/lib/python/cartodb_services/cartodb_services/here/routing.py index 3a04a3a..2b739d4 100644 --- a/server/lib/python/cartodb_services/cartodb_services/here/routing.py +++ b/server/lib/python/cartodb_services/cartodb_services/here/routing.py @@ -30,12 +30,17 @@ class HereMapsRoutingIsoline(Traceable): 'quality' ] - def __init__(self, app_id, app_code, logger, - base_url=PRODUCTION_ROUTING_BASE_URL): + def __init__(self, app_id, app_code, logger, service_params={}): + service_params = service_params or {} self._app_id = app_id self._app_code = app_code self._logger = logger - self._url = "{0}{1}".format(base_url, self.ISOLINE_PATH) + base_url = service_params.get('base_url', self.PRODUCTION_ROUTING_BASE_URL) + isoline_path = service_params.get('isoline_path', self.ISOLINE_PATH) + 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) + self._url = "{0}{1}".format(base_url, isoline_path) def calculate_isodistance(self, source, mode, data_range, options=[]): return self.__calculate_isolines(source, mode, data_range, 'distance', @@ -57,9 +62,9 @@ class HereMapsRoutingIsoline(Traceable): parsed_options) # TODO Extract HTTP client wrapper session = requests.Session() - session.mount(self._url, HTTPAdapter(max_retries=self.MAX_RETRIES)) + session.mount(self._url, HTTPAdapter(max_retries=self.max_retries)) 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) if response.status_code == requests.codes.ok: return self.__parse_isolines_response(response.text) diff --git a/server/lib/python/cartodb_services/test/test_heremapsrouting.py b/server/lib/python/cartodb_services/test/test_heremapsrouting.py index 6ce247a..6226572 100644 --- a/server/lib/python/cartodb_services/test/test_heremapsrouting.py +++ b/server/lib/python/cartodb_services/test/test_heremapsrouting.py @@ -212,3 +212,20 @@ class HereMapsRoutingIsolineTestCase(unittest.TestCase): parsed_url = urlparse(req_mock.request_history[0].url) url_params = parse_qs(parsed_url.query) self.assertEqual(url_params['destination'][0], 'geo!33.0,1.0') + + def test_isodistance_with_nonstandard_url(self, req_mock): + base_url = 'http://nonstandard_base' + url = "{0}{1}".format(base_url, HereMapsRoutingIsoline.ISOLINE_PATH) + routing = HereMapsRoutingIsoline(None, None, Mock(), { 'base_url': base_url }) + req_mock.register_uri('GET', url, text=self.GOOD_RESPONSE) + response = routing.calculate_isodistance('geo!33.0,1.0', 'car', + ['1000', '2000']) + self.assertEqual(len(response), 2) + self.assertEqual(response[0]['range'], 1000) + self.assertEqual(response[1]['range'], 2000) + self.assertEqual(response[0]['geom'], [u'32.9699707,0.9462833', + u'32.9699707,0.9458542', + u'32.9699707,0.9462833']) + self.assertEqual(response[1]['geom'], [u'32.9699707,0.9462833', + u'32.9699707,0.9750366', + u'32.9699707,0.9462833'])