-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
808bf7f
Add error handling for missing timeZone in commonEventObject
WassCodeur 7130319
Refactor on_change_cat function to accept a dictionary parameter
WassCodeur 2ac976f
Merge remote-tracking branch 'upstream/main' into main
WassCodeur b1e4d7d
Refactor code to use models.GEvent instead of dict
WassCodeur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
||
|
@@ -80,7 +91,8 @@ async def on_change_cat(gevent: models.GEvent): | |
|
||
actionResponse = CardService.newActionResponseBuilder() \ | ||
.setNavigation(navigation) | ||
|
||
|
||
|
||
return actionResponse.build() | ||
|
||
|
||
|
@@ -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() \ | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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.') | ||
|
@@ -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') \ | ||
|
@@ -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 '' | ||
|
@@ -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) | ||
|
||
response = CardService.newUpdateDraftActionResponseBuilder() \ | ||
.setUpdateDraftBodyAction(draft_action) \ | ||
.setUpdateDraftgeventAction(draft_action) \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
|
||
|
@@ -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) |
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,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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?