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 new file mode 100644 index 0000000..3e2ada6 --- /dev/null +++ b/server/lib/python/cartodb_services/cartodb_services/google/client_factory.py @@ -0,0 +1,17 @@ +#!/usr/local/bin/python +# -*- coding: utf-8 -*- + +import googlemaps + +class GoogleMapsClientFactory(): + clients = {} + + @classmethod + def get(cls, client_id, client_secret): + client = cls.clients.get(client_id) + if not client: + client = googlemaps.Client( + client_id=client_id, + client_secret=client_secret) + cls.clients[client_id] = 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 a8458a4..b900921 100644 --- a/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/google/geocoder.py @@ -5,18 +5,16 @@ import base64 import googlemaps from exceptions import MalformedResult, InvalidGoogleCredentials +from client_factory import GoogleMapsClientFactory class GoogleMapsGeocoder: """A Google Maps Geocoder wrapper for python""" def __init__(self, client_id, client_secret, logger): - if not self._valid_credentials(client_secret): - raise InvalidGoogleCredentials('Invalid google secret key') self.client_id = self._clean_client_id(client_id) self.client_secret = client_secret - self.geocoder = googlemaps.Client( - client_id=self.client_id, client_secret=self.client_secret) + self.geocoder = GoogleMapsClientFactory.get(self.client_id, self.client_secret) self._logger = logger def geocode(self, searchtext, city=None, state=None, @@ -52,13 +50,3 @@ class GoogleMapsGeocoder: def _clean_client_id(self, client_id): # Consistency with how the client_id is saved in metadata return client_id.replace('client=', '') - - def _valid_credentials(self, private_key): - try: - # Only fails if the string dont have a correct padding for b64 - # but this way we could provide a more clear error than - # TypeError: Incorrect padding - base64.b64decode(private_key) - return True - except TypeError: - return False