Added parameters for PLPython functions

This commit is contained in:
Antonio 2017-12-28 11:36:02 +01:00
parent bc9dbdbb72
commit af07def7fc
13 changed files with 3106 additions and 26 deletions

View File

@ -1,5 +1,5 @@
comment = 'CartoDB dataservices server extension' comment = 'CartoDB dataservices server extension'
default_version = '0.29.0' default_version = '0.30.0'
requires = 'plpythonu, plproxy, postgis, cdb_geocoder' requires = 'plpythonu, plproxy, postgis, cdb_geocoder'
superuser = true superuser = true
schema = cdb_dataservices_server schema = cdb_dataservices_server

File diff suppressed because it is too large Load Diff

View File

@ -9,8 +9,7 @@ from cartodb_services.metrics import Traceable
from cartodb_services.mapbox.exceptions import ServiceException from cartodb_services.mapbox.exceptions import ServiceException
from cartodb_services.tools.qps import qps_retry from cartodb_services.tools.qps import qps_retry
ACCESS_TOKEN = 'pk.eyJ1IjoiYWNhcmxvbiIsImEiOiJjamJuZjQ1Zjc0Ymt4Mnh0YmFrMmhtYnY4In0.gt9cw0VeKc3rM2mV5pcEmg' GEOCODER_NAME = 'geocoder_name'
EPHEMERAL_GEOCODER = 'mapbox.places' EPHEMERAL_GEOCODER = 'mapbox.places'
PERMANENT_GEOCODER = 'mapbox.places-permanent' PERMANENT_GEOCODER = 'mapbox.places-permanent'
DEFAULT_GEOCODER = EPHEMERAL_GEOCODER DEFAULT_GEOCODER = EPHEMERAL_GEOCODER
@ -28,9 +27,14 @@ class MapboxGeocoder(Traceable):
Python wrapper for the Mapbox Geocoder service. Python wrapper for the Mapbox Geocoder service.
''' '''
def __init__(self, token=ACCESS_TOKEN, name=DEFAULT_GEOCODER): def __init__(self, token, logger, service_params=None):
service_params = service_params or {}
self._token = token self._token = token
self._geocoder = Geocoder(access_token=self._token, name=name) self._logger = logger
self._geocoder_name = service_params.get(GEOCODER_NAME,
EPHEMERAL_GEOCODER)
self._geocoder = Geocoder(access_token=self._token,
name=self._geocoder_name)
def _parse_geocoder_response(self, response): def _parse_geocoder_response(self, response):
json_response = json.loads(response) json_response = json.loads(response)

View File

