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] 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']