-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started refactoring of GSuite code into new (2024) osbot dev workflow…
…s and structure
- Loading branch information
Showing
11 changed files
with
179 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
7 changes: 4 additions & 3 deletions
7
osbot_gsuite/apis/GDrive.py → osbot_gsuite/gsuite/drive/GDrive.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
15 changes: 9 additions & 6 deletions
15
osbot_gsuite/apis/GSlides.py → osbot_gsuite/gsuite/slides/GSlides.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
|