@ -37,8 +37,10 @@ class MapboxIsolines():
Python wrapper for Mapbox services based isolines. Python wrapper for Mapbox services based isolines.
''' '''
def __init__(self, matrix_client): def __init__(self, matrix_client, logger, service_params=None):
service_params = service_params or {}
self._matrix_client = matrix_client self._matrix_client = matrix_client
self._logger = logger
def _calculate_matrix_cost(self, origin, targets, isorange, def _calculate_matrix_cost(self, origin, targets, isorange,
profile=DEFAULT_PROFILE, profile=DEFAULT_PROFILE,

View File

@ -9,8 +9,6 @@ from cartodb_services.tools.coordinates import (validate_coordinates,
from exceptions import ServiceException from exceptions import ServiceException
from cartodb_services.tools.qps import qps_retry from cartodb_services.tools.qps import qps_retry
ACCESS_TOKEN = 'pk.eyJ1IjoiYWNhcmxvbiIsImEiOiJjamJuZjQ1Zjc0Ymt4Mnh0YmFrMmhtYnY4In0.gt9cw0VeKc3rM2mV5pcEmg'
BASEURI = ('https://api.mapbox.com/directions-matrix/v1/mapbox/{profile}/' BASEURI = ('https://api.mapbox.com/directions-matrix/v1/mapbox/{profile}/'
'{coordinates}' '{coordinates}'
'?access_token={token}' '?access_token={token}'
@ -48,15 +46,14 @@ class MapboxMatrixClient(Traceable):
Python wrapper for the Mapbox Time Matrix service. Python wrapper for the Mapbox Time Matrix service.
''' '''
def __init__(self, token=ACCESS_TOKEN): def __init__(self, token, logger, service_params=None):
self.token = token service_params = service_params or {}
self._token = token
self._logger = logger
def _uri(self, coordinates, profile=DEFAULT_PROFILE): def _uri(self, coordinates, profile=DEFAULT_PROFILE):
return BASEURI.format(profile=profile, coordinates=coordinates, return BASEURI.format(profile=profile, coordinates=coordinates,
token=self.token) token=self._token)
def _parse_matrix_response(self, response):
return response
@qps_retry(qps=1) @qps_retry(qps=1)
def matrix(self, coordinates, profile=DEFAULT_PROFILE): def matrix(self, coordinates, profile=DEFAULT_PROFILE):
@ -70,6 +67,6 @@ class MapboxMatrixClient(Traceable):
response = requests.get(uri) response = requests.get(uri)
if response.status_code == requests.codes.ok: if response.status_code == requests.codes.ok:
return self._parse_matrix_response(response.text) return response.text
else: else:
raise ServiceException(response.status_code, response.content) raise ServiceException(response.status_code, response.content)

View File

@ -11,8 +11,6 @@ from cartodb_services.tools.coordinates import (validate_coordinates,
from cartodb_services.mapbox.exceptions import ServiceException from cartodb_services.mapbox.exceptions import ServiceException
from cartodb_services.tools.qps import qps_retry from cartodb_services.tools.qps import qps_retry
ACCESS_TOKEN = 'pk.eyJ1IjoiYWNhcmxvbiIsImEiOiJjamJuZjQ1Zjc0Ymt4Mnh0YmFrMmhtYnY4In0.gt9cw0VeKc3rM2mV5pcEmg'
BASEURI = ('https://api.mapbox.com/directions/v5/mapbox/{profile}/' BASEURI = ('https://api.mapbox.com/directions/v5/mapbox/{profile}/'
'{coordinates}' '{coordinates}'
'?access_token={token}' '?access_token={token}'
@ -45,8 +43,10 @@ class MapboxRouting(Traceable):
Python wrapper for the Mapbox Routing service. Python wrapper for the Mapbox Routing service.
''' '''
def __init__(self, token=ACCESS_TOKEN): def __init__(self, token, logger, service_params=None):
service_params = service_params or {}
self._token = token self._token = token
self._logger = logger
def _uri(self, coordinates, profile=DEFAULT_PROFILE, def _uri(self, coordinates, profile=DEFAULT_PROFILE,
overview=DEFAULT_OVERVIEW): overview=DEFAULT_OVERVIEW):

View File

@ -1,7 +1,9 @@
import unittest import unittest
from mock import Mock
from cartodb_services.mapbox import MapboxGeocoder from cartodb_services.mapbox import MapboxGeocoder
from cartodb_services.mapbox import ServiceException from cartodb_services.mapbox import ServiceException
VALID_TOKEN = 'pk.eyJ1IjoiYWNhcmxvbiIsImEiOiJjamJuZjQ1Zjc0Ymt4Mnh0YmFrMmhtYnY4In0.gt9cw0VeKc3rM2mV5pcEmg'
INVALID_TOKEN = 'invalid_token' INVALID_TOKEN = 'invalid_token'
VALID_ADDRESS = 'Calle Siempreviva 3, Valladolid' VALID_ADDRESS = 'Calle Siempreviva 3, Valladolid'
WELL_KNOWN_LONGITUDE = -4.730947 WELL_KNOWN_LONGITUDE = -4.730947
@ -10,10 +12,10 @@ WELL_KNOWN_LATITUDE = 41.668654
class MapboxGeocoderTestCase(unittest.TestCase): class MapboxGeocoderTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.geocoder = MapboxGeocoder() self.geocoder = MapboxGeocoder(token=VALID_TOKEN, logger=Mock())
def test_invalid_token(self): def test_invalid_token(self):
invalid_geocoder = MapboxGeocoder(token=INVALID_TOKEN) invalid_geocoder = MapboxGeocoder(token=INVALID_TOKEN, logger=Mock())
with self.assertRaises(ServiceException): with self.assertRaises(ServiceException):
invalid_geocoder.geocode(VALID_ADDRESS) invalid_geocoder.geocode(VALID_ADDRESS)

View File

@ -1,4 +1,5 @@
import unittest import unittest
from mock import Mock
from cartodb_services.mapbox.isolines import MapboxIsolines from cartodb_services.mapbox.isolines import MapboxIsolines
from cartodb_services.mapbox.matrix_client import DEFAULT_PROFILE from cartodb_services.mapbox.matrix_client import DEFAULT_PROFILE
from cartodb_services.mapbox.matrix_client import MapboxMatrixClient from cartodb_services.mapbox.matrix_client import MapboxMatrixClient
@ -7,14 +8,15 @@ from cartodb_services.tools import Coordinate
from cartodb_services.tools.coordinates import (validate_coordinates, from cartodb_services.tools.coordinates import (validate_coordinates,
marshall_coordinates) marshall_coordinates)
VALID_TOKEN = 'pk.eyJ1IjoiYWNhcmxvbiIsImEiOiJjamJuZjQ1Zjc0Ymt4Mnh0YmFrMmhtYnY4In0.gt9cw0VeKc3rM2mV5pcEmg'
VALID_ORIGIN = Coordinate(-73.989, 40.733) VALID_ORIGIN = Coordinate(-73.989, 40.733)
class MapboxIsolinesTestCase(unittest.TestCase): class MapboxIsolinesTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
matrix_client = MapboxMatrixClient() matrix_client = MapboxMatrixClient(token=VALID_TOKEN, logger=Mock())
self.mapbox_isolines = MapboxIsolines(matrix_client) self.mapbox_isolines = MapboxIsolines(matrix_client, logger=Mock())
def test_calculate_isochrone(self): def test_calculate_isochrone(self):
time_range = 10 * 60 # 10 minutes time_range = 10 * 60 # 10 minutes

View File

@ -1,9 +1,11 @@
import unittest import unittest
from mock import Mock
from cartodb_services.mapbox import MapboxMatrixClient from cartodb_services.mapbox import MapboxMatrixClient
from cartodb_services.mapbox.matrix_client import DEFAULT_PROFILE from cartodb_services.mapbox.matrix_client import DEFAULT_PROFILE
from cartodb_services.mapbox import ServiceException from cartodb_services.mapbox import ServiceException
from cartodb_services.tools import Coordinate from cartodb_services.tools import Coordinate
VALID_TOKEN = 'pk.eyJ1IjoiYWNhcmxvbiIsImEiOiJjamJuZjQ1Zjc0Ymt4Mnh0YmFrMmhtYnY4In0.gt9cw0VeKc3rM2mV5pcEmg'
INVALID_TOKEN = 'invalid_token' INVALID_TOKEN = 'invalid_token'
VALID_ORIGIN = Coordinate(-73.989, 40.733) VALID_ORIGIN = Coordinate(-73.989, 40.733)
VALID_TARGET = Coordinate(-74, 40.733) VALID_TARGET = Coordinate(-74, 40.733)
@ -20,7 +22,8 @@ INVALID_PROFILE = 'invalid_profile'
class MapboxMatrixTestCase(unittest.TestCase): class MapboxMatrixTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.matrix_client = MapboxMatrixClient() self.matrix_client = MapboxMatrixClient(token=VALID_TOKEN,
logger=Mock())
def test_invalid_profile(self): def test_invalid_profile(self):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
@ -43,7 +46,7 @@ class MapboxMatrixTestCase(unittest.TestCase):
VALID_PROFILE) VALID_PROFILE)
def test_invalid_token(self): def test_invalid_token(self):
invalid_matrix = MapboxMatrixClient(token=INVALID_TOKEN) invalid_matrix = MapboxMatrixClient(token=INVALID_TOKEN, logger=Mock())
with self.assertRaises(ServiceException): with self.assertRaises(ServiceException):
invalid_matrix.matrix(VALID_COORDINATES, invalid_matrix.matrix(VALID_COORDINATES,
VALID_PROFILE) VALID_PROFILE)

View File

@ -1,9 +1,11 @@
import unittest import unittest
from mock import Mock
from cartodb_services.mapbox import MapboxRouting from cartodb_services.mapbox import MapboxRouting
from cartodb_services.mapbox.routing import DEFAULT_PROFILE from cartodb_services.mapbox.routing import DEFAULT_PROFILE
from cartodb_services.mapbox import ServiceException from cartodb_services.mapbox import ServiceException
from cartodb_services.tools import Coordinate from cartodb_services.tools import Coordinate
VALID_TOKEN = 'pk.eyJ1IjoiYWNhcmxvbiIsImEiOiJjamJuZjQ1Zjc0Ymt4Mnh0YmFrMmhtYnY4In0.gt9cw0VeKc3rM2mV5pcEmg'
INVALID_TOKEN = 'invalid_token' INVALID_TOKEN = 'invalid_token'
VALID_WAYPOINTS = [Coordinate(-73.989, 40.733), Coordinate(-74, 40.733)] VALID_WAYPOINTS = [Coordinate(-73.989, 40.733), Coordinate(-74, 40.733)]
NUM_WAYPOINTS_MAX = 25 NUM_WAYPOINTS_MAX = 25
@ -29,7 +31,7 @@ WELL_KNOWN_LENGTH = 1317.9
class MapboxRoutingTestCase(unittest.TestCase): class MapboxRoutingTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.routing = MapboxRouting() self.routing = MapboxRouting(token=VALID_TOKEN, logger=Mock())
def test_invalid_profile(self): def test_invalid_profile(self):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
@ -48,7 +50,7 @@ class MapboxRoutingTestCase(unittest.TestCase):
self.routing.directions(INVALID_WAYPOINTS_MAX, VALID_PROFILE) self.routing.directions(INVALID_WAYPOINTS_MAX, VALID_PROFILE)
def test_invalid_token(self): def test_invalid_token(self):
invalid_routing = MapboxRouting(token=INVALID_TOKEN) invalid_routing = MapboxRouting(token=INVALID_TOKEN, logger=Mock())
with self.assertRaises(ServiceException): with self.assertRaises(ServiceException):
invalid_routing.directions(VALID_WAYPOINTS, invalid_routing.directions(VALID_WAYPOINTS,
VALID_PROFILE) VALID_PROFILE)