From ecd00e62e3d5d329a7d6358aea091f74f6b5da4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Arroyo=20Torrens?= Date: Tue, 31 Mar 2020 15:47:43 +0200 Subject: [PATCH] Use default credentials. Add checks to avoid exceptions --- cartoframes/auth/credentials.py | 4 +++- cartoframes/utils/metrics.py | 5 +---- cartoframes/utils/utils.py | 12 +++++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cartoframes/auth/credentials.py b/cartoframes/auth/credentials.py index 6e0c1cf35..9cd61ba68 100644 --- a/cartoframes/auth/credentials.py +++ b/cartoframes/auth/credentials.py @@ -118,7 +118,9 @@ def user_id(self): try: user_me = api_key_auth_client.send(ME_SERVICE, 'get').json() - self._user_id = user_me.get('user_data', {}).get('id') + user_data = user_me.get('user_data') + if user_data: + self._user_id = user_data.get('id') except ValueError: # When the response isn't a JSON pass diff --git a/cartoframes/utils/metrics.py b/cartoframes/utils/metrics.py index e0295abeb..e0892c355 100644 --- a/cartoframes/utils/metrics.py +++ b/cartoframes/utils/metrics.py @@ -114,10 +114,7 @@ def build_extra_metrics_data(decorated_function, *args, **kwargs): 'credentials', decorated_function, *args, **kwargs) credentials = get_credentials(credentials) return {'user_id': credentials.user_id} if credentials and credentials.user_id else {} - - # ValueError: When the decorated function doesn't contain `credentials`, e.g. creating a map - # AttributeError: When no user is set, e.g., reading a public table - except (ValueError, AttributeError): + except Exception: return {} diff --git a/cartoframes/utils/utils.py b/cartoframes/utils/utils.py index 555abb8b3..b65188a7c 100644 --- a/cartoframes/utils/utils.py +++ b/cartoframes/utils/utils.py @@ -529,15 +529,17 @@ def fn(*args, **kw): def get_parameter_from_decorator(parameter_name, decorated_function, *args, **kwargs): + parameter = None + try: parameter = kwargs[parameter_name] - except KeyError: try: - parameter_arg_index = inspect.getargspec(decorated_function).args.index(parameter_name) - parameter = args[parameter_arg_index] - + parameter_args = inspect.getargspec(decorated_function).args + if parameter_name in parameter_args: + parameter_arg_index = parameter_args.index(parameter_name) + parameter = args[parameter_arg_index] except IndexError: - parameter = None + pass return parameter