From ad2b05bc4a8626dfb5f8d33d1f54152eea9ca9c0 Mon Sep 17 00:00:00 2001 From: Vinicius Aguiar Date: Mon, 9 Oct 2023 19:20:04 -0300 Subject: [PATCH] fix: adapt get_credentials for filepaths --- basedosdados_api/api/v1/admin.py | 13 ++++++++++--- basedosdados_api/settings/dev.py | 3 +++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/basedosdados_api/api/v1/admin.py b/basedosdados_api/api/v1/admin.py index fc8e7d04..ec82a48a 100644 --- a/basedosdados_api/api/v1/admin.py +++ b/basedosdados_api/api/v1/admin.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- -import json +from json import loads +from pathlib import Path from time import sleep from django import forms @@ -216,8 +217,14 @@ class CoverageTableInline(admin.StackedInline): def get_credentials(): """Get google cloud credentials""" - credentials_dict = json.loads(settings.GOOGLE_APPLICATION_CREDENTIALS) - credentials = service_account.Credentials.from_service_account_info(credentials_dict) + creds_env = settings.GOOGLE_APPLICATION_CREDENTIALS + try: + creds_path = Path(creds_env) + assert creds_path.is_absolute() or creds_path.is_relative_to('.') + credentials = service_account.Credentials.from_service_account_file(creds_path) + except (TypeError, ValueError): + credentials_dict = loads(creds_env) + credentials = service_account.Credentials.from_service_account_info(credentials_dict) return credentials diff --git a/basedosdados_api/settings/dev.py b/basedosdados_api/settings/dev.py index 1c9d53c2..5926ff38 100644 --- a/basedosdados_api/settings/dev.py +++ b/basedosdados_api/settings/dev.py @@ -42,3 +42,6 @@ # Logging setup_logger(level="DEBUG", serialize=False) + +# Google Application Credentials +GOOGLE_APPLICATION_CREDENTIALS = getenv("GOOGLE_APPLICATION_CREDENTIALS", "")