From 1fefe3f59b130b9947a4ff1a89de444e906b23a8 Mon Sep 17 00:00:00 2001 From: linstohu Date: Mon, 25 Dec 2023 20:57:02 +0800 Subject: [PATCH] refactor: binance api response --- binance/usdmfutures/account/client.go | 508 +++++++++++------- binance/usdmfutures/account/types/account.go | 7 + binance/usdmfutures/account/types/balance.go | 7 + binance/usdmfutures/account/types/leverage.go | 6 + binance/usdmfutures/account/types/margin.go | 6 + .../usdmfutures/account/types/multi_asset.go | 6 + binance/usdmfutures/account/types/order.go | 11 + binance/usdmfutures/account/types/position.go | 6 + .../account/types/position_mode.go | 6 + binance/usdmfutures/account/types/response.go | 7 + binance/usdmfutures/account/types/trade.go | 6 + binance/usdmfutures/marketdata/client.go | 388 +++++++------ .../marketdata/types/agg_trades.go | 7 + .../marketdata/types/book_ticker.go | 12 + .../marketdata/types/exchange_info.go | 6 + .../marketdata/types/funding_rate.go | 7 + .../marketdata/types/mark_price.go | 12 + .../marketdata/types/open_interest_hist.go | 7 + .../usdmfutures/marketdata/types/orderbook.go | 9 + .../marketdata/types/ticker_price.go | 12 + .../usdmfutures/marketdata/types/trades.go | 7 + binance/usdmfutures/utils/client.go | 6 +- binance/usdmfutures/utils/request.go | 31 -- 23 files changed, 704 insertions(+), 376 deletions(-) delete mode 100644 binance/usdmfutures/utils/request.go diff --git a/binance/usdmfutures/account/client.go b/binance/usdmfutures/account/client.go index e802fda..f2b34e6 100644 --- a/binance/usdmfutures/account/client.go +++ b/binance/usdmfutures/account/client.go @@ -22,7 +22,6 @@ import ( "crypto/hmac" "crypto/sha256" "encoding/hex" - "encoding/json" "log/slog" "net/http" "time" @@ -31,6 +30,7 @@ import ( "github.com/linstohu/nexapi/binance/usdmfutures/account/types" umutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type UsdMFuturesAccountClient struct { @@ -77,15 +77,18 @@ func NewUsdMFuturesAccountClient(cfg *umutils.USDMarginedClientCfg) (*UsdMFuture }, nil } -func (u *UsdMFuturesAccountClient) ChangePositionMode(ctx context.Context, param types.ChangePositionModeParam) (*types.Response, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/positionSide/dual", - Method: http.MethodPost, +func (u *UsdMFuturesAccountClient) ChangePositionMode(ctx context.Context, param types.ChangePositionModeParam) (*types.DefaultResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/positionSide/dual", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -106,7 +109,7 @@ func (u *UsdMFuturesAccountClient) ChangePositionMode(ctx context.Context, param return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -127,23 +130,31 @@ func (u *UsdMFuturesAccountClient) ChangePositionMode(ctx context.Context, param return nil, err } - var ret types.Response - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.Response + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.DefaultResp{ + Http: resp, + Body: &body, + } + + return data, nil } func (u *UsdMFuturesAccountClient) GetPositionMode(ctx context.Context) (*types.GetCurrentPositionModeResp, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/positionSide/dual", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/positionSide/dual", + Method: http.MethodGet, } + + securityType := umutils.TRADE + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -161,7 +172,7 @@ func (u *UsdMFuturesAccountClient) GetPositionMode(ctx context.Context) (*types. return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -182,23 +193,31 @@ func (u *UsdMFuturesAccountClient) GetPositionMode(ctx context.Context) (*types. return nil, err } - var ret types.GetCurrentPositionModeResp - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.GetCurrentPositionModeAPIResp + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.GetCurrentPositionModeResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *UsdMFuturesAccountClient) ChangeMultiAssetsMode(ctx context.Context, param types.ChangeMultiAssetsModeParam) (*types.Response, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/multiAssetsMargin", - Method: http.MethodPost, +func (u *UsdMFuturesAccountClient) ChangeMultiAssetsMode(ctx context.Context, param types.ChangeMultiAssetsModeParam) (*types.DefaultResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/multiAssetsMargin", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -219,7 +238,7 @@ func (u *UsdMFuturesAccountClient) ChangeMultiAssetsMode(ctx context.Context, pa return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -240,23 +259,31 @@ func (u *UsdMFuturesAccountClient) ChangeMultiAssetsMode(ctx context.Context, pa return nil, err } - var ret types.Response - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.Response + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.DefaultResp{ + Http: resp, + Body: &body, + } + + return data, nil } func (u *UsdMFuturesAccountClient) GetMultiAssetsMode(ctx context.Context) (*types.GetCurrentMultiAssetsModeResp, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/multiAssetsMargin", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/multiAssetsMargin", + Method: http.MethodGet, } + + securityType := umutils.TRADE + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -274,7 +301,7 @@ func (u *UsdMFuturesAccountClient) GetMultiAssetsMode(ctx context.Context) (*typ return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -295,23 +322,31 @@ func (u *UsdMFuturesAccountClient) GetMultiAssetsMode(ctx context.Context) (*typ return nil, err } - var ret types.GetCurrentMultiAssetsModeResp - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.GetCurrentMultiAssetsModeAPIResp + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.GetCurrentMultiAssetsModeResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *UsdMFuturesAccountClient) NewOrder(ctx context.Context, param types.NewOrderParam) (*types.Order, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/order", - Method: http.MethodPost, +func (u *UsdMFuturesAccountClient) NewOrder(ctx context.Context, param types.NewOrderParam) (*types.OrderResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/order", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -332,7 +367,7 @@ func (u *UsdMFuturesAccountClient) NewOrder(ctx context.Context, param types.New return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -353,23 +388,31 @@ func (u *UsdMFuturesAccountClient) NewOrder(ctx context.Context, param types.New return nil, err } - var ret types.Order - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.Order + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.OrderResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *UsdMFuturesAccountClient) QueryOrder(ctx context.Context, param types.GetOrderParam) (*types.Order, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/order", - Method: http.MethodGet, +func (u *UsdMFuturesAccountClient) QueryOrder(ctx context.Context, param types.GetOrderParam) (*types.OrderResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/order", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -390,7 +433,7 @@ func (u *UsdMFuturesAccountClient) QueryOrder(ctx context.Context, param types.G return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -411,23 +454,31 @@ func (u *UsdMFuturesAccountClient) QueryOrder(ctx context.Context, param types.G return nil, err } - var ret types.Order - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.Order + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.OrderResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *UsdMFuturesAccountClient) QueryOpenOrder(ctx context.Context, param types.GetOrderParam) (*types.Order, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/openOrder", - Method: http.MethodGet, +func (u *UsdMFuturesAccountClient) QueryOpenOrder(ctx context.Context, param types.GetOrderParam) (*types.OrderResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/openOrder", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -448,7 +499,7 @@ func (u *UsdMFuturesAccountClient) QueryOpenOrder(ctx context.Context, param typ return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -469,23 +520,31 @@ func (u *UsdMFuturesAccountClient) QueryOpenOrder(ctx context.Context, param typ return nil, err } - var ret types.Order - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.Order + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.OrderResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *UsdMFuturesAccountClient) QueryAllOpenOrders(ctx context.Context, param types.GetAllOpenOrdersParam) ([]*types.Order, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/openOrders", - Method: http.MethodGet, +func (u *UsdMFuturesAccountClient) QueryAllOpenOrders(ctx context.Context, param types.GetAllOpenOrdersParam) (*types.OrdersResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/openOrders", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -506,7 +565,7 @@ func (u *UsdMFuturesAccountClient) QueryAllOpenOrders(ctx context.Context, param return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -527,23 +586,31 @@ func (u *UsdMFuturesAccountClient) QueryAllOpenOrders(ctx context.Context, param return nil, err } - var ret []*types.Order - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.Order + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.OrdersResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (u *UsdMFuturesAccountClient) CancelOrder(ctx context.Context, param types.GetOrderParam) (*types.Order, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/order", - Method: http.MethodDelete, +func (u *UsdMFuturesAccountClient) CancelOrder(ctx context.Context, param types.GetOrderParam) (*types.OrderResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/order", + Method: http.MethodDelete, } + + securityType := umutils.TRADE + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -564,7 +631,7 @@ func (u *UsdMFuturesAccountClient) CancelOrder(ctx context.Context, param types. return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -585,23 +652,31 @@ func (u *UsdMFuturesAccountClient) CancelOrder(ctx context.Context, param types. return nil, err } - var ret types.Order - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.Order + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.OrderResp{ + Http: resp, + Body: &body, + } + + return data, nil } func (u *UsdMFuturesAccountClient) CancelAllOpenOrders(ctx context.Context, param types.CancelAllOpenOrdersParam) error { - req := umutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/allOpenOrders", - Method: http.MethodDelete, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/allOpenOrders", + Method: http.MethodDelete, } + + securityType := umutils.TRADE + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return err } @@ -622,7 +697,7 @@ func (u *UsdMFuturesAccountClient) CancelAllOpenOrders(ctx context.Context, para return err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return err @@ -646,15 +721,18 @@ func (u *UsdMFuturesAccountClient) CancelAllOpenOrders(ctx context.Context, para return nil } -func (u *UsdMFuturesAccountClient) GetAllOrders(ctx context.Context, param types.GetAllOrdersParam) ([]*types.Order, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/allOrders", - Method: http.MethodGet, +func (u *UsdMFuturesAccountClient) GetAllOrders(ctx context.Context, param types.GetAllOrdersParam) (*types.OrdersResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/allOrders", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -675,7 +753,7 @@ func (u *UsdMFuturesAccountClient) GetAllOrders(ctx context.Context, param types return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -696,23 +774,31 @@ func (u *UsdMFuturesAccountClient) GetAllOrders(ctx context.Context, param types return nil, err } - var ret []*types.Order - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.Order + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.OrdersResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (u *UsdMFuturesAccountClient) GetBalance(ctx context.Context) ([]*types.Balance, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v2/balance", - Method: http.MethodGet, +func (u *UsdMFuturesAccountClient) GetBalance(ctx context.Context) (*types.GetBalanceResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v2/balance", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -730,7 +816,7 @@ func (u *UsdMFuturesAccountClient) GetBalance(ctx context.Context) ([]*types.Bal return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -751,23 +837,31 @@ func (u *UsdMFuturesAccountClient) GetBalance(ctx context.Context) ([]*types.Bal return nil, err } - var ret []*types.Balance - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.Balance + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetBalanceResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (u *UsdMFuturesAccountClient) GetAccountInformation(ctx context.Context) (*types.Account, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v2/account", - Method: http.MethodGet, +func (u *UsdMFuturesAccountClient) GetAccountInformation(ctx context.Context) (*types.GetAccountInfoResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v2/account", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -785,7 +879,7 @@ func (u *UsdMFuturesAccountClient) GetAccountInformation(ctx context.Context) (* return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -806,23 +900,31 @@ func (u *UsdMFuturesAccountClient) GetAccountInformation(ctx context.Context) (* return nil, err } - var ret types.Account - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.Account + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.GetAccountInfoResp{ + Http: resp, + Body: &body, + } + + return data, nil } func (u *UsdMFuturesAccountClient) ChangeInitialLeverage(ctx context.Context, param types.ChangeLeverageParam) (*types.ChangeLeverageResp, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/leverage", - Method: http.MethodPost, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/leverage", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -843,7 +945,7 @@ func (u *UsdMFuturesAccountClient) ChangeInitialLeverage(ctx context.Context, pa return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -864,23 +966,31 @@ func (u *UsdMFuturesAccountClient) ChangeInitialLeverage(ctx context.Context, pa return nil, err } - var ret types.ChangeLeverageResp - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.ChangeLeverageAPIResp + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.ChangeLeverageResp{ + Http: resp, + Body: &body, + } + + return data, nil } func (u *UsdMFuturesAccountClient) ChangeMarginType(ctx context.Context, param types.ChangeMarginTypeParam) error { - req := umutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/marginType", - Method: http.MethodPost, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/marginType", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return err } @@ -901,7 +1011,7 @@ func (u *UsdMFuturesAccountClient) ChangeMarginType(ctx context.Context, param t return err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return err @@ -926,14 +1036,17 @@ func (u *UsdMFuturesAccountClient) ChangeMarginType(ctx context.Context, param t } func (u *UsdMFuturesAccountClient) ModifyIsolatedPositionMargin(ctx context.Context, param types.ModifyIsolatedPositionMarginParam) (*types.ModifyIsolatedPositionMarginResp, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/positionMargin", - Method: http.MethodPost, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/positionMargin", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -954,7 +1067,7 @@ func (u *UsdMFuturesAccountClient) ModifyIsolatedPositionMargin(ctx context.Cont return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -975,23 +1088,31 @@ func (u *UsdMFuturesAccountClient) ModifyIsolatedPositionMargin(ctx context.Cont return nil, err } - var ret types.ModifyIsolatedPositionMarginResp - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.ModifyIsolatedPositionMarginAPIResp + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.ModifyIsolatedPositionMarginResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *UsdMFuturesAccountClient) GetPositionInformation(ctx context.Context, param types.GetPositionParam) ([]*types.Position, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v2/positionRisk", - Method: http.MethodGet, +func (u *UsdMFuturesAccountClient) GetPositionInformation(ctx context.Context, param types.GetPositionParam) (*types.GetPositionResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v2/positionRisk", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -1012,7 +1133,7 @@ func (u *UsdMFuturesAccountClient) GetPositionInformation(ctx context.Context, p return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -1033,23 +1154,31 @@ func (u *UsdMFuturesAccountClient) GetPositionInformation(ctx context.Context, p return nil, err } - var ret []*types.Position - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.Position + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetPositionResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (u *UsdMFuturesAccountClient) GetAccountTradeList(ctx context.Context, param types.GetTradeListParam) ([]*types.Trade, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/userTrades", - Method: http.MethodGet, +func (u *UsdMFuturesAccountClient) GetAccountTradeList(ctx context.Context, param types.GetTradeListParam) (*types.GetTradeListResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/userTrades", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(securityType) if err != nil { return nil, err } @@ -1070,7 +1199,7 @@ func (u *UsdMFuturesAccountClient) GetAccountTradeList(ctx context.Context, para return nil, err } - if need := u.NeedSignature(req.SecurityType); need { + if need := u.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -1091,10 +1220,15 @@ func (u *UsdMFuturesAccountClient) GetAccountTradeList(ctx context.Context, para return nil, err } - var ret []*types.Trade - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.Trade + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetTradeListResp{ + Http: resp, + Body: body, + } + + return data, nil } diff --git a/binance/usdmfutures/account/types/account.go b/binance/usdmfutures/account/types/account.go index 5527459..1edef9b 100644 --- a/binance/usdmfutures/account/types/account.go +++ b/binance/usdmfutures/account/types/account.go @@ -17,6 +17,13 @@ package types +import "github.com/linstohu/nexapi/utils" + +type GetAccountInfoResp struct { + Http *utils.ApiResponse + Body *Account +} + type Account struct { FeeTier int `json:"feeTier"` CanTrade bool `json:"canTrade"` diff --git a/binance/usdmfutures/account/types/balance.go b/binance/usdmfutures/account/types/balance.go index da29eaa..d77037a 100644 --- a/binance/usdmfutures/account/types/balance.go +++ b/binance/usdmfutures/account/types/balance.go @@ -17,6 +17,8 @@ package types +import "github.com/linstohu/nexapi/utils" + type Balance struct { AccountAlias string `json:"accountAlias"` Asset string `json:"asset"` @@ -28,3 +30,8 @@ type Balance struct { MarginAvailable bool `json:"marginAvailable"` UpdateTime int64 `json:"updateTime"` } + +type GetBalanceResp struct { + Http *utils.ApiResponse + Body []*Balance +} diff --git a/binance/usdmfutures/account/types/leverage.go b/binance/usdmfutures/account/types/leverage.go index ce5ca59..c14016e 100644 --- a/binance/usdmfutures/account/types/leverage.go +++ b/binance/usdmfutures/account/types/leverage.go @@ -19,6 +19,7 @@ package types import ( bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type ChangeLeverageParam struct { @@ -32,6 +33,11 @@ type ChangeLeverageParams struct { } type ChangeLeverageResp struct { + Http *utils.ApiResponse + Body *ChangeLeverageAPIResp +} + +type ChangeLeverageAPIResp struct { Leverage int `json:"leverage"` MaxNotionalValue string `json:"maxNotionalValue"` Symbol string `json:"symbol"` diff --git a/binance/usdmfutures/account/types/margin.go b/binance/usdmfutures/account/types/margin.go index 01ce656..f29a4af 100644 --- a/binance/usdmfutures/account/types/margin.go +++ b/binance/usdmfutures/account/types/margin.go @@ -20,6 +20,7 @@ package types import ( umutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type ChangeMarginTypeParam struct { @@ -45,6 +46,11 @@ type ModifyIsolatedPositionMarginParams struct { } type ModifyIsolatedPositionMarginResp struct { + Http *utils.ApiResponse + Body *ModifyIsolatedPositionMarginAPIResp +} + +type ModifyIsolatedPositionMarginAPIResp struct { Amount float64 `json:"amount"` Code int `json:"code"` Msg string `json:"msg"` diff --git a/binance/usdmfutures/account/types/multi_asset.go b/binance/usdmfutures/account/types/multi_asset.go index 94380a4..709975a 100644 --- a/binance/usdmfutures/account/types/multi_asset.go +++ b/binance/usdmfutures/account/types/multi_asset.go @@ -19,6 +19,7 @@ package types import ( bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type ChangeMultiAssetsModeParam struct { @@ -31,5 +32,10 @@ type ChangeMultiAssetsModeParams struct { } type GetCurrentMultiAssetsModeResp struct { + Http *utils.ApiResponse + Body *GetCurrentMultiAssetsModeAPIResp +} + +type GetCurrentMultiAssetsModeAPIResp struct { MultiAssetsMargin bool `json:"multiAssetsMargin"` } diff --git a/binance/usdmfutures/account/types/order.go b/binance/usdmfutures/account/types/order.go index 4bdeb85..6dea1ea 100644 --- a/binance/usdmfutures/account/types/order.go +++ b/binance/usdmfutures/account/types/order.go @@ -20,6 +20,7 @@ package types import ( umutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type Order struct { @@ -69,6 +70,16 @@ type NewOrderParams struct { bnutils.DefaultParam } +type OrderResp struct { + Http *utils.ApiResponse + Body *Order +} + +type OrdersResp struct { + Http *utils.ApiResponse + Body []*Order +} + type GetOrderParam struct { Symbol string `url:"symbol" validate:"required"` OrderID int64 `url:"orderId,omitempty" validate:"omitempty"` diff --git a/binance/usdmfutures/account/types/position.go b/binance/usdmfutures/account/types/position.go index e959c6e..1fa3810 100644 --- a/binance/usdmfutures/account/types/position.go +++ b/binance/usdmfutures/account/types/position.go @@ -19,6 +19,7 @@ package types import ( bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type GetPositionParam struct { @@ -30,6 +31,11 @@ type GetPositionParams struct { bnutils.DefaultParam } +type GetPositionResp struct { + Http *utils.ApiResponse + Body []*Position +} + type Position struct { EntryPrice string `json:"entryPrice"` MarginType string `json:"marginType"` diff --git a/binance/usdmfutures/account/types/position_mode.go b/binance/usdmfutures/account/types/position_mode.go index cc19199..687ea57 100644 --- a/binance/usdmfutures/account/types/position_mode.go +++ b/binance/usdmfutures/account/types/position_mode.go @@ -19,6 +19,7 @@ package types import ( bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type ChangePositionModeParam struct { @@ -31,5 +32,10 @@ type ChangePositionModeParams struct { } type GetCurrentPositionModeResp struct { + Http *utils.ApiResponse + Body *GetCurrentPositionModeAPIResp +} + +type GetCurrentPositionModeAPIResp struct { DualSidePosition bool `json:"dualSidePosition"` } diff --git a/binance/usdmfutures/account/types/response.go b/binance/usdmfutures/account/types/response.go index 9f7fbe0..4f76d4a 100644 --- a/binance/usdmfutures/account/types/response.go +++ b/binance/usdmfutures/account/types/response.go @@ -17,6 +17,13 @@ package types +import "github.com/linstohu/nexapi/utils" + +type DefaultResp struct { + Http *utils.ApiResponse + Body *Response +} + type Response struct { Code int `json:"code"` Message string `json:"msg"` diff --git a/binance/usdmfutures/account/types/trade.go b/binance/usdmfutures/account/types/trade.go index f4ce666..0c64a66 100644 --- a/binance/usdmfutures/account/types/trade.go +++ b/binance/usdmfutures/account/types/trade.go @@ -19,6 +19,7 @@ package types import ( bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type GetTradeListParam struct { @@ -35,6 +36,11 @@ type GetTradeListParams struct { bnutils.DefaultParam } +type GetTradeListResp struct { + Http *utils.ApiResponse + Body []*Trade +} + type Trade struct { Buyer bool `json:"buyer"` Commission string `json:"commission"` diff --git a/binance/usdmfutures/marketdata/client.go b/binance/usdmfutures/marketdata/client.go index 2b93ebe..e4b8987 100644 --- a/binance/usdmfutures/marketdata/client.go +++ b/binance/usdmfutures/marketdata/client.go @@ -19,7 +19,6 @@ package marketdata import ( "context" - "encoding/json" "fmt" "net/http" @@ -27,6 +26,7 @@ import ( spottypes "github.com/linstohu/nexapi/binance/spot/marketdata/types" "github.com/linstohu/nexapi/binance/usdmfutures/marketdata/types" umutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" + "github.com/linstohu/nexapi/utils" "github.com/valyala/fastjson" ) @@ -52,14 +52,14 @@ func NewUSDMFuturesMarketDataClient(cfg *umutils.USDMarginedClientCfg) (*USDMFut } func (u *USDMFuturesMarketDataClient) Ping(ctx context.Context) error { - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/ping", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/ping", + Method: http.MethodGet, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return err } @@ -73,15 +73,15 @@ func (u *USDMFuturesMarketDataClient) Ping(ctx context.Context) error { return nil } -func (u *USDMFuturesMarketDataClient) GetServerTime(ctx context.Context) (*spottypes.ServerTime, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/time", - Method: http.MethodGet, +func (u *USDMFuturesMarketDataClient) GetServerTime(ctx context.Context) (*spottypes.ServerTimeResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/time", + Method: http.MethodGet, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -92,23 +92,28 @@ func (u *USDMFuturesMarketDataClient) GetServerTime(ctx context.Context) (*spott return nil, err } - var ret spottypes.ServerTime - if err := json.Unmarshal(resp, &ret); err != nil { + var body spottypes.ServerTime + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &spottypes.ServerTimeResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetExchangeInfo(ctx context.Context) (*types.ExchangeInfo, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/exchangeInfo", - Method: http.MethodGet, +func (u *USDMFuturesMarketDataClient) GetExchangeInfo(ctx context.Context) (*types.GetExchangeInfoResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/exchangeInfo", + Method: http.MethodGet, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -119,29 +124,35 @@ func (u *USDMFuturesMarketDataClient) GetExchangeInfo(ctx context.Context) (*typ return nil, err } - var ret types.ExchangeInfo - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.ExchangeInfo + + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.GetExchangeInfoResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetOrderbook(ctx context.Context, param types.GetOrderbookParams) (*types.Orderbook, error) { +func (u *USDMFuturesMarketDataClient) GetOrderbook(ctx context.Context, param types.GetOrderbookParams) (*types.GetOrderbookResp, error) { err := u.validate.Struct(param) if err != nil { return nil, err } - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/depth", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/depth", + Method: http.MethodGet, + Query: param, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -152,29 +163,35 @@ func (u *USDMFuturesMarketDataClient) GetOrderbook(ctx context.Context, param ty return nil, err } - var ret types.Orderbook - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.Orderbook + + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.GetOrderbookResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetRecentTradeList(ctx context.Context, param types.GetTradeParams) ([]*types.Trade, error) { +func (u *USDMFuturesMarketDataClient) GetRecentTradeList(ctx context.Context, param types.GetTradeParams) (*types.GetTradeResp, error) { err := u.validate.Struct(param) if err != nil { return nil, err } - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/trades", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/trades", + Method: http.MethodGet, + Query: param, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -185,29 +202,34 @@ func (u *USDMFuturesMarketDataClient) GetRecentTradeList(ctx context.Context, pa return nil, err } - var ret []*types.Trade - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.Trade + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetTradeResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetAggTrades(ctx context.Context, param types.GetAggTradesParam) ([]*types.AggTrade, error) { +func (u *USDMFuturesMarketDataClient) GetAggTrades(ctx context.Context, param types.GetAggTradesParam) (*types.GetAggTradesResp, error) { err := u.validate.Struct(param) if err != nil { return nil, err } - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/aggTrades", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/aggTrades", + Method: http.MethodGet, + Query: param, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -218,29 +240,34 @@ func (u *USDMFuturesMarketDataClient) GetAggTrades(ctx context.Context, param ty return nil, err } - var ret []*types.AggTrade - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.AggTrade + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetAggTradesResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetKlines(ctx context.Context, param types.GetKlineParam) ([]*spottypes.Kline, error) { +func (u *USDMFuturesMarketDataClient) GetKlines(ctx context.Context, param types.GetKlineParam) (*spottypes.GetKlineResp, error) { err := u.validate.Struct(param) if err != nil { return nil, err } - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/klines", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/klines", + Method: http.MethodGet, + Query: param, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -251,8 +278,13 @@ func (u *USDMFuturesMarketDataClient) GetKlines(ctx context.Context, param types return nil, err } + body, err := resp.ReadBody() + if err != nil { + return nil, err + } + var p fastjson.Parser - js, err := p.ParseBytes(resp) + js, err := p.ParseBytes(body) if err != nil { return nil, err } @@ -288,24 +320,29 @@ func (u *USDMFuturesMarketDataClient) GetKlines(ctx context.Context, param types }) } - return ret, nil + data := &spottypes.GetKlineResp{ + Http: resp, + Body: ret, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetMarkPriceForSymbol(ctx context.Context, param types.GetMarkPriceParam) (*types.MarkPrice, error) { +func (u *USDMFuturesMarketDataClient) GetMarkPriceForSymbol(ctx context.Context, param types.GetMarkPriceParam) (*types.GetMarkPriceResp, error) { err := u.validate.Struct(param) if err != nil { return nil, err } - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/premiumIndex", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/premiumIndex", + Method: http.MethodGet, + Query: param, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -316,23 +353,29 @@ func (u *USDMFuturesMarketDataClient) GetMarkPriceForSymbol(ctx context.Context, return nil, err } - var ret *types.MarkPrice - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.MarkPrice + + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetMarkPriceResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetMarkPriceForAllSymbols(ctx context.Context) ([]*types.MarkPrice, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/premiumIndex", - Method: http.MethodGet, +func (u *USDMFuturesMarketDataClient) GetMarkPriceForAllSymbols(ctx context.Context) (*types.GetMarkPriceForAllSymbolsResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/premiumIndex", + Method: http.MethodGet, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -343,29 +386,34 @@ func (u *USDMFuturesMarketDataClient) GetMarkPriceForAllSymbols(ctx context.Cont return nil, err } - var ret []*types.MarkPrice - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.MarkPrice + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetMarkPriceForAllSymbolsResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetFundingRateHistory(ctx context.Context, param types.GetFundingRateParam) ([]*types.FundingRate, error) { +func (u *USDMFuturesMarketDataClient) GetFundingRateHistory(ctx context.Context, param types.GetFundingRateParam) (*types.GetFundingRateResp, error) { err := u.validate.Struct(param) if err != nil { return nil, err } - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/fundingRate", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/fundingRate", + Method: http.MethodGet, + Query: param, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -376,29 +424,34 @@ func (u *USDMFuturesMarketDataClient) GetFundingRateHistory(ctx context.Context, return nil, err } - var ret []*types.FundingRate - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.FundingRate + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetFundingRateResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetTickerPriceForSymbol(ctx context.Context, param types.GetTickerPriceParam) (*types.TickerPrice, error) { +func (u *USDMFuturesMarketDataClient) GetTickerPriceForSymbol(ctx context.Context, param types.GetTickerPriceParam) (*types.GetTickerPriceForSymbolResp, error) { err := u.validate.Struct(param) if err != nil { return nil, err } - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/ticker/price", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/ticker/price", + Method: http.MethodGet, + Query: param, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -409,23 +462,28 @@ func (u *USDMFuturesMarketDataClient) GetTickerPriceForSymbol(ctx context.Contex return nil, err } - var ret *types.TickerPrice - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.TickerPrice + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetTickerPriceForSymbolResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetTickerPriceForAllSymbols(ctx context.Context) ([]*types.TickerPrice, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/ticker/price", - Method: http.MethodGet, +func (u *USDMFuturesMarketDataClient) GetTickerPriceForAllSymbols(ctx context.Context) (*types.GetTickerPriceForSymbolsResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/ticker/price", + Method: http.MethodGet, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -436,29 +494,34 @@ func (u *USDMFuturesMarketDataClient) GetTickerPriceForAllSymbols(ctx context.Co return nil, err } - var ret []*types.TickerPrice - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.TickerPrice + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetTickerPriceForSymbolsResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetBookTickerForSymbol(ctx context.Context, param types.GetBookTickerForSymbolParam) (*types.BookTicker, error) { +func (u *USDMFuturesMarketDataClient) GetBookTickerForSymbol(ctx context.Context, param types.GetBookTickerForSymbolParam) (*types.GetBookTickerForSymbolResp, error) { err := u.validate.Struct(param) if err != nil { return nil, err } - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/ticker/bookTicker", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/ticker/bookTicker", + Method: http.MethodGet, + Query: param, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -469,23 +532,28 @@ func (u *USDMFuturesMarketDataClient) GetBookTickerForSymbol(ctx context.Context return nil, err } - var ret *types.BookTicker - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.BookTicker + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetBookTickerForSymbolResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetBookTickerForAllSymbols(ctx context.Context) ([]*types.BookTicker, error) { - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/fapi/v1/ticker/bookTicker", - Method: http.MethodGet, +func (u *USDMFuturesMarketDataClient) GetBookTickerForAllSymbols(ctx context.Context) (*types.GetBookTickerForSymbolsResp, error) { + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/fapi/v1/ticker/bookTicker", + Method: http.MethodGet, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -496,29 +564,34 @@ func (u *USDMFuturesMarketDataClient) GetBookTickerForAllSymbols(ctx context.Con return nil, err } - var ret []*types.BookTicker - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.BookTicker + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetBookTickerForSymbolsResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (u *USDMFuturesMarketDataClient) GetOpenInterestHistory(ctx context.Context, param types.GetOpenInterestHistParam) ([]*types.OpenInterestHist, error) { +func (u *USDMFuturesMarketDataClient) GetOpenInterestHistory(ctx context.Context, param types.GetOpenInterestHistParam) (*types.GetOpenInterestHistResp, error) { err := u.validate.Struct(param) if err != nil { return nil, err } - req := umutils.HTTPRequest{ - SecurityType: umutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/futures/data/openInterestHist", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: u.GetDebug(), + BaseURL: u.GetBaseURL(), + Path: "/futures/data/openInterestHist", + Method: http.MethodGet, + Query: param, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := u.GenHeaders(umutils.NONE) if err != nil { return nil, err } @@ -529,10 +602,15 @@ func (u *USDMFuturesMarketDataClient) GetOpenInterestHistory(ctx context.Context return nil, err } - var ret []*types.OpenInterestHist - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.OpenInterestHist + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetOpenInterestHistResp{ + Http: resp, + Body: body, + } + + return data, nil } diff --git a/binance/usdmfutures/marketdata/types/agg_trades.go b/binance/usdmfutures/marketdata/types/agg_trades.go index e2d9610..2c64c12 100644 --- a/binance/usdmfutures/marketdata/types/agg_trades.go +++ b/binance/usdmfutures/marketdata/types/agg_trades.go @@ -17,6 +17,8 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetAggTradesParam struct { Symbol string `url:"symbol" validate:"required"` FromID string `url:"fromId,omitempty" validate:"omitempty"` @@ -25,6 +27,11 @@ type GetAggTradesParam struct { Limit int `url:"limit,omitempty" validate:"omitempty,max=1000"` } +type GetAggTradesResp struct { + Http *utils.ApiResponse + Body []*AggTrade +} + type AggTrade struct { A int `json:"a"` // Aggregate tradeId P string `json:"p"` // Price diff --git a/binance/usdmfutures/marketdata/types/book_ticker.go b/binance/usdmfutures/marketdata/types/book_ticker.go index 56dbc30..9a4ceb5 100644 --- a/binance/usdmfutures/marketdata/types/book_ticker.go +++ b/binance/usdmfutures/marketdata/types/book_ticker.go @@ -17,10 +17,22 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetBookTickerForSymbolParam struct { Symbol string `url:"symbol" validate:"required"` } +type GetBookTickerForSymbolResp struct { + Http *utils.ApiResponse + Body *BookTicker +} + +type GetBookTickerForSymbolsResp struct { + Http *utils.ApiResponse + Body []*BookTicker +} + type BookTicker struct { Symbol string `json:"symbol"` BidPrice string `json:"bidPrice"` diff --git a/binance/usdmfutures/marketdata/types/exchange_info.go b/binance/usdmfutures/marketdata/types/exchange_info.go index 0c8c78a..097f9e8 100644 --- a/binance/usdmfutures/marketdata/types/exchange_info.go +++ b/binance/usdmfutures/marketdata/types/exchange_info.go @@ -19,8 +19,14 @@ package types import ( umutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" + "github.com/linstohu/nexapi/utils" ) +type GetExchangeInfoResp struct { + Http *utils.ApiResponse + Body *ExchangeInfo +} + type ExchangeInfo struct { Timezone string `json:"timezone"` ServerTime int64 `json:"serverTime"` diff --git a/binance/usdmfutures/marketdata/types/funding_rate.go b/binance/usdmfutures/marketdata/types/funding_rate.go index 116d285..0bb0481 100644 --- a/binance/usdmfutures/marketdata/types/funding_rate.go +++ b/binance/usdmfutures/marketdata/types/funding_rate.go @@ -17,6 +17,8 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetFundingRateParam struct { Symbol string `url:"symbol,omitempty" validate:"omitempty"` StartTime int64 `url:"startTime,omitempty" validate:"omitempty"` @@ -24,6 +26,11 @@ type GetFundingRateParam struct { Limit int `url:"limit,omitempty" validate:"omitempty,max=1000"` } +type GetFundingRateResp struct { + Http *utils.ApiResponse + Body []*FundingRate +} + type FundingRate struct { Symbol string `json:"symbol"` FundingRate string `json:"fundingRate"` diff --git a/binance/usdmfutures/marketdata/types/mark_price.go b/binance/usdmfutures/marketdata/types/mark_price.go index adaf254..fd27415 100644 --- a/binance/usdmfutures/marketdata/types/mark_price.go +++ b/binance/usdmfutures/marketdata/types/mark_price.go @@ -17,10 +17,22 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetMarkPriceParam struct { Symbol string `url:"symbol" validate:"required"` } +type GetMarkPriceResp struct { + Http *utils.ApiResponse + Body *MarkPrice +} + +type GetMarkPriceForAllSymbolsResp struct { + Http *utils.ApiResponse + Body []*MarkPrice +} + type MarkPrice struct { Symbol string `json:"symbol"` MarkPrice string `json:"markPrice"` diff --git a/binance/usdmfutures/marketdata/types/open_interest_hist.go b/binance/usdmfutures/marketdata/types/open_interest_hist.go index 9ba5b04..de2a524 100644 --- a/binance/usdmfutures/marketdata/types/open_interest_hist.go +++ b/binance/usdmfutures/marketdata/types/open_interest_hist.go @@ -17,6 +17,8 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetOpenInterestHistParam struct { Symbol string `url:"symbol" validate:"required"` Period string `url:"period" validate:"required,oneof=5m 15m 30m 1h 2h 4h 6h 12h 1d"` @@ -25,6 +27,11 @@ type GetOpenInterestHistParam struct { Limit int `url:"limit,omitempty" validate:"omitempty,max=500"` } +type GetOpenInterestHistResp struct { + Http *utils.ApiResponse + Body []*OpenInterestHist +} + type OpenInterestHist struct { Symbol string `json:"symbol"` SumOpenInterest string `json:"sumOpenInterest"` diff --git a/binance/usdmfutures/marketdata/types/orderbook.go b/binance/usdmfutures/marketdata/types/orderbook.go index 0f1ed95..4a488e2 100644 --- a/binance/usdmfutures/marketdata/types/orderbook.go +++ b/binance/usdmfutures/marketdata/types/orderbook.go @@ -17,11 +17,20 @@ package types +import ( + "github.com/linstohu/nexapi/utils" +) + type GetOrderbookParams struct { Symbol string `url:"symbol" validate:"required"` Limit int `url:"limit,omitempty" validate:"omitempty,oneof=5 10 20 50 100 500 1000"` } +type GetOrderbookResp struct { + Http *utils.ApiResponse + Body *Orderbook +} + type Orderbook struct { LastUpdateID int64 `json:"lastUpdateId"` MessageOutputTime int64 `json:"E"` diff --git a/binance/usdmfutures/marketdata/types/ticker_price.go b/binance/usdmfutures/marketdata/types/ticker_price.go index 7d8c44b..2309a9c 100644 --- a/binance/usdmfutures/marketdata/types/ticker_price.go +++ b/binance/usdmfutures/marketdata/types/ticker_price.go @@ -17,10 +17,22 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetTickerPriceParam struct { Symbol string `url:"symbol" validate:"required"` } +type GetTickerPriceForSymbolResp struct { + Http *utils.ApiResponse + Body *TickerPrice +} + +type GetTickerPriceForSymbolsResp struct { + Http *utils.ApiResponse + Body []*TickerPrice +} + type TickerPrice struct { Symbol string `json:"symbol"` Price string `json:"price"` diff --git a/binance/usdmfutures/marketdata/types/trades.go b/binance/usdmfutures/marketdata/types/trades.go index fe9366e..e99c6ef 100644 --- a/binance/usdmfutures/marketdata/types/trades.go +++ b/binance/usdmfutures/marketdata/types/trades.go @@ -17,11 +17,18 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetTradeParams struct { Symbol string `url:"symbol" validate:"required"` Limit int `url:"limit,omitempty" validate:"omitempty,max=1000"` } +type GetTradeResp struct { + Http *utils.ApiResponse + Body []*Trade +} + type Trade struct { ID int64 `json:"id"` Price string `json:"price"` diff --git a/binance/usdmfutures/utils/client.go b/binance/usdmfutures/utils/client.go index 46cce87..c868df0 100644 --- a/binance/usdmfutures/utils/client.go +++ b/binance/usdmfutures/utils/client.go @@ -30,6 +30,7 @@ import ( "github.com/go-playground/validator" "github.com/google/go-querystring/query" + "github.com/linstohu/nexapi/utils" ) type USDMarginedClient struct { @@ -131,7 +132,7 @@ func (u *USDMarginedClient) NeedSignature(t SecurityType) bool { } } -func (u *USDMarginedClient) SendHTTPRequest(ctx context.Context, req HTTPRequest) ([]byte, error) { +func (u *USDMarginedClient) SendHTTPRequest(ctx context.Context, req utils.HTTPRequest) (*utils.ApiResponse, error) { client := http.Client{} var body io.Reader @@ -177,7 +178,6 @@ func (u *USDMarginedClient) SendHTTPRequest(ctx context.Context, req HTTPRequest if err != nil { return nil, err } - defer resp.Body.Close() if u.GetDebug() { dump, err := httputil.DumpResponse(resp, true) @@ -194,5 +194,5 @@ func (u *USDMarginedClient) SendHTTPRequest(ctx context.Context, req HTTPRequest return nil, fmt.Errorf("API returned a non-200 status code: [%d] - [%s]", resp.StatusCode, buf.String()) } - return buf.Bytes(), nil + return utils.NewApiResponse(&req, resp), nil } diff --git a/binance/usdmfutures/utils/request.go b/binance/usdmfutures/utils/request.go deleted file mode 100644 index 0aba47b..0000000 --- a/binance/usdmfutures/utils/request.go +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2023, LinstoHu - * All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package umutils - -type HTTPRequest struct { - // SecurityType each endpoint has a security type that determines how you will interact with it - // docs: https://binance-docs.github.io/apidocs/futures/en/#endpoint-security-type - SecurityType SecurityType - - BaseURL string - Path string - Method string - Headers map[string]string - Query any - Body any -}