(20250120) Mail sending API ready.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
DATE:
|
||||
|
||||
Thursday, 19th Dec., 2024
|
||||
Monday, 20th Jan., 2025.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
@@ -45,6 +45,7 @@ from quart import Blueprint, current_app, g, request
|
||||
# My utils:
|
||||
from utils_v2.string import json
|
||||
from utils_v2.database.async_mongo_v2 import AsyncMongo
|
||||
from utils_v2.logging.context import AsyncLoggerContext
|
||||
from utils_v2.api.codes import StatusCodes, HttpCodes
|
||||
from utils_v2.api.response import ResponseModel
|
||||
from utils_v2.api.async_quart import (
|
||||
@@ -63,6 +64,7 @@ from utils_v2.api.async_quart import (
|
||||
|
||||
# GMail-related utils:
|
||||
from utils_v2.goog.controllers.gmail.gmail_client import SCOPES_GMAIL_MAIL_MANAGEMENT
|
||||
from utils_v2.goog.controllers.gmail.gmail_message import GmailMessage
|
||||
from utils_v2.goog.models.auth_tokens import GoogleAuthTokens
|
||||
|
||||
# Common:
|
||||
@@ -70,7 +72,9 @@ from shared import constants
|
||||
|
||||
# Data Models:
|
||||
from models.api.message.mail.send import MailSendRequestHeaders, MailSendRequestData
|
||||
from models.message.mail.send import MailSendOneResult
|
||||
from models.core.user import CoreUserInfoModel
|
||||
from models.core.auth_token import CoreAuthTokenModel
|
||||
|
||||
# To work with datatypes:
|
||||
from typing import Literal
|
||||
@@ -124,6 +128,50 @@ def init(blueprint_setup_state):
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@AsyncLoggerContext.log_it(
|
||||
api_version = "1.0.0",
|
||||
project = constants.PROJECT_NAME,
|
||||
log_type = constants.MODULE_NAME,
|
||||
operation = "mailGmailMsgCreate",
|
||||
log_input = 2,
|
||||
log_output = 1,
|
||||
sensitive_keys = ["sessionToken", "X-Session-Token"]
|
||||
)
|
||||
async def create_gmail_mail_message(
|
||||
from_email: str,
|
||||
inbound_data: MailSendRequestData
|
||||
) -> GmailMessage:
|
||||
|
||||
"""
|
||||
To create a mail message object of to be sent via Gmail.
|
||||
:param from_email: The e-mail id of the sender.
|
||||
:param inbound_data: The data that came in with the request.
|
||||
:return: The mail message object that can be sent via Gmail.
|
||||
"""
|
||||
|
||||
# Create the instance of the message:
|
||||
mail_message = GmailMessage(
|
||||
from_email = from_email,
|
||||
to_email = inbound_data.to,
|
||||
subject = inbound_data.subject,
|
||||
cc_emails = inbound_data.cc,
|
||||
bcc_emails = inbound_data.bcc
|
||||
)
|
||||
|
||||
# Add all the parts one-by-one:
|
||||
for part in inbound_data.body:
|
||||
if part.type == "plain": mail_message.add_text(part.part.content)
|
||||
elif part.type == "html": mail_message.add_html(part.part.content)
|
||||
elif part.type == "inline": mail_message.add_inline_image(part.part.content, part.part.fileName, part.part.cid)
|
||||
elif part.type == "attachment": mail_message.add_attachment(part.part.content, part.part.fileName)
|
||||
|
||||
# Done here:
|
||||
return mail_message
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@mail_send_bp.route("", methods = ["POST"])
|
||||
@set_api_version(api_version = "1.0.0")
|
||||
@read_input(sanitize_headers = False, sanitize_data = False)
|
||||
@@ -133,9 +181,9 @@ def init(blueprint_setup_state):
|
||||
project = constants.PROJECT_NAME,
|
||||
log_type = constants.MODULE_NAME,
|
||||
operation = "mailSendApi",
|
||||
log_input = True,
|
||||
log_input = 1,
|
||||
log_output = True,
|
||||
sensitive_keys = ["sessionToken", "X-Session-Token"]
|
||||
sensitive_keys = ["sessionToken", "X-Session-Token", "tokenKey", "tokenId"]
|
||||
)
|
||||
@log_chain_to_mongo(attr_name = "logs_mongo")
|
||||
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
|
||||
@@ -161,7 +209,10 @@ async def send_one_mail(
|
||||
"""
|
||||
|
||||
# Start by assuming failure:
|
||||
success = False
|
||||
client_controller = None
|
||||
client_connector = None
|
||||
mail_message = None
|
||||
send_result = MailSendOneResult()
|
||||
|
||||
# ┏┓ ┓ ┏┓┓ ┓
|
||||
# ┣┫┓┏╋┣┓ ┃ ┣┓┏┓┏┃┏
|
||||
@@ -181,18 +232,15 @@ async def send_one_mail(
|
||||
|
||||
# Get the token based on the key:
|
||||
auth_token = await current_app.mail_controller.get_token_from_key(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
token_key = inbound_data.tokenKey
|
||||
)
|
||||
|
||||
print("INBOUND DATA:", json.to_string(inbound_data.model_dump(), default=str))
|
||||
print("INBOUND FILES:", json.to_string(inbound_files, default=str))
|
||||
print("AUTH TOKEN:", json.to_string(auth_token, default=str))
|
||||
user_info = CoreUserInfoModel(**kwargs["session_info"])
|
||||
|
||||
# We check if the token that was used to fetch the mail is owned by this user:
|
||||
if not await token_check.is_authorized(
|
||||
mongo_conn = current_app.data_mongo,
|
||||
user_info = CoreUserInfoModel(**kwargs["session_info"]),
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
user_info = user_info,
|
||||
token_ids = [auth_token.authTokenId]
|
||||
): return ResponseModel(
|
||||
status_code = StatusCodes.FAILED,
|
||||
@@ -204,7 +252,34 @@ async def send_one_mail(
|
||||
# ┗┓┏┓┏┓┏┫ ┃┃┃┏┓┓┃
|
||||
# ┗┛┗ ┛┗┗┻ ┛ ┗┗┻┗┗
|
||||
|
||||
pass
|
||||
# Figure out the client connector:
|
||||
match auth_token.client:
|
||||
case "gmail":
|
||||
client_controller = current_app.gmail_controller
|
||||
client_connector = current_app.gmail_client
|
||||
mail_message = await create_gmail_mail_message(
|
||||
from_email = auth_token.clientUserId["email"],
|
||||
inbound_data = inbound_data
|
||||
)
|
||||
case _:
|
||||
client_controller = None
|
||||
client_connector = None
|
||||
mail_message = None
|
||||
send_result.message = f"Invalid/unimplemented client '{auth_token.client}'"
|
||||
|
||||
# If the controller and connector were matched:
|
||||
if client_controller is not None and client_connector is not None:
|
||||
send_result = await client_controller.send_mail(
|
||||
sql_conn = current_app.sql_writer,
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
mail_client = client_connector,
|
||||
mail_message = mail_message,
|
||||
auth_token = auth_token,
|
||||
client_thread_id = inbound_data.clientThreadId,
|
||||
user_info = user_info,
|
||||
llm = current_app.llm,
|
||||
session_token = inbound_headers["X-Session-Token"]
|
||||
)
|
||||
|
||||
# ┳┓
|
||||
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
|
||||
@@ -213,8 +288,9 @@ async def send_one_mail(
|
||||
|
||||
# Done here:
|
||||
return ResponseModel(
|
||||
status_code = StatusCodes.OK if success else StatusCodes.FAILED,
|
||||
http_code = HttpCodes.SUCCESS if success else HttpCodes.INTERNAL_SERVER_ERROR
|
||||
status_code = StatusCodes.OK if send_result.success else StatusCodes.FAILED,
|
||||
http_code = HttpCodes.SUCCESS if send_result.success else HttpCodes.INTERNAL_SERVER_ERROR,
|
||||
message = send_result.message
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -440,6 +440,7 @@ async def app_startup(**kwargs):
|
||||
alert_url = current_app.script_data["alerts"]["url"],
|
||||
debug = enable_debugging
|
||||
)
|
||||
current_app.printer("Auth-Token (C) ready.")
|
||||
|
||||
# Messages / SMS Controllers:
|
||||
current_app.sms_controller = AllSMSController(
|
||||
@@ -460,6 +461,7 @@ async def app_startup(**kwargs):
|
||||
alert_url = current_app.script_data["alerts"]["url"],
|
||||
debug = enable_debugging
|
||||
)
|
||||
current_app.printer("Message/SMS (C) ready.")
|
||||
|
||||
# Messages / Mail Controllers:
|
||||
current_app.mail_controller = AllMailController(
|
||||
@@ -474,6 +476,7 @@ async def app_startup(**kwargs):
|
||||
alert_url = current_app.script_data["alerts"]["url"],
|
||||
debug = enable_debugging
|
||||
)
|
||||
current_app.printer("Message/Mail (C) ready.")
|
||||
|
||||
# Messages / Chat Controllers:
|
||||
current_app.chat_controller = AllChatController(
|
||||
@@ -488,6 +491,7 @@ async def app_startup(**kwargs):
|
||||
alert_url = current_app.script_data["alerts"]["url"],
|
||||
debug = enable_debugging
|
||||
)
|
||||
current_app.printer("Message/Chat (C) ready.")
|
||||
|
||||
# Finstitutions / Trading Controllers:
|
||||
current_app.trading_controller = AllTradingController(
|
||||
@@ -514,6 +518,7 @@ async def app_startup(**kwargs):
|
||||
alert_url = current_app.script_data["alerts"]["url"],
|
||||
debug = enable_debugging
|
||||
)
|
||||
current_app.printer("Finstitutions/Trading (C) ready.")
|
||||
|
||||
# Finstitutions / Payments Controllers:
|
||||
current_app.payments_controller = AllPaymentsController(
|
||||
@@ -528,6 +533,7 @@ async def app_startup(**kwargs):
|
||||
alert_url = current_app.script_data["alerts"]["url"],
|
||||
debug = enable_debugging
|
||||
)
|
||||
current_app.printer("Finstitutions/Payments (C) ready.")
|
||||
|
||||
# ┏┓ ┓ ┏┓┓•
|
||||
# ┃ ┏┓┏┓┏┓┏┓┏╋┏┓┏┓┏ ┏┓┏┓┏┫ ┃ ┃┓┏┓┏┓╋┏
|
||||
|
||||
Reference in New Issue
Block a user