jupiter0913 commited on
Commit ·
ce0cf0a
1
Parent(s): 76be69d
feature(#98): created email router module and updated app
Browse files- Brain/src/router/api.py +0 -57
- Brain/src/router/email_router.py +77 -0
- app.py +3 -2
Brain/src/router/api.py
CHANGED
|
@@ -29,7 +29,6 @@ from Brain.src.rising_plugin.risingplugin import (
|
|
| 29 |
)
|
| 30 |
from Brain.src.firebase.cloudmessage import CloudMessage
|
| 31 |
from Brain.src.rising_plugin.image_embedding import embed_image_text, query_image_text
|
| 32 |
-
from Brain.src.rising_plugin.gmail.manage_gmail import EmailManager
|
| 33 |
|
| 34 |
from Brain.src.logs import logger
|
| 35 |
from Brain.src.model.basic_model import BasicModel
|
|
@@ -501,62 +500,6 @@ def construct_blueprint_api() -> APIRouter:
|
|
| 501 |
200, "Deleted data from real-time database of firebase", ""
|
| 502 |
)
|
| 503 |
|
| 504 |
-
"""@generator.request_body(
|
| 505 |
-
{
|
| 506 |
-
"token": "String",
|
| 507 |
-
"uuid": "String",
|
| 508 |
-
"data": {
|
| 509 |
-
"sender": "test@gmail.com",
|
| 510 |
-
"pwd": "use app password of your google account",
|
| 511 |
-
"imap_folder": "inbox or drafts"
|
| 512 |
-
},
|
| 513 |
-
}
|
| 514 |
-
)
|
| 515 |
-
|
| 516 |
-
@generator.response(
|
| 517 |
-
status_code=200, schema={"message": "message", "result": [{
|
| 518 |
-
"From": "testfrom@test.com",
|
| 519 |
-
"To": "test@gmail.com",
|
| 520 |
-
"Date": "Tue, 04 Jul 2023 12:55:19 +0000",
|
| 521 |
-
"CC": "",
|
| 522 |
-
"Subject": "subject",
|
| 523 |
-
"Message Body": "message"
|
| 524 |
-
}]}
|
| 525 |
-
)
|
| 526 |
-
|
| 527 |
-
"""
|
| 528 |
-
|
| 529 |
-
@router.post("/read_emails")
|
| 530 |
-
def read_emails(data: EmailReader):
|
| 531 |
-
# firebase admin init
|
| 532 |
-
try:
|
| 533 |
-
setting, firebase_app = firebase_admin_with_setting(data)
|
| 534 |
-
except BrainException as ex:
|
| 535 |
-
return assembler.to_response(ex.code, ex.message, "")
|
| 536 |
-
try:
|
| 537 |
-
token = setting.token
|
| 538 |
-
uuid = setting.uuid
|
| 539 |
-
|
| 540 |
-
# if imap_folder is drafts, then search is ALL
|
| 541 |
-
imap_search_cmd = "UNSEEN"
|
| 542 |
-
if data.data.imap_folder not in "inbox":
|
| 543 |
-
imap_search_cmd = "(ALL)"
|
| 544 |
-
|
| 545 |
-
# read emails
|
| 546 |
-
email_manager = EmailManager()
|
| 547 |
-
result = email_manager.read_emails(
|
| 548 |
-
sender=data.data.sender,
|
| 549 |
-
pwd=data.data.pwd,
|
| 550 |
-
imap_folder=data.data.imap_folder,
|
| 551 |
-
imap_search_command=imap_search_cmd,
|
| 552 |
-
)
|
| 553 |
-
result = json.loads(result)
|
| 554 |
-
except Exception as e:
|
| 555 |
-
if isinstance(e, BrainException):
|
| 556 |
-
return e.get_response_exp()
|
| 557 |
-
return assembler.to_response(400, "Failed to read emails", "")
|
| 558 |
-
return assembler.to_response(200, "", result)
|
| 559 |
-
|
| 560 |
"""@generator.request_body(
|
| 561 |
{
|
| 562 |
"token": "String",
|
|
|
|
| 29 |
)
|
| 30 |
from Brain.src.firebase.cloudmessage import CloudMessage
|
| 31 |
from Brain.src.rising_plugin.image_embedding import embed_image_text, query_image_text
|
|
|
|
| 32 |
|
| 33 |
from Brain.src.logs import logger
|
| 34 |
from Brain.src.model.basic_model import BasicModel
|
|
|
|
| 500 |
200, "Deleted data from real-time database of firebase", ""
|
| 501 |
)
|
| 502 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
"""@generator.request_body(
|
| 504 |
{
|
| 505 |
"token": "String",
|
Brain/src/router/email_router.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
from fastapi import APIRouter
|
| 4 |
+
|
| 5 |
+
from Brain.src.common.assembler import Assembler
|
| 6 |
+
from Brain.src.common.brain_exception import BrainException
|
| 7 |
+
from Brain.src.model.requests.request_model import (
|
| 8 |
+
EmailReader,
|
| 9 |
+
)
|
| 10 |
+
from Brain.src.rising_plugin.gmail.manage_gmail import EmailManager
|
| 11 |
+
from Brain.src.firebase.firebase import firebase_admin_with_setting
|
| 12 |
+
|
| 13 |
+
router = APIRouter()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def construct_blueprint_email_api() -> APIRouter:
|
| 17 |
+
# Assembler
|
| 18 |
+
assembler = Assembler()
|
| 19 |
+
# Services
|
| 20 |
+
|
| 21 |
+
"""@generator.request_body(
|
| 22 |
+
{
|
| 23 |
+
"token": "String",
|
| 24 |
+
"uuid": "String",
|
| 25 |
+
"data": {
|
| 26 |
+
"sender": "test@gmail.com",
|
| 27 |
+
"pwd": "use app password of your google account",
|
| 28 |
+
"imap_folder": "inbox or drafts"
|
| 29 |
+
},
|
| 30 |
+
}
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
@generator.response(
|
| 34 |
+
status_code=200, schema={"message": "message", "result": [{
|
| 35 |
+
"From": "testfrom@test.com",
|
| 36 |
+
"To": "test@gmail.com",
|
| 37 |
+
"Date": "Tue, 04 Jul 2023 12:55:19 +0000",
|
| 38 |
+
"CC": "",
|
| 39 |
+
"Subject": "subject",
|
| 40 |
+
"Message Body": "message"
|
| 41 |
+
}]}
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
@router.post("/read_emails")
|
| 47 |
+
def read_emails(data: EmailReader):
|
| 48 |
+
# firebase admin init
|
| 49 |
+
try:
|
| 50 |
+
setting, firebase_app = firebase_admin_with_setting(data)
|
| 51 |
+
except BrainException as ex:
|
| 52 |
+
return assembler.to_response(ex.code, ex.message, "")
|
| 53 |
+
try:
|
| 54 |
+
token = setting.token
|
| 55 |
+
uuid = setting.uuid
|
| 56 |
+
|
| 57 |
+
# if imap_folder is drafts, then search is ALL
|
| 58 |
+
imap_search_cmd = "UNSEEN"
|
| 59 |
+
if data.data.imap_folder not in "inbox":
|
| 60 |
+
imap_search_cmd = "(ALL)"
|
| 61 |
+
|
| 62 |
+
# read emails
|
| 63 |
+
email_manager = EmailManager()
|
| 64 |
+
result = email_manager.read_emails(
|
| 65 |
+
sender=data.data.sender,
|
| 66 |
+
pwd=data.data.pwd,
|
| 67 |
+
imap_folder=data.data.imap_folder,
|
| 68 |
+
imap_search_command=imap_search_cmd,
|
| 69 |
+
)
|
| 70 |
+
result = json.loads(result)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
if isinstance(e, BrainException):
|
| 73 |
+
return e.get_response_exp()
|
| 74 |
+
return assembler.to_response(400, "Failed to read emails", "")
|
| 75 |
+
return assembler.to_response(200, "", result)
|
| 76 |
+
|
| 77 |
+
return router
|
app.py
CHANGED
|
@@ -6,8 +6,9 @@ import uvicorn
|
|
| 6 |
# from Brain.src.gradio_debug import debug_send_notification
|
| 7 |
from Brain.src.router.browser_router import construct_blueprint_browser_api
|
| 8 |
from Brain.src.router.train_router import construct_blueprint_train_api
|
| 9 |
-
|
| 10 |
from Brain.src.router.api import construct_blueprint_api
|
|
|
|
| 11 |
import gradio as gr
|
| 12 |
import random
|
| 13 |
import time
|
|
@@ -27,8 +28,8 @@ app.include_router(construct_blueprint_api(), tags=["ai_app"])
|
|
| 27 |
app.include_router(
|
| 28 |
construct_blueprint_browser_api(), prefix="/browser", tags=["ai_browser"]
|
| 29 |
)
|
| 30 |
-
|
| 31 |
app.include_router(construct_blueprint_train_api(), prefix="/train", tags=["ai_train"])
|
|
|
|
| 32 |
|
| 33 |
# gradio
|
| 34 |
|
|
|
|
| 6 |
# from Brain.src.gradio_debug import debug_send_notification
|
| 7 |
from Brain.src.router.browser_router import construct_blueprint_browser_api
|
| 8 |
from Brain.src.router.train_router import construct_blueprint_train_api
|
| 9 |
+
from Brain.src.router.email_router import construct_blueprint_email_api
|
| 10 |
from Brain.src.router.api import construct_blueprint_api
|
| 11 |
+
|
| 12 |
import gradio as gr
|
| 13 |
import random
|
| 14 |
import time
|
|
|
|
| 28 |
app.include_router(
|
| 29 |
construct_blueprint_browser_api(), prefix="/browser", tags=["ai_browser"]
|
| 30 |
)
|
|
|
|
| 31 |
app.include_router(construct_blueprint_train_api(), prefix="/train", tags=["ai_train"])
|
| 32 |
+
app.include_router(construct_blueprint_email_api(), prefix="/email", tags=["ai_email"])
|
| 33 |
|
| 34 |
# gradio
|
| 35 |
|