Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
chore(logging): add logger to enhance debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
NTGNguyen committed Dec 24, 2024
1 parent e5e94aa commit 1928e0b
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/check_phat_nguoi/modules/get_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
from check_phat_nguoi.models.plate_info import PlateInfo
from check_phat_nguoi.utils.constants import URL

from logging import getLogger

logger = getLogger(__name__)


class GetData:
"""Get data by sending a request"""
Expand All @@ -22,11 +26,12 @@ def __init__(self, plate_infos: list[PlateInfo]) -> None:
self._plate_infos: list[PlateInfo] = plate_infos
self.data_dict: Dict[str, None | Dict] = {}

def _get_data(self, plate: str) -> None:
def _get_data(self, plate: str, timeout: int = 5) -> None:
"""Get data with a single object
Args:
plate: plate's information
timeout: maximum wait time in seconds(default is 5)
Returns:
None | Dict: a dict
Expand All @@ -35,32 +40,35 @@ def _get_data(self, plate: str) -> None:
try:
response: Response = requests.post(url=URL, json=payload)
response.raise_for_status()

logger.info(f"Request successful: {response.status_code}")

response_data: Dict = response.json()
if response_data.get("data") is None:
self.data_dict[plate] = None
else:
self.data_dict[plate] = response_data
except Exception:
self._data_dict[plate] = None
except requests.exceptions.ConnectionError:
logger.error(f"Unable to connect to{URL}")
except requests.exceptions.Timeout:
logger.error(f"Time out of {timeout} seconds from URL {URL}")

def get_data(self) -> Dict[str, None | Dict] | None:
"""Get data
Returns:
Dict: A dictionary mapping plate to response data.
"""

threads: list[Thread] = []
try:
for plate_info in self._plate_infos:
thread = Thread(target=self._get_data,
args=(plate_info.plate,))
threads.append(thread)
thread.start()

for thread in threads:
for plate_info in self._plate_infos:
thread = Thread(target=self._get_data,
args=(plate_info.plate,))
threads.append(thread)
thread.start()
for idx, thread in enumerate(threads, start=1):
try:
thread.join()
except Exception:
logger.error(f"An error occurs in thread number {idx}")

return self.data_dict
except Exception:
return None
return self.data_dict

0 comments on commit 1928e0b

Please sign in to comment.