From 2cf4072b210749e20d786dafc0e63bb58f38becf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20S=C3=A1nchez=20Lara?= Date: Wed, 8 Nov 2017 10:42:10 +0100 Subject: [PATCH 1/6] Support for multiple parameters at client (such as channel) --- .../cartodb_services/google/geocoder.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py index bf14b84..c3ecd80 100644 --- a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import googlemaps +import re from exceptions import MalformedResult from client_factory import GoogleMapsClientFactory @@ -48,4 +49,11 @@ class GoogleMapsGeocoder: def _clean_client_id(self, client_id): # Consistency with how the client_id is saved in metadata - return client_id.replace('client=', '') + if client_id is None: + return '' + else: + search_result = re.search(r'client=([^&]*).*', client_id) + if search_result is None: + return client_id + else: + return search_result.group(1) From 43dd9f6ada3134b8d5d9553efd875c977db5afac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20S=C3=A1nchez=20Lara?= Date: Wed, 8 Nov 2017 17:33:00 +0100 Subject: [PATCH 2/6] Better regexp and wrong input handling --- .../cartodb_services/google/geocoder.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py index c3ecd80..39a6f73 100644 --- a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py @@ -5,6 +5,7 @@ import googlemaps import re from exceptions import MalformedResult +from cartodb_services.google.exceptions import InvalidGoogleCredentials from client_factory import GoogleMapsClientFactory @@ -12,6 +13,8 @@ class GoogleMapsGeocoder: """A Google Maps Geocoder wrapper for python""" def __init__(self, client_id, client_secret, logger): + if client_id is None: + raise InvalidGoogleCredentials self.client_id = self._clean_client_id(client_id) self.client_secret = client_secret self.geocoder = GoogleMapsClientFactory.get(self.client_id, self.client_secret) @@ -49,11 +52,5 @@ class GoogleMapsGeocoder: def _clean_client_id(self, client_id): # Consistency with how the client_id is saved in metadata - if client_id is None: - return '' - else: - search_result = re.search(r'client=([^&]*).*', client_id) - if search_result is None: - return client_id - else: - return search_result.group(1) + search_result = re.search(r'(client=)?(?P[^&]*).*', client_id) + return search_result.groupdict()['client_id'] From e26bf2a803dcdf28382373cb4d292ba3a556a818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20S=C3=A1nchez=20Lara?= Date: Mon, 13 Nov 2017 19:09:17 +0100 Subject: [PATCH 3/6] Proper parsing of client and channel parameters --- .../cartodb_services/google/client_factory.py | 7 ++++--- .../cartodb_services/google/geocoder.py | 13 +++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/google/client_factory.py b/server/lib/python/cartodb_services/cartodb_services/google/client_factory.py index caf3fd8..feb7f4a 100644 --- a/server/lib/python/cartodb_services/cartodb_services/google/client_factory.py +++ b/server/lib/python/cartodb_services/cartodb_services/google/client_factory.py @@ -9,14 +9,15 @@ class GoogleMapsClientFactory(): clients = {} @classmethod - def get(cls, client_id, client_secret): - cache_key = "{}:{}".format(client_id, client_secret) + def get(cls, client_id, client_secret, channel=None): + cache_key = "{}:{}:{}".format(client_id, client_secret, channel) client = cls.clients.get(cache_key) if not client: cls.assert_valid_crendentials(client_secret) client = googlemaps.Client( client_id=client_id, - client_secret=client_secret) + client_secret=client_secret, + channel=channel) cls.clients[cache_key] = client return client diff --git a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py index 39a6f73..51a6184 100644 --- a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- import googlemaps -import re +from urlparse import parse_qs from exceptions import MalformedResult from cartodb_services.google.exceptions import InvalidGoogleCredentials @@ -15,9 +15,11 @@ class GoogleMapsGeocoder: def __init__(self, client_id, client_secret, logger): if client_id is None: raise InvalidGoogleCredentials - self.client_id = self._clean_client_id(client_id) + arguments = parse_qs(client_id) + self.client_id = arguments['client'] if arguments.has_key('client') else client_id self.client_secret = client_secret - self.geocoder = GoogleMapsClientFactory.get(self.client_id, self.client_secret) + self.channel = arguments['channel'] if arguments.has_key('channel') else None + self.geocoder = GoogleMapsClientFactory.get(self.client_id, self.client_secret, self.channel) self._logger = logger def geocode(self, searchtext, city=None, state=None, @@ -49,8 +51,3 @@ class GoogleMapsGeocoder: if country: optional_params['country'] = country return optional_params - - def _clean_client_id(self, client_id): - # Consistency with how the client_id is saved in metadata - search_result = re.search(r'(client=)?(?P[^&]*).*', client_id) - return search_result.groupdict()['client_id'] From f540a3d6b32b270c0c3a99b93ae090e8b84b50e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20S=C3=A1nchez=20Lara?= Date: Mon, 13 Nov 2017 19:09:52 +0100 Subject: [PATCH 4/6] Bump version --- server/lib/python/cartodb_services/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/lib/python/cartodb_services/setup.py b/server/lib/python/cartodb_services/setup.py index 3b3446a..9133421 100644 --- a/server/lib/python/cartodb_services/setup.py +++ b/server/lib/python/cartodb_services/setup.py @@ -10,7 +10,7 @@ from setuptools import setup, find_packages setup( name='cartodb_services', - version='0.15.5', + version='0.15.6', description='CartoDB Services API Python Library', From da8e5f6e36db09009786e306bdeee1b658af7cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20S=C3=A1nchez=20Lara?= Date: Tue, 14 Nov 2017 13:15:11 +0100 Subject: [PATCH 5/6] Extract parsing and return fix --- .../cartodb_services/google/geocoder.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py index 51a6184..2fb0e01 100644 --- a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py @@ -15,10 +15,8 @@ class GoogleMapsGeocoder: def __init__(self, client_id, client_secret, logger): if client_id is None: raise InvalidGoogleCredentials - arguments = parse_qs(client_id) - self.client_id = arguments['client'] if arguments.has_key('client') else client_id + self.client_id, self.channel = self.parse_client_id(client_id) self.client_secret = client_secret - self.channel = arguments['channel'] if arguments.has_key('channel') else None self.geocoder = GoogleMapsClientFactory.get(self.client_id, self.client_secret, self.channel) self._logger = logger @@ -51,3 +49,9 @@ class GoogleMapsGeocoder: if country: optional_params['country'] = country return optional_params + + def parse_client_id(self, client_id): + arguments = parse_qs(client_id) + client = arguments['client'][0] if arguments.has_key('client') else client_id + channel = arguments['channel'][0] if arguments.has_key('channel') else None + return client, channel From d42fb3cf8e75ba9200f242e851cfdfc76db0dda2 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Fri, 17 Nov 2017 11:20:06 +0100 Subject: [PATCH 6/6] Updated NEWS.md --- NEWS.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS.md b/NEWS.md index a77777e..3ba451f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +November 17th, 2017 +================= +* Version `0.15.6` of the python library + * Added support for channels for google geocoder #409 + * Improved the way we manage the client_id #409 + October 18th, 2017 ================= * Version `0.21.0` of the client extension