From d8820b0d13d32cc544ff183bce03fe518f1ae213 Mon Sep 17 00:00:00 2001 From: NTGNguyen <23521049@gm.uit.edu.vn> Date: Mon, 30 Dec 2024 19:47:41 +0700 Subject: [PATCH] fix(GetData): resolve mutiples error of async in GetData --- src/check_phat_nguoi/__init__.py | 11 ++++++++++- src/check_phat_nguoi/config/dto/plate_info.py | 8 ++++++++ src/check_phat_nguoi/get_data/check_phat_nguoi.py | 15 ++++++++++----- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/check_phat_nguoi/__init__.py b/src/check_phat_nguoi/__init__.py index a7a9db2..8044072 100644 --- a/src/check_phat_nguoi/__init__.py +++ b/src/check_phat_nguoi/__init__.py @@ -1,15 +1,24 @@ +import asyncio from logging import Logger, getLogger from check_phat_nguoi.config import config +from check_phat_nguoi.config.config_reader import _config_reader +from check_phat_nguoi.get_data.check_phat_nguoi import GetDataCheckPhatNguoi from .utils.setup_logger import setup_logger logger: Logger = getLogger(__name__) -def main() -> None: +async def _main(): setup_logger() logger.debug(config) + get_data_object = GetDataCheckPhatNguoi(_config_reader().data) + data = await get_data_object.get_data() + + +def main(): + asyncio.run(_main()) __all__ = ["main"] diff --git a/src/check_phat_nguoi/config/dto/plate_info.py b/src/check_phat_nguoi/config/dto/plate_info.py index 60cf0f0..b8b2a9f 100644 --- a/src/check_phat_nguoi/config/dto/plate_info.py +++ b/src/check_phat_nguoi/config/dto/plate_info.py @@ -14,5 +14,13 @@ class PlateInfoDTO(BaseModel): default=None, ) + def __hash__(self): + return hash(self.plate) + + def __eq__(self, other): + if isinstance(other, PlateInfoDTO): + return self.plate == other.plate + return False + __all__ = ["PlateInfoDTO"] diff --git a/src/check_phat_nguoi/get_data/check_phat_nguoi.py b/src/check_phat_nguoi/get_data/check_phat_nguoi.py index 48d6275..3ccaecf 100644 --- a/src/check_phat_nguoi/get_data/check_phat_nguoi.py +++ b/src/check_phat_nguoi/get_data/check_phat_nguoi.py @@ -1,4 +1,5 @@ import asyncio +import json import re from datetime import datetime from logging import getLogger @@ -28,14 +29,14 @@ def __init__( ) -> None: super().__init__(plate_infos) self.data_dict: Dict[PlateInfoDTO, None | Dict] = {} - self.session = ClientSession() self.timeout = timeout self.headers = {"Content-Type": "application/json"} async def _get_data_request(self, plate_info_object: PlateInfoDTO) -> None: payload: dict[str, str] = {"bienso": plate_info_object.plate} + session = ClientSession() try: - async with self.session.post( + async with session.post( API_URL, headers=self.headers, json=payload, @@ -43,7 +44,8 @@ async def _get_data_request(self, plate_info_object: PlateInfoDTO) -> None: ) as response: response.raise_for_status() - response_data: Dict = await response.json() + response_data = await response.read() + response_data = json.loads(response_data) self.data_dict[plate_info_object] = response_data except asyncio.TimeoutError: logger.error( @@ -53,6 +55,8 @@ async def _get_data_request(self, plate_info_object: PlateInfoDTO) -> None: logger.error( f"Error occurs while connecting to {API_URL} for plate: {plate_info_object.plate}" ) + finally: + await session.close() async def _get_data(self) -> None: tasks = [self._get_data_request(plate_info) for plate_info in self._plate_infos] @@ -64,6 +68,8 @@ def get_plate_violation( ) -> tuple[ViolationModel, ...]: if plate_violation_dict is None: return () + if plate_violation_dict["data"] is None: + return () def _create_resolution_office_mode( resolution_offices: list[str], @@ -100,7 +106,7 @@ def _create_violation_model(data: Dict): date=datetime.strptime(data["Thời gian vi phạm"], DATETIME_FORMAT), location=data["Địa điểm vi phạm"], action=data["Hành vi vi phạm"], - status=data["Trạng thái"], + status=False if data["Trạng thái"] == "Chưa xử phạt" else True, enforcement_unit=data["Đơn vị phát hiện vi phạm"], resolution_office=_create_resolution_office_mode( data["Nơi giải quyết vụ việc"] @@ -125,6 +131,5 @@ async def get_data(self) -> tuple[PlateInfoModel, ...]: ) for plate_info_object, plate_violation_dict in self.data_dict.items() ) - await self.session.close() return plate_infos