From 57c87c4ab9a4652f5a0f5e1fd052bd759ef7bb91 Mon Sep 17 00:00:00 2001 From: rex <1073853456@qq.com> Date: Fri, 9 Aug 2024 22:40:02 +0800 Subject: [PATCH] use class to handle apis --- src/one_api_cli/channel.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/one_api_cli/channel.py b/src/one_api_cli/channel.py index a15950f..9e2d699 100644 --- a/src/one_api_cli/channel.py +++ b/src/one_api_cli/channel.py @@ -2,6 +2,32 @@ from .constant import base_url, headers from loguru import logger +class Channel(): + id: int + """The ID of the channel.""" + + type: int + """The type of the channel. Default to OpenAI, i.e. channel type 1.""" + + key: str + """The api key of the channel.""" + + name: str + """The display name of the channel.""" + + base_url: str + """The base URL of the channel.""" + + models: str + """The models of the channel, separated by commas.""" + + def __init__(self, **kwargs): + self.__dict__.update(kwargs) + + def update(self, **kwargs): + self.__dict__.update(kwargs) + + channel_url = f"{base_url}/api/channel" def get_channels(): @@ -18,7 +44,7 @@ def get_channels(): if not msg['success']: logger.error(msg['message']) return {} - return msg['data'] + return Channel(**msg['data']) except requests.RequestException as e: logger.error(f"Error fetching channels: {e}") return [] @@ -38,7 +64,7 @@ def get_channel(channel_id:int)->dict: if not msg['success']: logger.error(msg['message']) return {} - return msg['data'] + return Channel(**msg['data']) except requests.RequestException as e: logger.error(f"Error fetching channel: {e}") return {}