From 1df915871ac5cbb7b688c66dde8ef51a4dcd2420 Mon Sep 17 00:00:00 2001 From: NTGNguyen <23521049@gm.uit.edu.vn> Date: Fri, 3 Jan 2025 22:31:57 +0700 Subject: [PATCH] refactor(message): using a clearly class to store data instead of a complex type --- src/check_phat_nguoi/notify/message.py | 29 ++++++++++++++++++------- src/check_phat_nguoi/notify/telegram.py | 27 ++++++++++------------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/check_phat_nguoi/notify/message.py b/src/check_phat_nguoi/notify/message.py index 318708f..e441c59 100644 --- a/src/check_phat_nguoi/notify/message.py +++ b/src/check_phat_nguoi/notify/message.py @@ -1,3 +1,5 @@ +from pydantic import BaseModel, Field + from check_phat_nguoi.config import config from check_phat_nguoi.context import PlateInfoModel, PlatesModel from check_phat_nguoi.context.plate_context.models.resolution_office import ( @@ -10,6 +12,13 @@ ) +class MessagesModel(BaseModel): + plate: str = Field(description="Biển số") + messages: tuple[str, ...] = Field( + description="List chứa các string chứa các thông tin cụ thể về lỗi vi phạm sau" + ) + + class Message: def __init__(self, plates: PlatesModel): self._plates: PlatesModel = plates @@ -58,11 +67,15 @@ def _format_message( ] ) - # FIXME: complex object!! - def format_messages(self) -> dict[str, tuple[str, ...]]: - message_dict: dict[str, tuple[str, ...]] = {} - for plate_info_context in self._plates.plates: - message_dict[plate_info_context.plate] = Message._format_message( - plate_info_context, unpaid_only=config.unpaid_only - ) - return message_dict + def format_messages(self) -> tuple[MessagesModel, ...]: + return tuple( + [ + MessagesModel( + plate=plate_info_context.plate, + messages=Message._format_message( + plate_info_context, unpaid_only=config.unpaid_only + ), + ) + for plate_info_context in self._plates.plates + ] + ) diff --git a/src/check_phat_nguoi/notify/telegram.py b/src/check_phat_nguoi/notify/telegram.py index df55973..cd65fff 100644 --- a/src/check_phat_nguoi/notify/telegram.py +++ b/src/check_phat_nguoi/notify/telegram.py @@ -6,6 +6,7 @@ from check_phat_nguoi.config.dto.notify.telegram import TelegramDTO from ..constants.notify import SEND_MESSAGE_API_URL_TELEGRAM as API_URL +from .message import MessagesModel from .noti_engine import NotificationEngine logger = getLogger(__name__) @@ -16,15 +17,15 @@ class Telegram(NotificationEngine): def __init__( self, telegram: TelegramDTO, - message_dict: dict[str, tuple[str, ...]], + messages: tuple[MessagesModel, ...], ): self._telegram: TelegramDTO = telegram - self._message_dict: dict[str, tuple[str, ...]] = message_dict + self._messages: tuple[MessagesModel, ...] = messages # FIXME: Heyyyyy refactor timeout hehe self.timeout = 10 self.session: ClientSession = ClientSession() - async def _send_message(self, message: str) -> None: + async def _send_message(self, message: str, plate: str) -> None: # FIXME: specify which plate is successfully send, or fail # FIXME: use fstring instead of string format url = API_URL.format(bot_token=self._telegram.bot_token) @@ -39,31 +40,25 @@ async def _send_message(self, message: str) -> None: url, json=payload, timeout=ClientTimeout(self.timeout) ) as response: response.raise_for_status() - logger.info( - "Sending message completed for chat_id:{chat_id} and bot_token:{bot_token}".format( - chat_id=self._telegram.chat_id, - bot_token=self._telegram.bot_token, - ) + logger.debug( + f"Sending message completed for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token} with plate:{plate}" ) except asyncio.TimeoutError: logger.error( - "Time out of {self.timeout} seconds for chat_id:{chat_id} and bot_token:{bot_token}".format( - chat_id=self._telegram.chat_id, - bot_token=self._telegram.bot_token, - ) + f"Time out of {self.timeout} seconds for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token} with plate{plate}" ) except ClientConnectionError: logger.error( - f"Unable to sending message for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token}" + f"Unable to sending message for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token} with plate{plate}" ) except Exception as e: logger.error(e) async def send_messages(self) -> None: tasks = ( - self._send_message(message) - for message_tuple in self._message_dict.values() - for message in message_tuple + self._send_message(message, messages.plate) + for messages in self._messages + for message in messages.messages ) await asyncio.gather(*tasks) await self.session.close()