From 808bf7f54a83a7296d4ea834ad5c23fbdf6bd253 Mon Sep 17 00:00:00 2001 From: WassCodeur Date: Sat, 20 Jan 2024 02:03:25 +0000 Subject: [PATCH 1/3] Add error handling for missing timeZone in commonEventObject --- docs/examples/cats.py | 15 +++++++++------ requirements.txt | 7 +++++++ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 requirements.txt diff --git a/docs/examples/cats.py b/docs/examples/cats.py index 8b1c17c..d7e3552 100644 --- a/docs/examples/cats.py +++ b/docs/examples/cats.py @@ -6,7 +6,7 @@ from gapps import CardService from gapps.cardservice import models, utilities as ut -from fastapi import FastAPI +from fastapi import FastAPI, HTTPException from fastapi.responses import JSONResponse import googleapiclient.discovery import google.oauth2.credentials @@ -21,19 +21,22 @@ async def root(): @app.post("/homepage", response_class=JSONResponse) -async def homepage(gevent: models.GEvent): +async def homepage(gevent: dict): + + if "commonEventObject" not in gevent or "timeZone" not in gevent["commonEventObject"]: + raise HTTPException(status_code=422, detail={"detail": "commonEventObject must contain timeZone"}) + message = 'Hello' - if gevent.commonEventObject.timeZone: + if gevent["commonEventObject"]["timeZone"]: date = datetime.now(tz=pytz.timezone( - gevent.commonEventObject.timeZone.id)) + gevent["commonEventObject"]["timeZone"]["id"])) message = 'Good night' if 12 > date.hour >= 6: message = 'Good morning' elif 18 > date.hour >= 12: message = 'Good afternoon' - message += ' ' + gevent.commonEventObject.hostApp - + message += ' ' + gevent["commonEventObject"]["hostApp"] return create_cat_card(message, True) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5ef1c27 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +dataclasses_json==0.6.1 +fastapi==0.104.1 +google_api_python_client +protobuf +pydantic==1.10.11 +pytz +sphinx_rtd_theme==1.3.0 \ No newline at end of file From 7130319a5a0228296141917c5abfba2cb12af901 Mon Sep 17 00:00:00 2001 From: WassCodeur Date: Sat, 20 Jan 2024 02:04:41 +0000 Subject: [PATCH 2/3] Refactor on_change_cat function to accept a dictionary parameter --- docs/examples/cats.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/examples/cats.py b/docs/examples/cats.py index d7e3552..821cdfe 100644 --- a/docs/examples/cats.py +++ b/docs/examples/cats.py @@ -51,7 +51,9 @@ async def on_drive_items_selected(gevent: models.GEvent): @app.post('/on_change_cat', response_class=JSONResponse) -async def on_change_cat(gevent: models.GEvent): +async def on_change_cat(gevent: dict): + + """Callback for the 'Change cat' button. Parameters @@ -68,11 +70,13 @@ async def on_change_cat(gevent: models.GEvent): """ # Get the text that was shown in the current cat image. This was passed as # a parameter on the Action set for the button. - text = gevent.commonEventObject.parameters['text'] + text = str(gevent["commonEventObject"]["parameters"]["text"]) + #gevent.commonEventObject.parameters['text'] # The isHomepage parameter is passed as a string, so convert to a Boolean. - is_homepage = gevent.commonEventObject.parameters['is_homepage'] == 'True' - + is_homepage = gevent["commonEventObject"]["parameters"]["is_homepage"] + + # Create a new card with the same text. card = create_cat_card(text, is_homepage) @@ -87,6 +91,7 @@ async def on_change_cat(gevent: models.GEvent): return actionResponse.build() + def truncate(message, max_message_length=40): """Truncate a message to fit in the cat image. From b1e4d7d41137a7d8062d4af6e5491d451ec91de5 Mon Sep 17 00:00:00 2001 From: WassCodeur Date: Sun, 11 Feb 2024 16:06:37 +0000 Subject: [PATCH 3/3] Refactor code to use models.GEvent instead of dict --- docs/examples/cats.py | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/docs/examples/cats.py b/docs/examples/cats.py index 821cdfe..d2675a7 100644 --- a/docs/examples/cats.py +++ b/docs/examples/cats.py @@ -21,8 +21,8 @@ async def root(): @app.post("/homepage", response_class=JSONResponse) -async def homepage(gevent: dict): - +async def homepage(gevent: models.GEvent): + gevent = gevent.dict() if "commonEventObject" not in gevent or "timeZone" not in gevent["commonEventObject"]: raise HTTPException(status_code=422, detail={"detail": "commonEventObject must contain timeZone"}) @@ -37,11 +37,16 @@ async def homepage(gevent: dict): message = 'Good afternoon' message += ' ' + gevent["commonEventObject"]["hostApp"] + + #return str(gevent) return create_cat_card(message, True) + + @app.post('/on_items_selected', response_class=JSONResponse) async def on_drive_items_selected(gevent: models.GEvent): + gevent = gevent.dict() all_items = gevent.drive.selectedItems all_items = all_items[:5] # Include at most 5 items in the text. print(all_items) @@ -51,9 +56,7 @@ async def on_drive_items_selected(gevent: models.GEvent): @app.post('/on_change_cat', response_class=JSONResponse) -async def on_change_cat(gevent: dict): - - +async def on_change_cat(gevent: models.GEvent): """Callback for the 'Change cat' button. Parameters @@ -70,6 +73,7 @@ async def on_change_cat(gevent: dict): """ # Get the text that was shown in the current cat image. This was passed as # a parameter on the Action set for the button. + gevent = gevent.dict() text = str(gevent["commonEventObject"]["parameters"]["text"]) #gevent.commonEventObject.parameters['text'] @@ -87,11 +91,11 @@ async def on_change_cat(gevent: dict): actionResponse = CardService.newActionResponseBuilder() \ .setNavigation(navigation) - + + return actionResponse.build() - def truncate(message, max_message_length=40): """Truncate a message to fit in the cat image. @@ -131,8 +135,10 @@ def create_cat_card(text, is_homepage=False): # Create a button that changes the cat image when pressed. # Note: Action parameter keys and values must be strings. + # https://test-gapps.vercel.app/on_change_cat + # action = CardService.newAction() \ - .setFunctionName('https://gwa.momentz.fr/on_change_cat') \ + .setFunctionName("https://test-gapps.vercel.app/on_change_cat") \ .setParameters({'text': text, 'is_homepage': str(is_homepage)}) button = CardService.newTextButton() \ @@ -194,6 +200,7 @@ def on_gmail_message(gevent: models.GEvent): # Get an access token scoped to the current message and use it for GmailApp # calls. + gevent = gevent.dict() access_token = gevent.authorizationEventObject.userOAuthToken cred = google.oauth2.credentials.Credentials(access_token) service = googleapiclient.discovery.build('gmail', 'v1', credentials=cred) @@ -215,8 +222,7 @@ def on_gmail_message(gevent: models.GEvent): # If neccessary, truncate the subject to fit in the image. subject = truncate(subject) - - return create_cat_card(subject) + return create_cat_card(subject, False) @app.post('/on_gmail_compose', response_class=JSONResponse) @@ -235,6 +241,7 @@ def on_gmail_compose(gevent: models.GEvent): The card to show to the user. """ + gevent = gevent.dict() header = CardService.newCardHeader() \ .setTitle('Insert cat') \ .setSubtitle('Add a custom cat image to your email message.') @@ -247,7 +254,7 @@ def on_gmail_compose(gevent: models.GEvent): # Create a button that inserts the cat image when pressed. action = CardService.newAction() \ - .setFunctionName('https://gwa.momentz.fr/on_gmail_insert_cat') + .setFunctionName('https://test-gapps.vercel.app/on_gmail_insert_cat') button = CardService.newTextButton() \ .setText('Insert cat') \ @@ -285,6 +292,7 @@ def on_gmail_insert_cat(gevent: models.GEvent): """ # Get the text that was entered by the user. + gevent = gevent.dict() form_inputs = gevent.commonEventObject.formInputs text = ut.get_form_value(form_inputs, 'text') text = text[0] if len(text) else '' @@ -303,13 +311,13 @@ def on_gmail_insert_cat(gevent: models.GEvent): imageHtmlContent = \ f'' - draft_action = CardService.newUpdateDraftBodyAction() \ + draft_action = CardService.newUpdateDraftgeventAction() \ .addUpdateContent(imageHtmlContent, CardService.ContentType.MUTABLE_HTML) \ - .setUpdateType(CardService.UpdateDraftBodyType.IN_PLACE_INSERT) + .setUpdateType(CardService.UpdateDraftgeventType.IN_PLACE_INSERT) response = CardService.newUpdateDraftActionResponseBuilder() \ - .setUpdateDraftBodyAction(draft_action) \ + .setUpdateDraftgeventAction(draft_action) \ .build() return response @@ -332,6 +340,7 @@ def on_calendar_event_open(gevent: models.GEvent): """ # Get the ID of the Calendar and the event + gevent = gevent.dict() calendar_id = gevent.calendar.calendarId event_id = gevent.calendar.id @@ -353,4 +362,4 @@ def on_calendar_event_open(gevent: models.GEvent): title = event.get('summary', 'A new event! Should I go?') # If necessary, truncate the title to fit in the image. title = truncate(title) - return create_cat_card(title) + return create_cat_card(title) \ No newline at end of file