From 5a3f54c3a767c121ce12aa43a159e86d50aa23ac Mon Sep 17 00:00:00 2001 From: Zakhar Izmaylov Date: Thu, 23 Jan 2025 10:24:16 +0300 Subject: [PATCH] Refactor client management in key_utils.py and trial_key.py to utilize ClientConfig dataclass for improved parameter handling. This change enhances code clarity and maintainability by consolidating client configuration parameters into a single structured object, streamlining the add_client function calls across multiple handlers. --- handlers/keys/key_utils.py | 46 +++++++++++++++++++++----------------- handlers/keys/trial_key.py | 24 +++++++++++--------- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/handlers/keys/key_utils.py b/handlers/keys/key_utils.py index 3657f77f..98877801 100644 --- a/handlers/keys/key_utils.py +++ b/handlers/keys/key_utils.py @@ -2,7 +2,7 @@ import asyncio from py3xui import AsyncApi -from client import add_client, delete_client, extend_client_key +from client import ClientConfig, add_client, delete_client, extend_client_key from config import ADMIN_PASSWORD, ADMIN_USERNAME, LIMIT_IP, SUPERNODE, TOTAL_GB from database import get_servers_from_db from logger import logger @@ -85,16 +85,18 @@ async def create_client_on_server( await add_client( xui, - client_id, - unique_email, - tg_id, - limit_ip=LIMIT_IP, - total_gb=TOTAL_GB, - expiry_time=expiry_timestamp, - enable=True, - flow="xtls-rprx-vision", - inbound_id=int(inbound_id), - sub_id=sub_id, + ClientConfig( + client_id=client_id, + email=unique_email, + tg_id=tg_id, + limit_ip=LIMIT_IP, + total_gb=TOTAL_GB, + expiry_time=expiry_timestamp, + enable=True, + flow="xtls-rprx-vision", + inbound_id=int(inbound_id), + sub_id=sub_id, + ), ) if SUPERNODE: @@ -208,16 +210,18 @@ async def update_key_on_cluster(tg_id, client_id, email, expiry_time, cluster_id tasks.append( add_client( xui, - client_id, - email, - tg_id, - limit_ip=LIMIT_IP, - total_gb=TOTAL_GB, - expiry_time=expiry_time, - enable=True, - flow="xtls-rprx-vision", - inbound_id=int(inbound_id), - sub_id=email, + ClientConfig( + client_id=client_id, + email=email, + tg_id=tg_id, + limit_ip=LIMIT_IP, + total_gb=TOTAL_GB, + expiry_time=expiry_time, + enable=True, + flow="xtls-rprx-vision", + inbound_id=int(inbound_id), + sub_id=email, + ), ) ) diff --git a/handlers/keys/trial_key.py b/handlers/keys/trial_key.py index 00048577..c54b31a5 100644 --- a/handlers/keys/trial_key.py +++ b/handlers/keys/trial_key.py @@ -6,7 +6,7 @@ from typing import Any import pytz from py3xui import AsyncApi -from client import add_client +from client import ClientConfig, add_client from config import ADMIN_PASSWORD, ADMIN_USERNAME, LIMIT_IP, PUBLIC_LINK, SUPERNODE, TOTAL_GB, TRIAL_TIME from database import get_servers_from_db, get_trial, store_key, use_trial from handlers.texts import INSTRUCTIONS @@ -56,16 +56,18 @@ async def create_trial_key(tg_id: int, session: Any): username=ADMIN_USERNAME, password=ADMIN_PASSWORD, ), - client_id, - email, - tg_id, - limit_ip=LIMIT_IP, - total_gb=TOTAL_GB, - expiry_time=expiry_timestamp, - enable=True, - flow="xtls-rprx-vision", - inbound_id=int(server_info["inbound_id"]), - sub_id=base_email, + ClientConfig( + client_id=client_id, + email=email, + tg_id=tg_id, + limit_ip=LIMIT_IP, + total_gb=TOTAL_GB, + expiry_time=expiry_timestamp, + enable=True, + flow="xtls-rprx-vision", + inbound_id=int(server_info["inbound_id"]), + sub_id=base_email, + ), ) )