Spaces:
Running
Running
Add error handling to collect function in mail router
Browse files- app/router/mail.py +19 -16
app/router/mail.py
CHANGED
@@ -21,22 +21,25 @@ def collect(email: str, request: Request):
|
|
21 |
Returns:
|
22 |
str: The generated response from the chat function.
|
23 |
"""
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
40 |
|
41 |
# @router.get("")
|
42 |
# def get():
|
|
|
21 |
Returns:
|
22 |
str: The generated response from the chat function.
|
23 |
"""
|
24 |
+
try:
|
25 |
+
if os.path.exists(f"cache/{email}.pickle"):
|
26 |
+
with open(f"cache/{email}.pickle", "rb") as token:
|
27 |
+
credentials = pickle.load(token)
|
28 |
+
else:
|
29 |
+
cred_dict = request.state.session.get("credential")
|
30 |
+
credentials = Credentials(
|
31 |
+
token=cred_dict["token"],
|
32 |
+
refresh_token=cred_dict["refresh_token"],
|
33 |
+
token_uri=cred_dict["token_uri"],
|
34 |
+
client_id=cred_dict["client_id"],
|
35 |
+
client_secret=cred_dict["client_secret"],
|
36 |
+
scopes=cred_dict["scopes"],
|
37 |
+
)
|
38 |
+
mailservice = build("gmail", "v1", credentials=credentials)
|
39 |
+
mail.collect(mailservice)
|
40 |
+
return JSONResponse(content={"message": "Mail collected successfully."})
|
41 |
+
except Exception as e:
|
42 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
43 |
|
44 |
# @router.get("")
|
45 |
# def get():
|