Resetting utils subtree.

This commit is contained in:
2024-12-05 14:47:16 +05:30
parent 6e6e6be87c
commit 75b4bf541b
151 changed files with 139 additions and 125374 deletions
+1
View File
@@ -181,6 +181,7 @@ async def request_oauth_authorization_url(
session_token = inbound_headers["X-Session-Token"],
user_info = kwargs["session_info"],
client_user_id = {"email": inbound_data.mailId},
auth = None,
service_client = inbound_data.mailClient,
auth_type = "oauth",
sync_freq = inbound_data.syncFreq
+46 -39
View File
@@ -112,7 +112,7 @@ def init(blueprint_setup_state):
# ---------------------------------------------------------------------------------------------------------------------
@sms_auth_bp.route("/auth", methods = ["GET"])
@sms_auth_bp.route("/auth", methods = ["POST"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
@get_session_info(key = "X-Session-Token", session_coro = "get_session")
@@ -129,7 +129,7 @@ def init(blueprint_setup_state):
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@validate_input(
header_validator = lambda x: SMSAuthRequestHeaders(**x).model_dump(),
data_validator = lambda x: SMSAuthRequestData(**{"client": x})
data_validator = lambda x: SMSAuthRequestData(**x)
)
@handle_cancelled_request()
async def request_oauth_authorization_url(
@@ -161,45 +161,52 @@ async def request_oauth_authorization_url(
)
# Start by assuming failure:
auth_success = None
token_id = None
# ┳ ┓ •┏ ┳┳
# ┃┏┫┏┓┏┓╋┓╋┓┏ ┃┃┏┏┓┏┓
# ┻┗┻┗ ┛┗┗┗┛┗ ┗┛┛┗ ┛
# ┛
# ┏┓ ┳┓• ┏┓┳┳┓┏┓ ┳ ┓•
# ┣ ┏┓┏┓ ┃┃┓┏┳┓┣┓┓┏┏ ┗┓┃┃┃┗┓ ┃┏┓┏┫┓┏┓
# ┻ ┗┛┛ ┛┗┗┛┗┗┗┛┗┻┛ ┗┛┛ ┗┗┛ ┻┛┗┗┻┗┗┻
# Make a user identifier from the session info:
user_identifier = await current_app.mail_oauth_model.get_account_identifier(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
session_token = inbound_headers["X-Session-Token"],
user_info = kwargs["session_info"],
email_id = inbound_data.mailId,
service_client = inbound_data.mailClient,
auth_type = "oauth"
)
if user_identifier is None:
return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.INTERNAL_SERVER_ERROR,
message = "failed to generate user identifier"
if inbound_data.messageClient == "nimbusSmsIndia":
token_id = await current_app.sms_auth_model.set(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
user_info = kwargs["session_info"],
client_user_id = {
"userId": inbound_data.auth.userId,
"senderId": inbound_data.auth.senderId,
"entityId": inbound_data.auth.enttiyId
},
auth = inbound_data.auth.model_dump(),
token = None,
service_client = inbound_data.messageClient,
auth_type = "auth",
sync_freq = 300,
session_token = inbound_headers["X-Session-Token"]
)
user_identifier = str(user_identifier)
# ┏┓ ┏┓┳┳
# ┣ ┏┓┏┓ ┃┓┃┃┃┏┓┓┃
# ┻ ┗┛┛ ┗┛┛ ┗┗┻┗┗
# ┏┓ ┏┓ ┳┓ ┓┓ ┏┓┳┳┓┏┓┏
# ┣ ┏┓┏┓ ┗┓┏┓┓┏┓┏┓┏ ┣┫┓┏┃┃┏ ┗┓┃┃┃┗┓ ┃┫ ┏┓┏┓┓┏┏┓
# ┻ ┗┛┛ ┗┛┗┻┗┛┗┛┗┫ ┻┛┗┻┗┛┗ ┗┛┛ ┗┗┛ ┛┗┛┗ ┛┗┗┫┗┻
# ┛ ┛
if inbound_data.mailClient == "gmail":
if inbound_data.messageClient == "savvyBulkSmsKenya":
# Get the authorization URL:
auth_url = await current_app.gmail_client.get_authorization_url(
scopes = SCOPES_GMAIL_MAIL_MANAGEMENT,
state = user_identifier,
access_type = "offline",
approval_prompt = "force",
include_granted_scopes = "true",
user_email = inbound_data.mailId
token_id = await current_app.sms_auth_model.set(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
user_info = kwargs["session_info"],
client_user_id = {
"partnerId": inbound_data.auth.partnerId,
"shortCode": inbound_data.auth.shortCode
},
auth = inbound_data.auth.model_dump(),
token = None,
service_client = inbound_data.messageClient,
auth_type = "auth",
sync_freq = 300,
session_token = inbound_headers["X-Session-Token"]
)
# ┳┓
@@ -209,11 +216,11 @@ async def request_oauth_authorization_url(
# Done here:
return ResponseModel(
status_code = StatusCodes.OK if auth_url else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if auth_url else HttpCodes.INTERNAL_SERVER_ERROR,
status_code = StatusCodes.OK if token_id else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if token_id else HttpCodes.INTERNAL_SERVER_ERROR,
data = {
"mailClient": inbound_data.mailClient,
"authorizationUrl": auth_url
"messageClient": inbound_data.messageClient,
"authorized": True
}
)
+37 -1
View File
@@ -74,6 +74,7 @@ from utils_v2.goog.gmail.gmail_client import AsyncGMailClient
from models.behaviour.mail.oauth_v2 import MailOAuthModel
from models.behaviour.mail.sync_v2 import MailSyncModel
from models.behaviour.mail.retrieve import MailRetrieveModel
from models.behaviour.sms.auth import SMSAuthModel
# To make REST API calls:
import httpx
@@ -87,6 +88,7 @@ from api.blueprints.mail.oauth_callback import mail_callback_bp
from api.blueprints.mail.sync import mail_sync_bp
from api.blueprints.mail.list import mail_list_bp
from api.blueprints.mail.retrieve import mail_retrieve_bp
from api.blueprints.sms.auth import sms_auth_bp
from api.blueprints.tech.chat_alerts import tech_chat_alert_bp
from api.blueprints.test.callback import test_callback_bp
@@ -124,6 +126,7 @@ app.register_blueprint(mail_callback_bp, url_prefix = f"/{MODULE_BASE}/mail")
app.register_blueprint(mail_sync_bp, url_prefix = f"/{MODULE_BASE}/mail")
app.register_blueprint(mail_list_bp, url_prefix = f"/{MODULE_BASE}/mail")
app.register_blueprint(mail_retrieve_bp, url_prefix = f"/{MODULE_BASE}/mail")
app.register_blueprint(sms_auth_bp, url_prefix = f"/{MODULE_BASE}/sms")
app.register_blueprint(tech_chat_alert_bp, url_prefix = f"/{MODULE_BASE}/tech/alert")
app.register_blueprint(test_callback_bp, url_prefix = f"/{MODULE_BASE}/test")
@@ -205,19 +208,25 @@ async def app_startup(**kwargs):
# ┃ ┏┓┏┓┏┫ ┏┓┏┓┏┫ ┃┃┏┓╋┏┓
# ┗┛┛ ┗ ┗┻ ┗┻┛┗┗┻ ┻┛┗┻┗┗┻
# Get the script credentials and data:
# Get the script credentials:
script_id = os.environ["SCRIPT_ID"]
response = await current_app.http_client.get(
url = r"https://nexcom.ditscentre.in/internal/cred/get",
headers = {"X-Script-Id": script_id}
)
if response.status_code not in [200]: print("CRITICAL: SCRIPT CREDENTIALS LOADING FAILED!")
script_cred = response.json().get("data")
# Get the script data:
response = await current_app.http_client.get(
url = r"https://nexcom.ditscentre.in/internal/data/get",
headers = {"X-Script-Id": script_id}
)
if response.status_code not in [200]: print("CRITICAL: SCRIPT DATA LOADING FAILED!")
current_app.script_data = response.json().get("data")
current_app.printer("Cred and Data loaded.")
# ┏┓┏┓┳┳ ┏┓┏┏• •
# ┃ ┃┃┃┃ ┣┫╋╋┓┏┓┓╋┓┏
# ┗┛┣┛┗┛ ┛┗┛┛┗┛┗┗┗┗┫
@@ -227,6 +236,8 @@ async def app_startup(**kwargs):
try: set_cpu_affinity(script_cred["cpuAffinity"])
except Exception as exception: current_app.printer(exception)
current_app.printer("CPU affinity set.")
# ┳┓ ┓• ┏┓ ┓
# ┣┫┏┓┏┫┓┏ ━━ ┃ ┏┓┏┣┓┏┓
# ┛┗┗ ┗┻┗┛ ┗┛┗┻┗┛┗┗
@@ -244,6 +255,8 @@ async def app_startup(**kwargs):
debug_prefix = "User Cache | "
)
current_app.printer("Redis cache ready.")
# ┳┳┓ • ┳┓┳┓
# ┃┃┃┏┓┏┓┓┏┓┃┃┣┫
# ┛ ┗┗┻┛ ┗┗┻┻┛┻┛
@@ -266,6 +279,8 @@ async def app_startup(**kwargs):
)
await current_app.sql_reader.connect()
current_app.printer("MariaDB ready.")
# ┳┳┓
# ┃┃┃┏┓┏┓┏┓┏┓
# ┛ ┗┗┛┛┗┗┫┗┛
@@ -294,6 +309,8 @@ async def app_startup(**kwargs):
)
await current_app.data_mongo.connect()
current_app.printer("MongoDB ready.")
# ┳ ┓ ┳┳┓ ┓ ┓
# ┃┏┓╋┏┓┏┓┏┓┏┓┃ ┃┃┃┏┓┏┫┏┓┃┏
# ┻┛┗┗┗ ┛ ┛┗┗┻┗ ┛ ┗┗┛┗┻┗ ┗┛
@@ -323,6 +340,17 @@ async def app_startup(**kwargs):
debug_only_errors = True
)
current_app.sms_auth_model = SMSAuthModel(
cache = current_app.module_cache,
alert_url = current_app.script_data["alerts"]["url"],
http_client = current_app.http_client,
debug = enable_debugging,
debug_prefix = "SMS-Auth | ",
debug_only_errors = True
)
current_app.printer("Internal models ready.")
# ┏┓ ┓ ┏┓┓•
# ┃ ┏┓┏┓┏┓┏┓┏╋┏┓┏┓┏ ┏┓┏┓┏┫ ┃ ┃┓┏┓┏┓╋┏
# ┗┛┗┛┛┗┛┗┗ ┗┗┗┛┛ ┛ ┗┻┛┗┗┻ ┗┛┗┗┗ ┛┗┗┛
@@ -338,6 +366,8 @@ async def app_startup(**kwargs):
debug_only_errors = False
)
current_app.printer("Connectors and Clients ready.")
# ┏┓┳ ┳┳┓ •
# ┣┫┃ ┃┃┃┏┓┏┓┓┏
# ┛┗┻ ┛ ┗┗┻┗┫┗┗
@@ -349,6 +379,12 @@ async def app_startup(**kwargs):
openai_api_key = script_cred["openAi"]["openai_api_key"]
)
current_app.printer("AI ready.")
# ┳┳┓•
# ┃┃┃┓┏┏
# ┛ ┗┗┛┗
# Pick the important stuff:
current_app.whitelisted_ips = current_app.script_data["whitelistedIps"]