diff --git a/controllers_v2/message/mail/gmail.py b/controllers_v2/message/mail/gmail.py index d13e19a..23eba2e 100644 --- a/controllers_v2/message/mail/gmail.py +++ b/controllers_v2/message/mail/gmail.py @@ -42,6 +42,7 @@ from utils_v2.date_time import date_time from utils_v2.database.async_mysql_v2 import AsyncMySQL from utils_v2.database.async_mongo_v2 import AsyncMongo from utils_v2.cache.async_redis_cache_v2 import AsyncRedisCache +from utils_v2.logging.context import AsyncLoggerContext # Controllers: from controllers_v2.message.mail.base import MailController @@ -63,6 +64,9 @@ from utils_v2.goog.controllers.gmail.gmail_client import AsyncGmailClient, SCOPE from utils_v2.goog.controllers.gmail.gmail_message import GmailMessage from utils_v2.goog.models.auth_tokens import GoogleAuthTokens +# Shared: +from shared import constants + # To work with datatypes: from typing import List, Any @@ -367,6 +371,15 @@ class GmailController(MailController): # Done here: return response + @AsyncLoggerContext.log_it( + api_version = "1.0.0", + project = constants.PROJECT_NAME, + log_type = constants.MODULE_NAME, + operation = "gmailTokRefresh", + log_input = False, + log_output = False, + sensitive_keys = ["session_token"] + ) async def refresh_authorization( self, sql_conn: AsyncMySQL, diff --git a/cron/misc/reminders_for_work.py b/cron/misc/reminders_for_work.py index ff79da6..ea994f8 100644 --- a/cron/misc/reminders_for_work.py +++ b/cron/misc/reminders_for_work.py @@ -346,6 +346,7 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None: # Start with blank variables: log_id = None + api_message = None # Get a list of unique token-keys: unique_token_keys = chat_df["tokenKey"].unique().tolist() @@ -378,6 +379,7 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None: # Get the log id from the response: log_id = response.json()["logId"] + api_message = response.json()["message"] # If the API call failed: response.raise_for_status() @@ -390,7 +392,11 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None: # Send out an alert if needed: if exception is not None: await send_telegram( - message = f"*Reminders For Work (Chat)*\n\nException: `{exception}`\n\nLog Id: `{log_id}`", + message = ( + f"*Reminders For Work (Chat)*\n\nException: `{exception}`\n\n" + f"Message: `{api_message}`\n\n" + f"Log Id: `{log_id}`" + ), message_type = "error" ) @@ -398,6 +404,64 @@ async def send_reminders_by_chat(chat_df: pd.DataFrame) -> None: # --------------------------------------------------------------------------------------------------------------------- +async def send_one_reminder_by_mail(reminder_row) -> None: + + # Start with blank variables: + log_id = None + api_message = None + + # Create the input(s) needed for making the API call: + input_json = { + "tokenKey": reminder_row["tokenKey"], + "to": [reminder_row["to"]], + "subject": f"Reminder for Work - {datetime.datetime.now().strftime('%d/%m/%Y')}", + "body": [ + { + "type": "html", + "part": { + "content": reminder_row["content"] + } + } + ] + } + + # We are now ready to make the API call: + exception = None + try: + + # Make the API call: + response = await http_client.post( + url = r"https://api.thecaoffice.com/converse/mail", + json = input_json + ) + + # Get the log id from the response: + log_id = response.json()["logId"] + api_message = response.json()["message"] + + # If the API call failed: + response.raise_for_status() + + # If the API call goes wrong: + except Exception as e: + exception = e + printer(exception) + + # Send out an alert if needed: + if exception is not None: + await send_telegram( + message = ( + f"*Reminders For Work (Mail)*\n\nException: `{exception}`\n\n" + f"Message: `{api_message}`\n\n" + f"Log Id: `{log_id}`" + ), + message_type = "error" + ) + + +# --------------------------------------------------------------------------------------------------------------------- + + async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None: """ @@ -406,54 +470,58 @@ async def send_reminders_by_mail(mail_df: pd.DataFrame) -> None: :return: None """ - # Start with blank variables: - log_id = None + # Iterate over all the rows and create mail-sending tasks: + tasks = [send_one_reminder_by_mail(row) for index, row in mail_df.iterrows()] + await asyncio.gather(*tasks) + # for index, row in mail_df.iterrows(): - # Iterate over all the rows and send out the mails: - for index, row in mail_df.iterrows(): - - # Create the input(s) needed for making the API call: - input_json = { - "tokenKey": row["tokenKey"], - "to": [row["to"]], - "subject": f"Reminder for Work - {datetime.datetime.now().strftime('%d/%m/%Y')}", - "body": [ - { - "type": "html", - "part": { - "content": row["content"] - } - } - ] - } - - # We are now ready to make the API call: - exception = None - try: - - # Make the API call: - response = await http_client.post( - url = r"https://api.thecaoffice.com/converse/mail", - json = input_json - ) - - # Get the log id from the response: - log_id = response.json()["logId"] - - # If the API call failed: - response.raise_for_status() - - # If the API call goes wrong: - except Exception as e: - exception = e - printer(exception) - - # Send out an alert if needed: - if exception is not None: - await send_telegram( - message = f"*Reminders For Work (Mail)*\n\nException: `{exception}`\n\nLog Id: `{log_id}`", - message_type = "error" - ) + # # Create the input(s) needed for making the API call: + # input_json = { + # "tokenKey": row["tokenKey"], + # "to": [row["to"]], + # "subject": f"Reminder for Work - {datetime.datetime.now().strftime('%d/%m/%Y')}", + # "body": [ + # { + # "type": "html", + # "part": { + # "content": row["content"] + # } + # } + # ] + # } + # + # # We are now ready to make the API call: + # exception = None + # try: + # + # # Make the API call: + # response = await http_client.post( + # url = r"https://api.thecaoffice.com/converse/mail", + # json = input_json + # ) + # + # # Get the log id from the response: + # log_id = response.json()["logId"] + # api_message = response.json()["message"] + # + # # If the API call failed: + # response.raise_for_status() + # + # # If the API call goes wrong: + # except Exception as e: + # exception = e + # printer(exception) + # + # # Send out an alert if needed: + # if exception is not None: + # await send_telegram( + # message = ( + # f"*Reminders For Work (Mail)*\n\nException: `{exception}`\n\n" + # f"Message: `{api_message}`\n\n" + # f"Log Id: `{log_id}`" + # ), + # message_type = "error" + # ) # ---------------------------------------------------------------------------------------------------------------------