Proper parsing of client and channel parameters

This commit is contained in:
Juan Ignacio Sánchez Lara 2017-11-13 19:09:17 +01:00
parent 43dd9f6ada
commit e26bf2a803
2 changed files with 9 additions and 11 deletions

View File

@ -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

View File

@ -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>[^&]*).*', client_id)
return search_result.groupdict()['client_id']