From 01156111712b96302cbe587064e76571f1f7eab0 Mon Sep 17 00:00:00 2001 From: Kevin Nitro Date: Thu, 16 Jan 2025 12:49:03 +0700 Subject: [PATCH] fix: use apis list, not allow single api --- config.sample.json | 6 +++--- src/check_phat_nguoi/config/models/config.py | 2 +- .../config/models/plate_info.py | 18 ++++++++++-------- src/check_phat_nguoi/get_data/get_data.py | 10 +--------- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/config.sample.json b/config.sample.json index 9aba2dd..381df08 100644 --- a/config.sample.json +++ b/config.sample.json @@ -18,13 +18,13 @@ { "plate": "60A64685", "owner": "dad", - "api": "checkphatnguoi.vn", + "apis": "checkphatnguoi.vn", "type": 1 }, { "plate": "98A56604", "type": "car", - "api": ["csgt.vn", "checkphatnguoi.vn"] + "apis": ["csgt.vn", "checkphatnguoi.vn"] }, { "plate": "30F88251", @@ -32,7 +32,7 @@ "enable": false } ], - "api": ["phatnguoi.vn", "checkphatnguoi.vn", "phatnguoi.vn", "csgt.vn"], + "apis": ["phatnguoi.vn", "checkphatnguoi.vn", "phatnguoi.vn", "csgt.vn"], "notifications": [ { "telegram": { diff --git a/src/check_phat_nguoi/config/models/config.py b/src/check_phat_nguoi/config/models/config.py index 8f51330..0d5e479 100644 --- a/src/check_phat_nguoi/config/models/config.py +++ b/src/check_phat_nguoi/config/models/config.py @@ -31,7 +31,7 @@ class Config(BaseModel): description="Danh sách các thiết lập để thông báo", default_factory=tuple, ) - api: tuple[ApiEnum, ...] | ApiEnum = Field( + apis: tuple[ApiEnum, ...] = Field( title="API", description="Sử dụng API từ trang web nào. Mặc định sẽ là list các API như trong schema hiển thị và dừng khi 1 API lấy dữ liệu thành công. Có thể điền giá trị trùng để retry. Hoặc chỉ dùng 1 API", default=(ApiEnum.phatnguoi_vn, ApiEnum.checkphatnguoi_vn), diff --git a/src/check_phat_nguoi/config/models/plate_info.py b/src/check_phat_nguoi/config/models/plate_info.py index 9f2b7f8..732ad83 100644 --- a/src/check_phat_nguoi/config/models/plate_info.py +++ b/src/check_phat_nguoi/config/models/plate_info.py @@ -30,7 +30,7 @@ class PlateInfo(BaseModel): description="Kích hoạt", default=True, ) - api: tuple[ApiEnum, ...] | ApiEnum | None = Field( + apis: tuple[ApiEnum, ...] | None = Field( description='Sử dụng API từ trang web nào. Config giống "api" ở ngoài .Để trống sẽ sử dụng API define ở scope ngoài.', title="API", default=None, @@ -49,7 +49,7 @@ def __hash__(self) -> int: hash(self.plate) + hash(self.type) + hash(self.enabled) - + hash(self.api) + + hash(self.apis) + hash(self.owner) ) @@ -65,14 +65,16 @@ def __eq__(self, other: Any): all( x == y for x, y in zip( - (self.api,) if isinstance(self.api, ApiEnum) else self.api, - (other.api,) - if isinstance(other.api, ApiEnum) - else other.api, + (self.apis,) + if isinstance(self.apis, ApiEnum) + else self.apis, + (other.apis,) + if isinstance(other.apis, ApiEnum) + else other.apis, ) ) - if self.api and other.api - else (not self.api and not other.api) + if self.apis and other.apis + else (not self.apis and not other.apis) ) ) return False diff --git a/src/check_phat_nguoi/get_data/get_data.py b/src/check_phat_nguoi/get_data/get_data.py index e9939a8..52ee591 100644 --- a/src/check_phat_nguoi/get_data/get_data.py +++ b/src/check_phat_nguoi/get_data/get_data.py @@ -24,15 +24,7 @@ def __init__(self) -> None: async def _get_data_for_plate(self, plate_info: PlateInfo) -> None: # NOTE: The config has constraint that config.api will be at least 1 api in tuple - apis: tuple[ApiEnum, ...] = ( - plate_info.api - if isinstance(plate_info.api, tuple) - else (plate_info.api,) - if plate_info.api - else config.api - if isinstance(config.api, tuple) - else (config.api,) - ) + apis: tuple[ApiEnum, ...] = plate_info.apis if plate_info.apis else config.apis engine: BaseGetDataEngine for api in apis: match api: