Skip to content

Commit

Permalink
started refactoring of GSuite code into new (2024) osbot dev workflow…
Browse files Browse the repository at this point in the history
…s and structure
  • Loading branch information
DinisCruz committed Oct 9, 2024
1 parent f5a708b commit faff7af
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-pipeline__dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ env:
jobs:

run-unit-tests:
name: "Run Unit tets"
name: "Run Unit tests"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci-pipeline__main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ env:
jobs:

run-unit-tests:
name: "Run Unit tets"
name: "Run Unit tests"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
96 changes: 0 additions & 96 deletions osbot_gsuite/apis/GSuite.py

This file was deleted.

68 changes: 68 additions & 0 deletions osbot_gsuite/gsuite/GSuite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import Resource
from osbot_utils.utils.Json import json_loads

from osbot_utils.base_classes.Type_Safe import Type_Safe
from osbot_utils.utils.Env import get_env

ENV_NAME__GSUITE__OAUTH2__DATA = 'GSUITE__OAUTH2__DATA'
ENV_NAME__GSUITE__OAUTH2__FILE = 'GSUITE__OAUTH2__FILE'


class GSuite(Type_Safe): # todo see if there is a better name for this

def gsuite__oauth2__data(self):
return get_env(ENV_NAME__GSUITE__OAUTH2__DATA)

# return file with token credentials
def gsuite__oauth2__file(self):
return get_env(ENV_NAME__GSUITE__OAUTH2__FILE)


# this creates the credentials object required to create the GSuite service object
def create_credentials(self, scopes=None) -> Credentials:
oauth2_data = self.gsuite__oauth2__data()
if oauth2_data:
info = json_loads(oauth2_data)
credentials = Credentials.from_authorized_user_info(info, scopes=scopes)
return credentials
oauth2_file = self.gsuite__oauth2__file()
if oauth2_file:
credentials = Credentials.from_authorized_user_file(filename=oauth2_file, scopes=scopes)
return credentials

raise ValueError("no OAuth2 data or file found")


def create_service(self,serviceName, version, scope) -> Resource:
creds = self.create_credentials(scope)
return build(serviceName, version, credentials=creds)

# helper files to create individual GSuite service objects
def admin_reports_v1(self):
return self.create_service('admin', 'reports_v1','admin.reports.audit.readonly')

def calendar_v3(self):
return self.create_service('calendar','v3','calendar')

def docs_v1(self):
return self.create_service('docs', 'v1', 'documents')

def drive_v3(self):
return self.create_service('drive', 'v3', 'drive')
#return self.create_service('drive', 'v3', 'drive.metadata.readonly')

def drive_activity_v2(self):
return self.create_service('driveactivity', 'v2', 'drive.activity')

def people_v1(self):
return self.create_service('people', 'v1', 'contacts')

def slides_v1(self):
return self.create_service('slides', 'v1', 'presentations')

def sheets_v4(self):
return self.create_service('sheets', 'v4', 'spreadsheets')

Empty file added osbot_gsuite/gsuite/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from googleapiclient.http import MediaFileUpload
from osbot_gsuite.apis.GSuite import GSuite
from osbot_utils.utils.Dev import Dev
from googleapiclient.http import MediaFileUpload

from osbot_gsuite.gsuite.GSuite import GSuite
from osbot_utils.utils.Dev import Dev
from osbot_utils.utils.Files import Files


Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from osbot_gsuite.apis.GDrive import GDrive
from osbot_gsuite.apis.GSuite import GSuite
from osbot_gsuite.gsuite.drive.GDrive import GDrive
from osbot_utils.base_classes.Type_Safe import Type_Safe

from osbot_utils.utils import Misc


class GSlides:
class GSlides(Type_Safe):
presentations : object
gdrive : GDrive

def __init__(self, gsuite_secret_id=None):
self.presentations = GSuite(gsuite_secret_id).slides_v1().presentations()
self.gdrive = GDrive(gsuite_secret_id)
# def __init__(self, gsuite_secret_id=None):
# self.presentations = GSuite(gsuite_secret_id).slides_v1().presentations()
# self.gdrive = GDrive(gsuite_secret_id)

# misc utils

Expand Down
Empty file.
12 changes: 12 additions & 0 deletions tests/integration/gsuite/slides/test_GSlides.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from unittest import TestCase

from osbot_gsuite.gsuite.slides.GSlides import GSlides


class test_GSlides(TestCase):

@classmethod
def setUpClass(cls):
cls.gslides = GSlides()


84 changes: 84 additions & 0 deletions tests/unit/gsuite/test_GSuite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from unittest import TestCase
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import Resource
from osbot_utils.utils.Env import load_dotenv
from osbot_gsuite.gsuite.GSuite import GSuite


class test_GSuite(TestCase):

@classmethod
def setUpClass(cls):
load_dotenv()
cls.gsuite = GSuite()

def test_create_credentials(self):
with self.gsuite as _:
credentials = self.gsuite.create_credentials()
assert type(credentials) is Credentials
assert credentials.token_uri == 'https://oauth2.googleapis.com/token'
assert credentials.universe_domain == 'googleapis.com'

def test_create_service(self):
with self.gsuite as _:
service = _.create_service('drive', 'v3', 'drive')
assert type(service) is Resource
assert service._baseUrl == "https://www.googleapis.com/drive/v3/"
assert service._dynamic_attrs == ['new_batch_http_request', 'about', 'apps', 'changes', 'channels', 'comments', 'drives', 'files', 'operation', 'operations', 'permissions', 'replies', 'revisions', 'teamdrives']

def test_admin_reports_v1(self):
with self.gsuite as _:
service = _.admin_reports_v1()
assert type(service) is Resource
assert service._baseUrl == "https://admin.googleapis.com/"
assert service._dynamic_attrs == ['new_batch_http_request', 'activities', 'channels', 'customerUsageReports', 'entityUsageReports', 'userUsageReport']

def test_calendar_v3(self):
with self.gsuite as _:
service = _.calendar_v3()
assert type(service) is Resource
assert service._baseUrl == 'https://www.googleapis.com/calendar/v3/'
assert service._dynamic_attrs == ['new_batch_http_request', 'acl', 'calendarList', 'calendars', 'channels', 'colors', 'events', 'freebusy', 'settings']

def test_docs_v1(self):
with self.gsuite as _:
service = _.docs_v1()
assert type(service) is Resource
assert service._baseUrl == 'https://docs.googleapis.com/'
assert service._dynamic_attrs == ['new_batch_http_request', 'documents']

def test_drive_v3(self):
with self.gsuite as _:
service = _.drive_v3()
assert type(service) is Resource
assert service._baseUrl == 'https://www.googleapis.com/drive/v3/'
assert service._dynamic_attrs == ['new_batch_http_request', 'about', 'apps', 'changes', 'channels', 'comments', 'drives', 'files', 'operation', 'operations', 'permissions', 'replies', 'revisions', 'teamdrives']

def test_drive_activity_v2(self):
with self.gsuite as _:
service = _.drive_activity_v2()
assert type(service) is Resource
assert service._baseUrl == 'https://driveactivity.googleapis.com/'
assert service._dynamic_attrs == ['new_batch_http_request', 'activity']

def test_people_v1(self):
with self.gsuite as _:
service = _.people_v1()
assert type(service) is Resource
assert service._baseUrl == 'https://people.googleapis.com/'
assert service._dynamic_attrs == ['new_batch_http_request', 'contactGroups', 'otherContacts', 'people']

def test_slides_v1(self):
with self.gsuite as _:
service = _.slides_v1()
assert type(service) is Resource
assert service._baseUrl == 'https://slides.googleapis.com/'
assert service._dynamic_attrs == ['new_batch_http_request', 'presentations']

def test_sheets_v4(self):
with self.gsuite as _:
service = _.sheets_v4()
assert type(service) is Resource
assert service._baseUrl == 'https://sheets.googleapis.com/'
assert service._dynamic_attrs == ['new_batch_http_request', 'spreadsheets']

0 comments on commit faff7af

Please sign in to comment.