Google Cloud Translate API works through GOOGLE_APPLICATION_CREDENTIALS env variable

This commit is contained in:
Fabio Manganiello 2020-07-02 02:24:54 +02:00
parent 566fd42ee8
commit a4824a4e95

View file

@ -1,8 +1,6 @@
import os import os
from typing import Optional from typing import Optional
# noinspection PyPackageRequirements
from google.auth import jwt
# noinspection PyPackageRequirements # noinspection PyPackageRequirements
from google.cloud import translate_v2 as translate from google.cloud import translate_v2 as translate
@ -53,10 +51,8 @@ class GoogleTranslatePlugin(Plugin):
elif os.path.isfile(self.default_credentials_file): elif os.path.isfile(self.default_credentials_file):
self.credentials_file = self.default_credentials_file self.credentials_file = self.default_credentials_file
def _get_credentials(self):
if self.credentials_file: if self.credentials_file:
return jwt.Credentials.from_service_account_file( os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = self.credentials_file
self.credentials_file)
# noinspection PyShadowingBuiltins # noinspection PyShadowingBuiltins
@action @action
@ -72,18 +68,11 @@ class GoogleTranslatePlugin(Plugin):
:return: :class:`platypush.message.response.translate.TranslateResponse`. :return: :class:`platypush.message.response.translate.TranslateResponse`.
""" """
target_language = target_language or self.target_language target_language = target_language or self.target_language
credentials = self._get_credentials()
args = {} args = {}
if target_language: if target_language:
args['target_language'] = target_language args['target_language'] = target_language
if credentials:
args['credentials'] = credentials
client = translate.Client(**args) client = translate.Client(**args)
if credentials:
del args['credentials']
if source_language: if source_language:
args['source_language'] = source_language args['source_language'] = source_language