Merge pull request #409 from CartoDB/s1060-google_geocoding_returns_null

Support for multiple parameters at client (such as channel)
This commit is contained in:
Mario de Frutos 2017-11-17 11:17:45 +01:00 committed by GitHub
commit 4f5117611f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 9 deletions

View File

@ -9,14 +9,15 @@ class GoogleMapsClientFactory():
clients = {} clients = {}
@classmethod @classmethod
def get(cls, client_id, client_secret): def get(cls, client_id, client_secret, channel=None):
cache_key = "{}:{}".format(client_id, client_secret) cache_key = "{}:{}:{}".format(client_id, client_secret, channel)
client = cls.clients.get(cache_key) client = cls.clients.get(cache_key)
if not client: if not client:
cls.assert_valid_crendentials(client_secret) cls.assert_valid_crendentials(client_secret)
client = googlemaps.Client( client = googlemaps.Client(
client_id=client_id, client_id=client_id,
client_secret=client_secret) client_secret=client_secret,
channel=channel)
cls.clients[cache_key] = client cls.clients[cache_key] = client
return client return client

View File

@ -2,8 +2,10 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import googlemaps import googlemaps
from urlparse import parse_qs
from exceptions import MalformedResult from exceptions import MalformedResult
from cartodb_services.google.exceptions import InvalidGoogleCredentials
from client_factory import GoogleMapsClientFactory from client_factory import GoogleMapsClientFactory
@ -11,9 +13,11 @@ class GoogleMapsGeocoder:
"""A Google Maps Geocoder wrapper for python""" """A Google Maps Geocoder wrapper for python"""
def __init__(self, client_id, client_secret, logger): def __init__(self, client_id, client_secret, logger):
self.client_id = self._clean_client_id(client_id) if client_id is None:
raise InvalidGoogleCredentials
self.client_id, self.channel = self.parse_client_id(client_id)
self.client_secret = client_secret self.client_secret = client_secret
self.geocoder = GoogleMapsClientFactory.get(self.client_id, self.client_secret) self.geocoder = GoogleMapsClientFactory.get(self.client_id, self.client_secret, self.channel)
self._logger = logger self._logger = logger
def geocode(self, searchtext, city=None, state=None, def geocode(self, searchtext, city=None, state=None,
@ -46,6 +50,8 @@ class GoogleMapsGeocoder:
optional_params['country'] = country optional_params['country'] = country
return optional_params return optional_params
def _clean_client_id(self, client_id): def parse_client_id(self, client_id):
# Consistency with how the client_id is saved in metadata arguments = parse_qs(client_id)
return client_id.replace('client=', '') 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

View File

@ -10,7 +10,7 @@ from setuptools import setup, find_packages
setup( setup(
name='cartodb_services', name='cartodb_services',
version='0.15.5', version='0.15.6',
description='CartoDB Services API Python Library', description='CartoDB Services API Python Library',