Skip to content

Commit

Permalink
Add Google API tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ejohb committed Dec 10, 2024
1 parent 3f8300c commit 3401f77
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions fmtr/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@
except ImportError as exception:
openai = MissingExtraMockModule('openai', exception)

try:
from fmtr.tools import google_api_tools as google_api
except ImportError as exception:
google_api = MissingExtraMockModule('google.api', exception)


__all__ = [
'config',
Expand Down
36 changes: 36 additions & 0 deletions fmtr/tools/google_api_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build

from fmtr.tools.logging_tools import logger


class Authenticator:
"""
Authenticate via link or saved token
"""

PATH = None
SCOPES = []
SERVICE = None
VERSION = None

@classmethod
def auth(cls):
msg = f'Doing auth for service {cls.SERVICE} ({cls.VERSION})...'
logger.info(msg)

PATH_CREDS = cls.PATH / 'credentials.json'
PATH_TOKEN = cls.PATH / 'token.json'

if PATH_TOKEN.exists():
data_token = PATH_TOKEN.read_json()
credentials = Credentials.from_authorized_user_info(data_token, cls.SCOPES)
else:
flow = InstalledAppFlow.from_client_secrets_file(PATH_CREDS, cls.SCOPES)
credentials = flow.run_local_server(open_browser=False)
PATH_TOKEN.write_text(credentials.to_json())
service = build(cls.SERVICE, cls.VERSION, credentials=credentials)
return service
2 changes: 1 addition & 1 deletion fmtr/tools/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.7
1.0.8
3 changes: 2 additions & 1 deletion requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
'metric': ['pandas'],
'html': ['html2text'],
'interface': ['streamlit', 'dm'],
'openai.api': ['openai']
'openai.api': ['openai'],
'google.api': ['google-auth', 'google-auth-oauthlib', 'google-auth-httplib2', 'google-api-python-client']
}

CONSOLE_SCRIPTS = [
Expand Down

0 comments on commit 3401f77

Please sign in to comment.