Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gevent type error #5

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions docs/examples/cats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,23 +22,31 @@ async def root():

@app.post("/homepage", response_class=JSONResponse)
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"})

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 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)
Expand All @@ -65,11 +73,14 @@ 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']
gevent = gevent.dict()
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)

Expand All @@ -80,7 +91,8 @@ async def on_change_cat(gevent: models.GEvent):

actionResponse = CardService.newActionResponseBuilder() \
.setNavigation(navigation)



return actionResponse.build()


Expand Down Expand Up @@ -123,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() \
Expand Down Expand Up @@ -186,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)
Expand All @@ -207,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)
Expand All @@ -227,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.')
Expand All @@ -239,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') \
Expand Down Expand Up @@ -277,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 ''
Expand All @@ -295,13 +311,13 @@ def on_gmail_insert_cat(gevent: models.GEvent):
imageHtmlContent = \
f'<img style="display: block max-height: 300px" src="{imageUrl}"/>'

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not find this in the documentation, where did you find it ?


response = CardService.newUpdateDraftActionResponseBuilder() \
.setUpdateDraftBodyAction(draft_action) \
.setUpdateDraftgeventAction(draft_action) \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, I do not find this in the documentation, where did you find it ?

.build()

return response
Expand All @@ -324,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

Expand All @@ -345,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)
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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