Add again the check for valid credentials

As our check is more strict that the one provided in googlemaps library.
This commit is contained in:
Rafa de la Torre
2017-10-06 13:44:01 +02:00
parent 854fbb803c
commit f79ac9297d
2 changed files with 20 additions and 2 deletions

View File

@@ -2,6 +2,8 @@
# -*- coding: utf-8 -*-
import googlemaps
import base64
from exceptions import InvalidGoogleCredentials
class GoogleMapsClientFactory():
clients = {}
@@ -10,8 +12,25 @@ class GoogleMapsClientFactory():
def get(cls, client_id, client_secret):
client = cls.clients.get(client_id)
if not client:
cls.assert_valid_crendentials(client_secret)
client = googlemaps.Client(
client_id=client_id,
client_secret=client_secret)
cls.clients[client_id] = client
return client
@classmethod
def assert_valid_crendentials(cls, client_secret):
if not cls.valid_credentials(client_secret):
raise InvalidGoogleCredentials
@staticmethod
def valid_credentials(client_secret):
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(client_secret)
return True
except TypeError:
return False

View File

@@ -1,10 +1,9 @@
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import base64
import googlemaps
from exceptions import MalformedResult, InvalidGoogleCredentials
from exceptions import MalformedResult
from client_factory import GoogleMapsClientFactory