From 1bec71a43f1df826d55531c8279cb67bc171eadc Mon Sep 17 00:00:00 2001 From: zjkung1123 Date: Wed, 10 Jul 2024 15:30:19 +0800 Subject: [PATCH] refactor: update response memo to user_def --- fugle_trade/order.py | 10 ++++----- fugle_trade/sdk.py | 52 ++++++++++++++++++++++++++++++++++++++----- tests/test_request.py | 6 +++++ 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/fugle_trade/order.py b/fugle_trade/order.py index faf6760..23ed397 100644 --- a/fugle_trade/order.py +++ b/fugle_trade/order.py @@ -15,7 +15,7 @@ class OrderObject: price: float stock_no: str quantity: int - memo: str + user_def: str def __init__( self, @@ -27,7 +27,7 @@ def __init__( bs_flag: BSFlag = BSFlag.ROD, price_flag: PriceFlag = PriceFlag.Limit, trade: Trade = Trade.Cash, - memo: str = "", + user_def: str = "", ): if type(buy_sell) is not Action: @@ -69,8 +69,8 @@ def __init__( if type(trade) is not Trade: raise TypeError("Please use fugleTrade.constant Trade") - if len(memo) > 50: - memo = memo[:50] + if len(user_def) > 50: + user_def = user_def[:50] self.ap_code = ap_code self.price_flag = price_flag @@ -80,7 +80,7 @@ def __init__( self.price = price self.stock_no = stock_no self.quantity = quantity - self.memo = memo + self.user_def = user_def def __str__(self): return ( diff --git a/fugle_trade/sdk.py b/fugle_trade/sdk.py index 6a36b5e..77dac59 100644 --- a/fugle_trade/sdk.py +++ b/fugle_trade/sdk.py @@ -3,6 +3,7 @@ """ +from typing import Any, Dict, List, Union from fugle_trade_core.fugle_trade_core import CoreSDK from fugle_trade.constant import APCode, PriceFlag from fugle_trade.order import OrderObject @@ -76,7 +77,7 @@ def reset_password(self): def place_order(self, order_object: OrderObject): """place order""" - return loads(self.__core.order(order_object, order_object.memo))["data"] + return loads(self.__core.order(order_object, order_object.user_def))["data"] def delete_order(self, order_result): """delete_order""" @@ -138,27 +139,36 @@ def modify_price( def get_order_results(self): """get order result data 取得當日委託明細""" order_res = self.__core.get_order_results() - return loads(order_res)["data"]["order_results"] + order_results = loads(order_res)["data"]["order_results"] + return self.replace_key_in_dict(order_results, "memo", "user_def") def get_order_results_by_date(self, start, end): """get order result data by date 用日期當作篩選條件委託明細""" order_res_history = self.__core.get_order_result_history(start, end, "0") - return loads(order_res_history)["data"]["order_result_history"] + return self.replace_key_in_dict( + loads(order_res_history)["data"]["order_result_history"], "memo", "user_def" + ) def get_transactions(self, query_range): """get transactions data 成交明細""" transactions_res = self.__core.get_transactions(query_range) - return loads(transactions_res)["data"]["mat_sums"] + return self.replace_key_in_dict( + loads(transactions_res)["data"]["mat_sums"], "memo", "user_def" + ) def get_transactions_by_date(self, start, end): """用日期當作篩選條件 get transactions data by date 成交明細""" transactions_res = self.__core.get_transactions_by_date(start, end) - return loads(transactions_res)["data"]["mat_sums"] + return self.replace_key_in_dict( + loads(transactions_res)["data"]["mat_sums"], "memo", "user_def" + ) def get_inventories(self): """get inventories data 庫存資訊""" inventories_res = self.__core.get_inventories() - return loads(inventories_res)["data"]["stk_sums"] + return self.replace_key_in_dict( + loads(inventories_res)["data"]["stk_sums"], "memo", "user_def" + ) def get_balance(self): """get balance data 餘額資訊""" @@ -196,6 +206,7 @@ def get_machine_time(self): return loads(key_info_res)["data"] def on(self, param): + def inner(func): """ do operations with func, register func into __wsHandler @@ -222,3 +233,32 @@ def recover_order_result(self, order_result): else: new_dict[key] = int(value) return {**order_result, **new_dict} + + def replace_key_in_dict( + self, data: Union[Dict[str, Any], List[Any]], old_key: str, new_key: str + ) -> Union[Dict[str, Any], List[Any]]: + """ + Recursively replace keys in a dictionary or list of dictionaries according to a key mapping. + + Parameters: + data (Union[Dict[str, Any], List[Any]]): The dictionary or list to update. + old_key (str): The key to replace. + new_key (str): The new key name. + + Returns: + Union[Dict[str, Any], List[Any]]: The updated dictionary or list with keys replaced. + """ + if isinstance(data, list): + return [self.replace_key_in_dict(item, old_key, new_key) for item in data] + elif isinstance(data, dict): + new_dict = {} + for k, v in data.items(): + new_key_name = new_key if k == old_key else k + new_dict[new_key_name] = ( + self.replace_key_in_dict(v, old_key, new_key) + if isinstance(v, (dict, list)) + else v + ) + return new_dict + else: + return data diff --git a/tests/test_request.py b/tests/test_request.py index ed4c6ec..394729f 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -55,6 +55,7 @@ def test_get_order_results(sdk): assert result[0]["err_msg"] == "" assert result[0]["avg_price"] == 0 assert result[0]["bs_flag"] == "R" + assert result[0]["user_def"] == "" # Assertions for the second order result assert result[1]["work_date"] == "20220222" @@ -80,6 +81,7 @@ def test_get_order_results(sdk): assert result[1]["err_msg"] == "" assert result[1]["avg_price"] == 0 assert result[1]["bs_flag"] == "R" + assert result[1]["user_def"] == "" def test_get_order_result_by_date(sdk): @@ -105,6 +107,7 @@ def test_get_order_result_by_date(sdk): assert result[0]["celable"] == 1 assert result[0]["avg_price"] == 0 assert result[0]["bs_flag"] == "R" + assert result[0]["user_def"] == "" # Assertions for the second order result assert result[1]["ack_date"] == "20220314" @@ -127,6 +130,7 @@ def test_get_order_result_by_date(sdk): assert result[1]["celable"] == "2" assert result[1]["avg_price"] == 0 assert result[1]["bs_flag"] == "R" + assert result[1]["user_def"] == "" def test_get_inventories(sdk): @@ -134,6 +138,7 @@ def test_get_inventories(sdk): assert result[0]["stk_no"] == "0056" assert result[0]["stk_na"] == "元大高股息" assert result[0]["price_mkt"] == "33.49" + assert result[0]["stk_dats"][0]["user_def"] == "" def test_get_settlements(sdk): @@ -148,6 +153,7 @@ def test_get_transactions(sdk): assert result[0]["stk_no"] == "2884" assert result[0]["buy_sell"] == "B" assert result[0]["qty"] == "1000" + assert result[0]["mat_dats"][0]["user_def"] == "" def test_cancel_order_intraday_odd(sdk):