diff --git a/binance/coinmfutures/account/client.go b/binance/coinmfutures/account/client.go index 136b716..cf7966c 100644 --- a/binance/coinmfutures/account/client.go +++ b/binance/coinmfutures/account/client.go @@ -22,7 +22,6 @@ import ( "crypto/hmac" "crypto/sha256" "encoding/hex" - "encoding/json" "log/slog" "net/http" "time" @@ -32,6 +31,7 @@ import ( cmutils "github.com/linstohu/nexapi/binance/coinmfutures/utils" umutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type CoinMFuturesAccountClient struct { @@ -78,15 +78,18 @@ func NewCoinMFuturesAccountClient(cfg *cmutils.CoinMarginedClientCfg) (*CoinMFut }, nil } -func (c *CoinMFuturesAccountClient) ChangePositionMode(ctx context.Context, param types.ChangePositionModeParam) (*types.Response, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/positionSide/dual", - Method: http.MethodPost, +func (c *CoinMFuturesAccountClient) ChangePositionMode(ctx context.Context, param types.ChangePositionModeParam) (*types.DefaultResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/positionSide/dual", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -107,7 +110,7 @@ func (c *CoinMFuturesAccountClient) ChangePositionMode(ctx context.Context, para return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -128,23 +131,31 @@ func (c *CoinMFuturesAccountClient) ChangePositionMode(ctx context.Context, para 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 (c *CoinMFuturesAccountClient) GetPositionMode(ctx context.Context) (*types.GetCurrentPositionModeResp, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/positionSide/dual", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/positionSide/dual", + Method: http.MethodGet, } + + securityType := umutils.TRADE + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -162,7 +173,7 @@ func (c *CoinMFuturesAccountClient) GetPositionMode(ctx context.Context) (*types return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -183,23 +194,31 @@ func (c *CoinMFuturesAccountClient) 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 (c *CoinMFuturesAccountClient) NewOrder(ctx context.Context, param types.NewOrderParam) (*types.Order, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/order", - Method: http.MethodPost, +func (c *CoinMFuturesAccountClient) NewOrder(ctx context.Context, param types.NewOrderParam) (*types.OrderResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/order", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -220,7 +239,7 @@ func (c *CoinMFuturesAccountClient) NewOrder(ctx context.Context, param types.Ne return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -241,23 +260,31 @@ func (c *CoinMFuturesAccountClient) NewOrder(ctx context.Context, param types.Ne 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 (c *CoinMFuturesAccountClient) QueryOrder(ctx context.Context, param types.GetOrderParam) (*types.Order, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/order", - Method: http.MethodGet, +func (c *CoinMFuturesAccountClient) QueryOrder(ctx context.Context, param types.GetOrderParam) (*types.OrderResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/order", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -278,7 +305,7 @@ func (c *CoinMFuturesAccountClient) QueryOrder(ctx context.Context, param types. return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -299,23 +326,31 @@ func (c *CoinMFuturesAccountClient) QueryOrder(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 (c *CoinMFuturesAccountClient) QueryOpenOrder(ctx context.Context, param types.GetOrderParam) (*types.Order, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/openOrder", - Method: http.MethodGet, +func (c *CoinMFuturesAccountClient) QueryOpenOrder(ctx context.Context, param types.GetOrderParam) (*types.OrderResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/openOrder", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -336,7 +371,7 @@ func (c *CoinMFuturesAccountClient) QueryOpenOrder(ctx context.Context, param ty return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -357,23 +392,31 @@ func (c *CoinMFuturesAccountClient) QueryOpenOrder(ctx context.Context, param ty 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 (c *CoinMFuturesAccountClient) QueryAllOpenOrders(ctx context.Context, param types.GetAllOpenOrdersParam) ([]*types.Order, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/openOrders", - Method: http.MethodGet, +func (c *CoinMFuturesAccountClient) QueryAllOpenOrders(ctx context.Context, param types.GetAllOpenOrdersParam) (*types.OrdersResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/openOrders", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -394,7 +437,7 @@ func (c *CoinMFuturesAccountClient) QueryAllOpenOrders(ctx context.Context, para return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -415,23 +458,31 @@ func (c *CoinMFuturesAccountClient) QueryAllOpenOrders(ctx context.Context, para 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 (c *CoinMFuturesAccountClient) CancelOrder(ctx context.Context, param types.GetOrderParam) (*types.Order, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/order", - Method: http.MethodDelete, +func (c *CoinMFuturesAccountClient) CancelOrder(ctx context.Context, param types.GetOrderParam) (*types.OrderResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/order", + Method: http.MethodDelete, } + + securityType := umutils.TRADE + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -452,7 +503,7 @@ func (c *CoinMFuturesAccountClient) CancelOrder(ctx context.Context, param types return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -473,23 +524,31 @@ func (c *CoinMFuturesAccountClient) 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 (c *CoinMFuturesAccountClient) CancelAllOpenOrders(ctx context.Context, param types.CancelAllOpenOrdersParam) error { - req := cmutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/allOpenOrders", - Method: http.MethodDelete, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/allOpenOrders", + Method: http.MethodDelete, } + + securityType := umutils.TRADE + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return err } @@ -510,7 +569,7 @@ func (c *CoinMFuturesAccountClient) CancelAllOpenOrders(ctx context.Context, par return err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return err @@ -534,15 +593,18 @@ func (c *CoinMFuturesAccountClient) CancelAllOpenOrders(ctx context.Context, par return nil } -func (c *CoinMFuturesAccountClient) GetAllOrders(ctx context.Context, param types.GetAllOrdersParam) ([]*types.Order, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/allOrders", - Method: http.MethodGet, +func (c *CoinMFuturesAccountClient) GetAllOrders(ctx context.Context, param types.GetAllOrdersParam) (*types.OrdersResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/allOrders", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -563,7 +625,7 @@ func (c *CoinMFuturesAccountClient) GetAllOrders(ctx context.Context, param type return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -584,23 +646,31 @@ func (c *CoinMFuturesAccountClient) GetAllOrders(ctx context.Context, param type 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 (c *CoinMFuturesAccountClient) GetBalance(ctx context.Context) ([]*types.Balance, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/balance", - Method: http.MethodGet, +func (c *CoinMFuturesAccountClient) GetBalance(ctx context.Context) (*types.GetBalanceResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/balance", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -618,7 +688,7 @@ func (c *CoinMFuturesAccountClient) GetBalance(ctx context.Context) ([]*types.Ba return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -639,23 +709,31 @@ func (c *CoinMFuturesAccountClient) GetBalance(ctx context.Context) ([]*types.Ba 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 (c *CoinMFuturesAccountClient) GetAccountInformation(ctx context.Context) (*types.Account, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/account", - Method: http.MethodGet, +func (c *CoinMFuturesAccountClient) GetAccountInformation(ctx context.Context) (*types.GetAccountInfoResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/account", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -673,7 +751,7 @@ func (c *CoinMFuturesAccountClient) GetAccountInformation(ctx context.Context) ( return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -694,23 +772,31 @@ func (c *CoinMFuturesAccountClient) 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 (c *CoinMFuturesAccountClient) ChangeInitialLeverage(ctx context.Context, param types.ChangeLeverageParam) (*types.ChangeLeverageResp, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/leverage", - Method: http.MethodPost, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/leverage", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -731,7 +817,7 @@ func (c *CoinMFuturesAccountClient) ChangeInitialLeverage(ctx context.Context, p return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -752,23 +838,31 @@ func (c *CoinMFuturesAccountClient) ChangeInitialLeverage(ctx context.Context, p 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 (c *CoinMFuturesAccountClient) ChangeMarginType(ctx context.Context, param types.ChangeMarginTypeParam) error { - req := cmutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/marginType", - Method: http.MethodPost, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/marginType", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return err } @@ -789,7 +883,7 @@ func (c *CoinMFuturesAccountClient) ChangeMarginType(ctx context.Context, param return err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return err @@ -814,14 +908,17 @@ func (c *CoinMFuturesAccountClient) ChangeMarginType(ctx context.Context, param } func (c *CoinMFuturesAccountClient) ModifyIsolatedPositionMargin(ctx context.Context, param types.ModifyIsolatedPositionMarginParam) (*types.ModifyIsolatedPositionMarginResp, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.TRADE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/positionMargin", - Method: http.MethodPost, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/positionMargin", + Method: http.MethodPost, } + + securityType := umutils.TRADE + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -842,7 +939,7 @@ func (c *CoinMFuturesAccountClient) ModifyIsolatedPositionMargin(ctx context.Con return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(nil, body) if err != nil { return nil, err @@ -863,23 +960,31 @@ func (c *CoinMFuturesAccountClient) ModifyIsolatedPositionMargin(ctx context.Con 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 (c *CoinMFuturesAccountClient) GetPositionInformation(ctx context.Context, param types.GetPositionParam) ([]*types.Position, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/positionRisk", - Method: http.MethodGet, +func (c *CoinMFuturesAccountClient) GetPositionInformation(ctx context.Context, param types.GetPositionParam) (*types.GetPositionResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/positionRisk", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -900,7 +1005,7 @@ func (c *CoinMFuturesAccountClient) GetPositionInformation(ctx context.Context, return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -921,23 +1026,31 @@ func (c *CoinMFuturesAccountClient) GetPositionInformation(ctx context.Context, 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 (c *CoinMFuturesAccountClient) GetAccountTradeList(ctx context.Context, param types.GetTradeListParam) ([]*types.Trade, error) { - req := cmutils.HTTPRequest{ - SecurityType: umutils.USER_DATA, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/userTrades", - Method: http.MethodGet, +func (c *CoinMFuturesAccountClient) GetAccountTradeList(ctx context.Context, param types.GetTradeListParam) (*types.GetTradeListResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/userTrades", + Method: http.MethodGet, } + + securityType := umutils.USER_DATA + { - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(securityType) if err != nil { return nil, err } @@ -958,7 +1071,7 @@ func (c *CoinMFuturesAccountClient) GetAccountTradeList(ctx context.Context, par return nil, err } - if need := c.NeedSignature(req.SecurityType); need { + if need := c.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -979,10 +1092,15 @@ func (c *CoinMFuturesAccountClient) GetAccountTradeList(ctx context.Context, par 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/coinmfutures/account/types/account.go b/binance/coinmfutures/account/types/account.go index 6d23006..ef0cfcc 100644 --- a/binance/coinmfutures/account/types/account.go +++ b/binance/coinmfutures/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 { Assets []struct { Asset string `json:"asset"` diff --git a/binance/coinmfutures/account/types/balance.go b/binance/coinmfutures/account/types/balance.go index f25704c..abaaa2e 100644 --- a/binance/coinmfutures/account/types/balance.go +++ b/binance/coinmfutures/account/types/balance.go @@ -17,6 +17,13 @@ package types +import "github.com/linstohu/nexapi/utils" + +type GetBalanceResp struct { + Http *utils.ApiResponse + Body []*Balance +} + type Balance struct { AccountAlias string `json:"accountAlias"` Asset string `json:"asset"` diff --git a/binance/coinmfutures/account/types/leverage.go b/binance/coinmfutures/account/types/leverage.go index 767e5d4..a76f22e 100644 --- a/binance/coinmfutures/account/types/leverage.go +++ b/binance/coinmfutures/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"` MaxQty string `json:"maxQty"` Symbol string `json:"symbol"` diff --git a/binance/coinmfutures/account/types/margin.go b/binance/coinmfutures/account/types/margin.go index 01ce656..f29a4af 100644 --- a/binance/coinmfutures/account/types/margin.go +++ b/binance/coinmfutures/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/coinmfutures/account/types/order.go b/binance/coinmfutures/account/types/order.go index 3e29edd..2732129 100644 --- a/binance/coinmfutures/account/types/order.go +++ b/binance/coinmfutures/account/types/order.go @@ -20,8 +20,19 @@ package types import ( umutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) +type OrderResp struct { + Http *utils.ApiResponse + Body *Order +} + +type OrdersResp struct { + Http *utils.ApiResponse + Body []*Order +} + type Order struct { ClientOrderID string `json:"clientOrderId"` CumQty string `json:"cumQty"` diff --git a/binance/coinmfutures/account/types/position.go b/binance/coinmfutures/account/types/position.go index b0e1227..bc111b8 100644 --- a/binance/coinmfutures/account/types/position.go +++ b/binance/coinmfutures/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 { @@ -31,6 +32,11 @@ type GetPositionParams struct { bnutils.DefaultParam } +type GetPositionResp struct { + Http *utils.ApiResponse + Body []*Position +} + type Position struct { Symbol string `json:"symbol"` PositionAmt string `json:"positionAmt"` diff --git a/binance/coinmfutures/account/types/position_mode.go b/binance/coinmfutures/account/types/position_mode.go index cc19199..687ea57 100644 --- a/binance/coinmfutures/account/types/position_mode.go +++ b/binance/coinmfutures/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/coinmfutures/account/types/response.go b/binance/coinmfutures/account/types/response.go index 9f7fbe0..4f76d4a 100644 --- a/binance/coinmfutures/account/types/response.go +++ b/binance/coinmfutures/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/coinmfutures/account/types/trade.go b/binance/coinmfutures/account/types/trade.go index 8e12131..bb27d45 100644 --- a/binance/coinmfutures/account/types/trade.go +++ b/binance/coinmfutures/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 { Symbol string `json:"symbol"` ID int64 `json:"id"` diff --git a/binance/coinmfutures/marketdata/client.go b/binance/coinmfutures/marketdata/client.go index 3ad5903..2e808f9 100644 --- a/binance/coinmfutures/marketdata/client.go +++ b/binance/coinmfutures/marketdata/client.go @@ -19,7 +19,6 @@ package marketdata import ( "context" - "encoding/json" "fmt" "net/http" @@ -29,6 +28,7 @@ import ( spottypes "github.com/linstohu/nexapi/binance/spot/marketdata/types" usdmtypes "github.com/linstohu/nexapi/binance/usdmfutures/marketdata/types" usdmutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" + "github.com/linstohu/nexapi/utils" "github.com/valyala/fastjson" ) @@ -54,14 +54,14 @@ func NewCoinMFuturesMarketDataClient(cfg *cmutils.CoinMarginedClientCfg) (*CoinM } func (c *CoinMFuturesMarketDataClient) Ping(ctx context.Context) error { - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/ping", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/ping", + Method: http.MethodGet, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return err } @@ -75,15 +75,15 @@ func (c *CoinMFuturesMarketDataClient) Ping(ctx context.Context) error { return nil } -func (c *CoinMFuturesMarketDataClient) GetServerTime(ctx context.Context) (*spottypes.ServerTime, error) { - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/time", - Method: http.MethodGet, +func (c *CoinMFuturesMarketDataClient) GetServerTime(ctx context.Context) (*spottypes.ServerTimeResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/time", + Method: http.MethodGet, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -94,23 +94,29 @@ func (c *CoinMFuturesMarketDataClient) GetServerTime(ctx context.Context) (*spot 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 (c *CoinMFuturesMarketDataClient) GetExchangeInfo(ctx context.Context) (*types.ExchangeInfo, error) { - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/exchangeInfo", - Method: http.MethodGet, +func (c *CoinMFuturesMarketDataClient) GetExchangeInfo(ctx context.Context) (*types.GetExchangeInfoResp, error) { + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/exchangeInfo", + Method: http.MethodGet, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -121,29 +127,35 @@ func (c *CoinMFuturesMarketDataClient) GetExchangeInfo(ctx context.Context) (*ty 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 (c *CoinMFuturesMarketDataClient) GetOrderbook(ctx context.Context, param types.GetOrderbookParams) (*types.Orderbook, error) { +func (c *CoinMFuturesMarketDataClient) GetOrderbook(ctx context.Context, param types.GetOrderbookParams) (*types.GetOrderbookResp, error) { err := c.validate.Struct(param) if err != nil { return nil, err } - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/depth", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/depth", + Method: http.MethodGet, + Query: param, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -154,29 +166,35 @@ func (c *CoinMFuturesMarketDataClient) GetOrderbook(ctx context.Context, param t 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 (c *CoinMFuturesMarketDataClient) GetRecentTradeList(ctx context.Context, param types.GetTradeParams) ([]*types.Trade, error) { +func (c *CoinMFuturesMarketDataClient) GetRecentTradeList(ctx context.Context, param types.GetTradeParams) (*types.GetTradeResp, error) { err := c.validate.Struct(param) if err != nil { return nil, err } - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/trades", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/trades", + Method: http.MethodGet, + Query: param, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -187,62 +205,72 @@ func (c *CoinMFuturesMarketDataClient) GetRecentTradeList(ctx context.Context, p 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 *CoinMFuturesMarketDataClient) GetAggTrades(ctx context.Context, param usdmtypes.GetAggTradesParam) ([]*usdmtypes.AggTrade, error) { - err := u.validate.Struct(param) +func (c *CoinMFuturesMarketDataClient) GetAggTrades(ctx context.Context, param usdmtypes.GetAggTradesParam) (*usdmtypes.GetAggTradesResp, error) { + err := c.validate.Struct(param) if err != nil { return nil, err } - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: u.GetBaseURL(), - Path: "/dapi/v1/aggTrades", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/aggTrades", + Method: http.MethodGet, + Query: param, } - headers, err := u.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } req.Headers = headers - resp, err := u.SendHTTPRequest(ctx, req) + resp, err := c.SendHTTPRequest(ctx, req) if err != nil { return nil, err } - var ret []*usdmtypes.AggTrade - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*usdmtypes.AggTrade + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &usdmtypes.GetAggTradesResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (c *CoinMFuturesMarketDataClient) GetMarkPrice(ctx context.Context, param types.GetMarkPriceParam) ([]*types.MarkPrice, error) { +func (c *CoinMFuturesMarketDataClient) GetMarkPrice(ctx context.Context, param types.GetMarkPriceParam) (*types.GetMarkPriceResp, error) { err := c.validate.Struct(param) if err != nil { return nil, err } - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/premiumIndex", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/premiumIndex", + Method: http.MethodGet, + Query: param, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -253,29 +281,34 @@ func (c *CoinMFuturesMarketDataClient) GetMarkPrice(ctx context.Context, param t 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 (c *CoinMFuturesMarketDataClient) GetFundingRateHistory(ctx context.Context, param types.GetFundingRateParam) ([]*types.FundingRate, error) { +func (c *CoinMFuturesMarketDataClient) GetFundingRateHistory(ctx context.Context, param types.GetFundingRateParam) (*types.GetFundingRateResp, error) { err := c.validate.Struct(param) if err != nil { return nil, err } - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/fundingRate", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/fundingRate", + Method: http.MethodGet, + Query: param, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -286,29 +319,34 @@ func (c *CoinMFuturesMarketDataClient) 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 (c *CoinMFuturesMarketDataClient) GetKlines(ctx context.Context, param usdmtypes.GetKlineParam) ([]*types.Kline, error) { +func (c *CoinMFuturesMarketDataClient) GetKlines(ctx context.Context, param usdmtypes.GetKlineParam) (*types.GetKlineResp, error) { err := c.validate.Struct(param) if err != nil { return nil, err } - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/klines", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/klines", + Method: http.MethodGet, + Query: param, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -319,8 +357,13 @@ func (c *CoinMFuturesMarketDataClient) GetKlines(ctx context.Context, param usdm 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 } @@ -356,24 +399,29 @@ func (c *CoinMFuturesMarketDataClient) GetKlines(ctx context.Context, param usdm }) } - return ret, nil + data := &types.GetKlineResp{ + Http: resp, + Body: ret, + } + + return data, nil } -func (c *CoinMFuturesMarketDataClient) GetTickerPrice(ctx context.Context, param types.GetPriceTickerParam) ([]*types.PriceTicker, error) { +func (c *CoinMFuturesMarketDataClient) GetTickerPrice(ctx context.Context, param types.GetPriceTickerParam) (*types.GetPriceTickerResp, error) { err := c.validate.Struct(param) if err != nil { return nil, err } - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/ticker/price", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/ticker/price", + Method: http.MethodGet, + Query: param, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -384,29 +432,34 @@ func (c *CoinMFuturesMarketDataClient) GetTickerPrice(ctx context.Context, param return nil, err } - var ret []*types.PriceTicker - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.PriceTicker + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetPriceTickerResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (c *CoinMFuturesMarketDataClient) GetBookTicker(ctx context.Context, param types.GetBookTickerParam) ([]*types.BookTicker, error) { +func (c *CoinMFuturesMarketDataClient) GetBookTicker(ctx context.Context, param types.GetBookTickerParam) (*types.GetBookTickerResp, error) { err := c.validate.Struct(param) if err != nil { return nil, err } - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/ticker/bookTicker", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/ticker/bookTicker", + Method: http.MethodGet, + Query: param, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -417,29 +470,34 @@ func (c *CoinMFuturesMarketDataClient) GetBookTicker(ctx context.Context, param 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.GetBookTickerResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (c *CoinMFuturesMarketDataClient) GetOpenInterest(ctx context.Context, param types.GetOpenInterestParam) (*types.OpenInterest, error) { +func (c *CoinMFuturesMarketDataClient) GetOpenInterest(ctx context.Context, param types.GetOpenInterestParam) (*types.GetOpenInterestResp, error) { err := c.validate.Struct(param) if err != nil { return nil, err } - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/dapi/v1/openInterest", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/dapi/v1/openInterest", + Method: http.MethodGet, + Query: param, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -450,29 +508,35 @@ func (c *CoinMFuturesMarketDataClient) GetOpenInterest(ctx context.Context, para return nil, err } - var ret types.OpenInterest - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.OpenInterest + + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.GetOpenInterestResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (c *CoinMFuturesMarketDataClient) GetOpenInterestHistory(ctx context.Context, param types.GetOpenInterestHistParam) ([]*types.OpenInterestHist, error) { +func (c *CoinMFuturesMarketDataClient) GetOpenInterestHistory(ctx context.Context, param types.GetOpenInterestHistParam) (*types.GetOpenInterestHistResp, error) { err := c.validate.Struct(param) if err != nil { return nil, err } - req := cmutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: c.GetBaseURL(), - Path: "/futures/data/openInterestHist", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: c.GetDebug(), + BaseURL: c.GetBaseURL(), + Path: "/futures/data/openInterestHist", + Method: http.MethodGet, + Query: param, } - headers, err := c.GenHeaders(req.SecurityType) + headers, err := c.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -483,10 +547,15 @@ func (c *CoinMFuturesMarketDataClient) GetOpenInterestHistory(ctx context.Contex 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/coinmfutures/marketdata/client_test.go b/binance/coinmfutures/marketdata/client_test.go index 5524a1f..d5717b9 100644 --- a/binance/coinmfutures/marketdata/client_test.go +++ b/binance/coinmfutures/marketdata/client_test.go @@ -99,7 +99,8 @@ func TestGetMarkPrice(t *testing.T) { Symbol: "ETHUSD_PERP", }) assert.Nil(t, err) - assert.Equal(t, 1, len(resp)) + + assert.Equal(t, 1, len(resp.Body)) _, err = cli.GetMarkPrice(context.TODO(), types.GetMarkPriceParam{}) assert.Nil(t, err) diff --git a/binance/coinmfutures/marketdata/types/book_ticker.go b/binance/coinmfutures/marketdata/types/book_ticker.go index f7131d6..6817d95 100644 --- a/binance/coinmfutures/marketdata/types/book_ticker.go +++ b/binance/coinmfutures/marketdata/types/book_ticker.go @@ -17,11 +17,18 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetBookTickerParam struct { Symbol string `url:"symbol,omitempty" validate:"omitempty"` Pair string `url:"pair,omitempty" validate:"omitempty"` } +type GetBookTickerResp struct { + Http *utils.ApiResponse + Body []*BookTicker +} + type BookTicker struct { Symbol string `json:"symbol"` Pair string `json:"pair"` diff --git a/binance/coinmfutures/marketdata/types/exchange_info.go b/binance/coinmfutures/marketdata/types/exchange_info.go index 23142e5..9f266c0 100644 --- a/binance/coinmfutures/marketdata/types/exchange_info.go +++ b/binance/coinmfutures/marketdata/types/exchange_info.go @@ -17,6 +17,13 @@ package types +import "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/coinmfutures/marketdata/types/funding_rate.go b/binance/coinmfutures/marketdata/types/funding_rate.go index cc8dca6..ef961b9 100644 --- a/binance/coinmfutures/marketdata/types/funding_rate.go +++ b/binance/coinmfutures/marketdata/types/funding_rate.go @@ -17,6 +17,8 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetFundingRateParam struct { Symbol string `url:"symbol" validate:"required"` 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/coinmfutures/marketdata/types/klines.go b/binance/coinmfutures/marketdata/types/klines.go index 7f5901a..20bf756 100644 --- a/binance/coinmfutures/marketdata/types/klines.go +++ b/binance/coinmfutures/marketdata/types/klines.go @@ -17,6 +17,13 @@ package types +import "github.com/linstohu/nexapi/utils" + +type GetKlineResp struct { + Http *utils.ApiResponse + Body []*Kline +} + type Kline struct { OpenTime int64 `json:"openTime"` OpenPrice string `json:"openPrice"` diff --git a/binance/coinmfutures/marketdata/types/mark_price.go b/binance/coinmfutures/marketdata/types/mark_price.go index 8521d2b..249be69 100644 --- a/binance/coinmfutures/marketdata/types/mark_price.go +++ b/binance/coinmfutures/marketdata/types/mark_price.go @@ -17,11 +17,18 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetMarkPriceParam struct { Symbol string `url:"symbol,omitempty" validate:"omitempty"` Pair string `url:"pair,omitempty" validate:"omitempty"` } +type GetMarkPriceResp struct { + Http *utils.ApiResponse + Body []*MarkPrice +} + type MarkPrice struct { Symbol string `json:"symbol"` Pair string `json:"pair"` diff --git a/binance/coinmfutures/marketdata/types/open_interest.go b/binance/coinmfutures/marketdata/types/open_interest.go index 4977f36..9a23679 100644 --- a/binance/coinmfutures/marketdata/types/open_interest.go +++ b/binance/coinmfutures/marketdata/types/open_interest.go @@ -19,12 +19,18 @@ package types import ( cmutils "github.com/linstohu/nexapi/binance/coinmfutures/utils" + "github.com/linstohu/nexapi/utils" ) type GetOpenInterestParam struct { Symbol string `url:"symbol" validate:"required"` } +type GetOpenInterestResp struct { + Http *utils.ApiResponse + Body *OpenInterest +} + type OpenInterest struct { Symbol string `json:"symbol"` Pair string `json:"pair"` @@ -42,6 +48,11 @@ type GetOpenInterestHistParam struct { Limit int `url:"limit,omitempty" validate:"omitempty,max=500"` } +type GetOpenInterestHistResp struct { + Http *utils.ApiResponse + Body []*OpenInterestHist +} + type OpenInterestHist struct { Pair string `json:"pair"` ContractType string `json:"contractType"` diff --git a/binance/coinmfutures/marketdata/types/orderbook.go b/binance/coinmfutures/marketdata/types/orderbook.go index 4908bb5..c0c5f9b 100644 --- a/binance/coinmfutures/marketdata/types/orderbook.go +++ b/binance/coinmfutures/marketdata/types/orderbook.go @@ -17,11 +17,18 @@ 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 { Symbol string `json:"symbol"` Pair string `json:"pair"` diff --git a/binance/coinmfutures/marketdata/types/price_ticker.go b/binance/coinmfutures/marketdata/types/price_ticker.go index d13d5bf..6343c5f 100644 --- a/binance/coinmfutures/marketdata/types/price_ticker.go +++ b/binance/coinmfutures/marketdata/types/price_ticker.go @@ -17,11 +17,18 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetPriceTickerParam struct { Symbol string `url:"symbol,omitempty" validate:"omitempty"` Pair string `url:"pair,omitempty" validate:"omitempty"` } +type GetPriceTickerResp struct { + Http *utils.ApiResponse + Body []*PriceTicker +} + type PriceTicker struct { Symbol string `json:"symbol"` Pair string `json:"pair"` diff --git a/binance/coinmfutures/marketdata/types/trades.go b/binance/coinmfutures/marketdata/types/trades.go index f6e5681..5e3c952 100644 --- a/binance/coinmfutures/marketdata/types/trades.go +++ b/binance/coinmfutures/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/coinmfutures/utils/client.go b/binance/coinmfutures/utils/client.go index e2b7aad..49e36d0 100644 --- a/binance/coinmfutures/utils/client.go +++ b/binance/coinmfutures/utils/client.go @@ -31,6 +31,7 @@ import ( "github.com/go-playground/validator" "github.com/google/go-querystring/query" usdmutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" + "github.com/linstohu/nexapi/utils" ) type CoinMarginedClient struct { @@ -132,7 +133,7 @@ func (u *CoinMarginedClient) NeedSignature(t usdmutils.SecurityType) bool { } } -func (u *CoinMarginedClient) SendHTTPRequest(ctx context.Context, req HTTPRequest) ([]byte, error) { +func (u *CoinMarginedClient) SendHTTPRequest(ctx context.Context, req utils.HTTPRequest) (*utils.ApiResponse, error) { client := http.Client{} var body io.Reader @@ -179,7 +180,6 @@ func (u *CoinMarginedClient) SendHTTPRequest(ctx context.Context, req HTTPReques if err != nil { return nil, err } - defer resp.Body.Close() if u.GetDebug() { dump, err := httputil.DumpResponse(resp, true) @@ -196,5 +196,5 @@ func (u *CoinMarginedClient) SendHTTPRequest(ctx context.Context, req HTTPReques 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/coinmfutures/utils/request.go b/binance/coinmfutures/utils/request.go deleted file mode 100644 index 3b18528..0000000 --- a/binance/coinmfutures/utils/request.go +++ /dev/null @@ -1,35 +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 cmutils - -import ( - usdmutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" -) - -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/delivery/en/#endpoint-security-type - SecurityType usdmutils.SecurityType - - BaseURL string - Path string - Method string - Headers map[string]string - Query any - Body any -} diff --git a/binance/europeanoptions/account/client.go b/binance/europeanoptions/account/client.go index bc7fc80..87944ee 100644 --- a/binance/europeanoptions/account/client.go +++ b/binance/europeanoptions/account/client.go @@ -22,7 +22,6 @@ import ( "crypto/hmac" "crypto/sha256" "encoding/hex" - "encoding/json" "fmt" "log/slog" "net/http" @@ -33,6 +32,7 @@ import ( eoutils "github.com/linstohu/nexapi/binance/europeanoptions/utils" usdmutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type OptionsAccountClient struct { @@ -79,15 +79,18 @@ func NewOptionsAccountClient(cfg *eoutils.OptionsClientCfg) (*OptionsAccountClie }, nil } -func (o *OptionsAccountClient) GetAccountInfo(ctx context.Context) (*types.AccountInfo, error) { - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/account", - Method: http.MethodGet, +func (o *OptionsAccountClient) GetAccountInfo(ctx context.Context) (*types.GetAccountInfoResp, error) { + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/account", + Method: http.MethodGet, } + + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return nil, err } @@ -105,7 +108,7 @@ func (o *OptionsAccountClient) GetAccountInfo(ctx context.Context) (*types.Accou return nil, err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -126,29 +129,36 @@ func (o *OptionsAccountClient) GetAccountInfo(ctx context.Context) (*types.Accou return nil, err } - var ret types.AccountInfo - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.AccountInfo + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.GetAccountInfoResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (o *OptionsAccountClient) NewOrder(ctx context.Context, param types.NewOrderParam) (*types.Order, error) { +func (o *OptionsAccountClient) NewOrder(ctx context.Context, param types.NewOrderParam) (*types.OrderResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/order", - Method: http.MethodPost, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/order", + Method: http.MethodPost, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return nil, err } @@ -169,7 +179,7 @@ func (o *OptionsAccountClient) NewOrder(ctx context.Context, param types.NewOrde return nil, err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -190,15 +200,20 @@ func (o *OptionsAccountClient) NewOrder(ctx context.Context, param types.NewOrde 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 (o *OptionsAccountClient) GetSingleOrder(ctx context.Context, param types.GetSingleOrderParam) (*types.Order, error) { +func (o *OptionsAccountClient) GetSingleOrder(ctx context.Context, param types.GetSingleOrderParam) (*types.OrderResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err @@ -208,15 +223,17 @@ func (o *OptionsAccountClient) GetSingleOrder(ctx context.Context, param types.G return nil, fmt.Errorf("either orderId or clientOrderId must be sent") } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/order", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/order", + Method: http.MethodGet, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return nil, err } @@ -237,7 +254,7 @@ func (o *OptionsAccountClient) GetSingleOrder(ctx context.Context, param types.G return nil, err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -258,15 +275,20 @@ func (o *OptionsAccountClient) GetSingleOrder(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 (o *OptionsAccountClient) CancelOrder(ctx context.Context, param types.CancelOrderParam) (*types.Order, error) { +func (o *OptionsAccountClient) CancelOrder(ctx context.Context, param types.CancelOrderParam) (*types.OrderResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err @@ -276,15 +298,17 @@ func (o *OptionsAccountClient) CancelOrder(ctx context.Context, param types.Canc return nil, fmt.Errorf("either orderId or clientOrderId must be sent") } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/order", - Method: http.MethodDelete, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/order", + Method: http.MethodDelete, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return nil, err } @@ -305,7 +329,7 @@ func (o *OptionsAccountClient) CancelOrder(ctx context.Context, param types.Canc return nil, err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -326,12 +350,17 @@ func (o *OptionsAccountClient) CancelOrder(ctx context.Context, param types.Canc 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 (o *OptionsAccountClient) CancelAllOrdersBySymbol(ctx context.Context, param types.CancelAllOrdersParam) error { @@ -340,15 +369,17 @@ func (o *OptionsAccountClient) CancelAllOrdersBySymbol(ctx context.Context, para return err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/allOpenOrders", - Method: http.MethodDelete, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/allOpenOrders", + Method: http.MethodDelete, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return err } @@ -369,7 +400,7 @@ func (o *OptionsAccountClient) CancelAllOrdersBySymbol(ctx context.Context, para return err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return err @@ -399,15 +430,17 @@ func (o *OptionsAccountClient) CancelAllOrdersByUnderlying(ctx context.Context, return err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/allOpenOrdersByUnderlying", - Method: http.MethodDelete, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/allOpenOrdersByUnderlying", + Method: http.MethodDelete, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return err } @@ -428,7 +461,7 @@ func (o *OptionsAccountClient) CancelAllOrdersByUnderlying(ctx context.Context, return err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return err @@ -452,21 +485,23 @@ func (o *OptionsAccountClient) CancelAllOrdersByUnderlying(ctx context.Context, return nil } -func (o *OptionsAccountClient) GetOpenOrders(ctx context.Context, param types.GetCurrentOpenOrdersParam) ([]*types.Order, error) { +func (o *OptionsAccountClient) GetOpenOrders(ctx context.Context, param types.GetCurrentOpenOrdersParam) (*types.OrdersResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/openOrders", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/openOrders", + Method: http.MethodGet, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return nil, err } @@ -487,7 +522,7 @@ func (o *OptionsAccountClient) GetOpenOrders(ctx context.Context, param types.Ge return nil, err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -508,29 +543,36 @@ func (o *OptionsAccountClient) GetOpenOrders(ctx context.Context, param types.Ge 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 (o *OptionsAccountClient) GetOrderHistory(ctx context.Context, param types.GetOrderHistoryParam) ([]*types.Order, error) { +func (o *OptionsAccountClient) GetOrderHistory(ctx context.Context, param types.GetOrderHistoryParam) (*types.OrdersResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/historyOrders", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/historyOrders", + Method: http.MethodGet, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return nil, err } @@ -551,7 +593,7 @@ func (o *OptionsAccountClient) GetOrderHistory(ctx context.Context, param types. return nil, err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -572,29 +614,36 @@ func (o *OptionsAccountClient) GetOrderHistory(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 (o *OptionsAccountClient) GetPositionInfo(ctx context.Context, param types.GetPositionInfoParam) ([]*types.Position, error) { +func (o *OptionsAccountClient) GetPositionInfo(ctx context.Context, param types.GetPositionInfoParam) (*types.GetPositionInfoResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/position", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/position", + Method: http.MethodGet, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return nil, err } @@ -615,7 +664,7 @@ func (o *OptionsAccountClient) GetPositionInfo(ctx context.Context, param types. return nil, err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -636,29 +685,36 @@ func (o *OptionsAccountClient) GetPositionInfo(ctx context.Context, param types. 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.GetPositionInfoResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (o *OptionsAccountClient) GetTradeList(ctx context.Context, param types.GetTradeListParam) ([]*types.UserTrade, error) { +func (o *OptionsAccountClient) GetTradeList(ctx context.Context, param types.GetTradeListParam) (*types.GetTradeListResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/userTrades", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/userTrades", + Method: http.MethodGet, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return nil, err } @@ -679,7 +735,7 @@ func (o *OptionsAccountClient) GetTradeList(ctx context.Context, param types.Get return nil, err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -700,29 +756,36 @@ func (o *OptionsAccountClient) GetTradeList(ctx context.Context, param types.Get return nil, err } - var ret []*types.UserTrade - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.UserTrade + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetTradeListResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (o *OptionsAccountClient) GetExerciseRecord(ctx context.Context, param types.GetExerciseRecordParam) ([]*types.UserTrade, error) { +func (o *OptionsAccountClient) GetExerciseRecord(ctx context.Context, param types.GetExerciseRecordParam) (*types.GetTradeListResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/exerciseRecord", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/exerciseRecord", + Method: http.MethodGet, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return nil, err } @@ -743,7 +806,7 @@ func (o *OptionsAccountClient) GetExerciseRecord(ctx context.Context, param type return nil, err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -764,29 +827,36 @@ func (o *OptionsAccountClient) GetExerciseRecord(ctx context.Context, param type return nil, err } - var ret []*types.UserTrade - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.UserTrade + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetTradeListResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (o *OptionsAccountClient) GetFundingFlow(ctx context.Context, param types.GetFundingFlowParam) ([]*types.FundingFlow, error) { +func (o *OptionsAccountClient) GetFundingFlow(ctx context.Context, param types.GetFundingFlowParam) (*types.GetFundingFlowResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.TRADE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/bill", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/bill", + Method: http.MethodGet, } + securityType := usdmutils.TRADE + { - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(securityType) if err != nil { return nil, err } @@ -807,7 +877,7 @@ func (o *OptionsAccountClient) GetFundingFlow(ctx context.Context, param types.G return nil, err } - if need := o.NeedSignature(req.SecurityType); need { + if need := o.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -828,10 +898,15 @@ func (o *OptionsAccountClient) GetFundingFlow(ctx context.Context, param types.G return nil, err } - var ret []*types.FundingFlow - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.FundingFlow + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetFundingFlowResp{ + Http: resp, + Body: body, + } + + return data, nil } diff --git a/binance/europeanoptions/account/types/account_info.go b/binance/europeanoptions/account/types/account_info.go index 9ef2401..3858fbb 100644 --- a/binance/europeanoptions/account/types/account_info.go +++ b/binance/europeanoptions/account/types/account_info.go @@ -17,6 +17,13 @@ package types +import "github.com/linstohu/nexapi/utils" + +type GetAccountInfoResp struct { + Http *utils.ApiResponse + Body *AccountInfo +} + type AccountInfo struct { Asset []struct { Asset string `json:"asset"` diff --git a/binance/europeanoptions/account/types/funding_flow.go b/binance/europeanoptions/account/types/funding_flow.go index 0bb5ede..e5a9e0e 100644 --- a/binance/europeanoptions/account/types/funding_flow.go +++ b/binance/europeanoptions/account/types/funding_flow.go @@ -19,6 +19,7 @@ package types import ( bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type GetFundingFlowParam struct { @@ -34,6 +35,11 @@ type GetFundingFlowParams struct { bnutils.DefaultParam } +type GetFundingFlowResp struct { + Http *utils.ApiResponse + Body []*FundingFlow +} + type FundingFlow struct { ID string `json:"id"` Asset string `json:"asset"` diff --git a/binance/europeanoptions/account/types/order.go b/binance/europeanoptions/account/types/order.go index 4802c4d..79dc2a0 100644 --- a/binance/europeanoptions/account/types/order.go +++ b/binance/europeanoptions/account/types/order.go @@ -20,8 +20,19 @@ package types import ( eoutils "github.com/linstohu/nexapi/binance/europeanoptions/utils" bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) +type OrderResp struct { + Http *utils.ApiResponse + Body *Order +} + +type OrdersResp struct { + Http *utils.ApiResponse + Body []*Order +} + type Order struct { OrderID int64 `json:"orderId"` Symbol string `json:"symbol"` diff --git a/binance/europeanoptions/account/types/position.go b/binance/europeanoptions/account/types/position.go index 65e0398..4567221 100644 --- a/binance/europeanoptions/account/types/position.go +++ b/binance/europeanoptions/account/types/position.go @@ -19,6 +19,7 @@ package types import ( bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type GetPositionInfoParam struct { @@ -30,6 +31,11 @@ type GetPositionInfoParams struct { bnutils.DefaultParam } +type GetPositionInfoResp struct { + Http *utils.ApiResponse + Body []*Position +} + type Position struct { EntryPrice string `json:"entryPrice"` Symbol string `json:"symbol"` diff --git a/binance/europeanoptions/account/types/trade.go b/binance/europeanoptions/account/types/trade.go index 84e1262..a437ae7 100644 --- a/binance/europeanoptions/account/types/trade.go +++ b/binance/europeanoptions/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 { @@ -34,6 +35,11 @@ type GetTradeListParams struct { bnutils.DefaultParam } +type GetTradeListResp struct { + Http *utils.ApiResponse + Body []*UserTrade +} + type UserTrade struct { ID int64 `json:"id"` TradeID int64 `json:"tradeId"` diff --git a/binance/europeanoptions/marketdata/client.go b/binance/europeanoptions/marketdata/client.go index 3c879c1..9defe2b 100644 --- a/binance/europeanoptions/marketdata/client.go +++ b/binance/europeanoptions/marketdata/client.go @@ -19,7 +19,6 @@ package marketdata import ( "context" - "encoding/json" "net/http" "github.com/go-playground/validator" @@ -28,6 +27,7 @@ import ( spottypes "github.com/linstohu/nexapi/binance/spot/marketdata/types" usdmtypes "github.com/linstohu/nexapi/binance/usdmfutures/marketdata/types" usdmutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" + "github.com/linstohu/nexapi/utils" ) type OptionsMarketDataClient struct { @@ -52,14 +52,14 @@ func NewOptionsMarketDataClient(cfg *eoutils.OptionsClientCfg) (*OptionsMarketDa } func (o *OptionsMarketDataClient) Ping(ctx context.Context) error { - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/ping", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/ping", + Method: http.MethodGet, } - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(usdmutils.NONE) if err != nil { return err } @@ -73,15 +73,15 @@ func (o *OptionsMarketDataClient) Ping(ctx context.Context) error { return nil } -func (o *OptionsMarketDataClient) GetServerTime(ctx context.Context) (*spottypes.ServerTime, error) { - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/time", - Method: http.MethodGet, +func (o *OptionsMarketDataClient) GetServerTime(ctx context.Context) (*spottypes.ServerTimeResp, error) { + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/time", + Method: http.MethodGet, } - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -92,23 +92,28 @@ func (o *OptionsMarketDataClient) GetServerTime(ctx context.Context) (*spottypes 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 (o *OptionsMarketDataClient) GetExchangeInfo(ctx context.Context) (*types.ExchangeInfo, error) { - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/exchangeInfo", - Method: http.MethodGet, +func (o *OptionsMarketDataClient) GetExchangeInfo(ctx context.Context) (*types.GetExchangeInfoResp, error) { + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/exchangeInfo", + Method: http.MethodGet, } - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -119,29 +124,35 @@ func (o *OptionsMarketDataClient) GetExchangeInfo(ctx context.Context) (*types.E 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 (o *OptionsMarketDataClient) GetOrderbook(ctx context.Context, param types.GetOrderbookParams) (*types.Orderbook, error) { +func (o *OptionsMarketDataClient) GetOrderbook(ctx context.Context, param types.GetOrderbookParams) (*types.GetOrderbookResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/depth", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/depth", + Method: http.MethodGet, + Query: param, } - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -152,29 +163,35 @@ func (o *OptionsMarketDataClient) GetOrderbook(ctx context.Context, param types. 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 (o *OptionsMarketDataClient) GetRecentTradesList(ctx context.Context, param types.GetTradeParams) ([]*types.Trade, error) { +func (o *OptionsMarketDataClient) GetRecentTradesList(ctx context.Context, param types.GetTradeParams) (*types.GetTradeResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/trades", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/trades", + Method: http.MethodGet, + Query: param, } - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -185,29 +202,34 @@ func (o *OptionsMarketDataClient) GetRecentTradesList(ctx context.Context, param 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 (o *OptionsMarketDataClient) GetKlines(ctx context.Context, param usdmtypes.GetKlineParam) ([]*types.Kline, error) { +func (o *OptionsMarketDataClient) GetKlines(ctx context.Context, param usdmtypes.GetKlineParam) (*types.GetKlineResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/klines", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/klines", + Method: http.MethodGet, + Query: param, } - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -218,29 +240,34 @@ func (o *OptionsMarketDataClient) GetKlines(ctx context.Context, param usdmtypes return nil, err } - var ret []*types.Kline - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.Kline + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetKlineResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (o *OptionsMarketDataClient) GetMarkPrice(ctx context.Context, param types.GetMarkPriceParam) ([]*types.MarkPrice, error) { +func (o *OptionsMarketDataClient) GetMarkPrice(ctx context.Context, param types.GetMarkPriceParam) (*types.GetMarkPriceResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/mark", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/mark", + Method: http.MethodGet, + Query: param, } - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -251,29 +278,34 @@ func (o *OptionsMarketDataClient) GetMarkPrice(ctx context.Context, param types. 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 (o *OptionsMarketDataClient) GetTickerPrice(ctx context.Context, param types.GetTickerPriceParam) ([]*types.TickerPrice, error) { +func (o *OptionsMarketDataClient) GetTickerPrice(ctx context.Context, param types.GetTickerPriceParam) (*types.GetTickerPriceResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/ticker", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/ticker", + Method: http.MethodGet, + Query: param, } - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -284,29 +316,34 @@ func (o *OptionsMarketDataClient) GetTickerPrice(ctx context.Context, param type 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.GetTickerPriceResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (o *OptionsMarketDataClient) GetUnderlyingIndexPrice(ctx context.Context, param types.GetUnderlyingIndexPriceParams) (*types.UnderlyingIndexPrice, error) { +func (o *OptionsMarketDataClient) GetUnderlyingIndexPrice(ctx context.Context, param types.GetUnderlyingIndexPriceParams) (*types.GetUnderlyingIndexPriceResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/index", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/index", + Method: http.MethodGet, + Query: param, } - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -317,29 +354,34 @@ func (o *OptionsMarketDataClient) GetUnderlyingIndexPrice(ctx context.Context, p return nil, err } - var ret types.UnderlyingIndexPrice - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.UnderlyingIndexPrice + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &types.GetUnderlyingIndexPriceResp{ + Http: resp, + Body: &body, + } + + return data, nil } -func (o *OptionsMarketDataClient) GetOpenInterest(ctx context.Context, param types.GetOpenInterestParam) ([]*types.OpenInterest, error) { +func (o *OptionsMarketDataClient) GetOpenInterest(ctx context.Context, param types.GetOpenInterestParam) (*types.GetOpenInterestResp, error) { err := o.validate.Struct(param) if err != nil { return nil, err } - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.NONE, - BaseURL: o.GetBaseURL(), - Path: "/eapi/v1/openInterest", - Method: http.MethodGet, - Query: param, + req := utils.HTTPRequest{ + Debug: o.GetDebug(), + BaseURL: o.GetBaseURL(), + Path: "/eapi/v1/openInterest", + Method: http.MethodGet, + Query: param, } - headers, err := o.GenHeaders(req.SecurityType) + headers, err := o.GenHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -350,10 +392,15 @@ func (o *OptionsMarketDataClient) GetOpenInterest(ctx context.Context, param typ return nil, err } - var ret []*types.OpenInterest - if err := json.Unmarshal(resp, &ret); err != nil { + var body []*types.OpenInterest + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return ret, nil + data := &types.GetOpenInterestResp{ + Http: resp, + Body: body, + } + + return data, nil } diff --git a/binance/europeanoptions/marketdata/types/exchange_info.go b/binance/europeanoptions/marketdata/types/exchange_info.go index 2d728f1..0c7070d 100644 --- a/binance/europeanoptions/marketdata/types/exchange_info.go +++ b/binance/europeanoptions/marketdata/types/exchange_info.go @@ -17,6 +17,13 @@ package types +import "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/europeanoptions/marketdata/types/index.go b/binance/europeanoptions/marketdata/types/index.go index 52c231f..1cde7c6 100644 --- a/binance/europeanoptions/marketdata/types/index.go +++ b/binance/europeanoptions/marketdata/types/index.go @@ -17,10 +17,17 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetUnderlyingIndexPriceParams struct { Underlying string `url:"underlying" validate:"required"` } +type GetUnderlyingIndexPriceResp struct { + Http *utils.ApiResponse + Body *UnderlyingIndexPrice +} + type UnderlyingIndexPrice struct { Time int64 `json:"time"` IndexPrice string `json:"indexPrice"` diff --git a/binance/europeanoptions/marketdata/types/klines.go b/binance/europeanoptions/marketdata/types/klines.go index fd869d7..9445b90 100644 --- a/binance/europeanoptions/marketdata/types/klines.go +++ b/binance/europeanoptions/marketdata/types/klines.go @@ -17,6 +17,13 @@ package types +import "github.com/linstohu/nexapi/utils" + +type GetKlineResp struct { + Http *utils.ApiResponse + Body []*Kline +} + type Kline struct { Open string `json:"open"` High string `json:"high"` diff --git a/binance/europeanoptions/marketdata/types/mark_price.go b/binance/europeanoptions/marketdata/types/mark_price.go index 84f56fd..5673f1a 100644 --- a/binance/europeanoptions/marketdata/types/mark_price.go +++ b/binance/europeanoptions/marketdata/types/mark_price.go @@ -17,10 +17,17 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetMarkPriceParam struct { Symbol string `url:"symbol,omitempty" validate:"omitempty"` } +type GetMarkPriceResp struct { + Http *utils.ApiResponse + Body []*MarkPrice +} + type MarkPrice struct { Symbol string `json:"symbol"` MarkPrice string `json:"markPrice"` diff --git a/binance/europeanoptions/marketdata/types/open_interest.go b/binance/europeanoptions/marketdata/types/open_interest.go index 36c57dc..a95f658 100644 --- a/binance/europeanoptions/marketdata/types/open_interest.go +++ b/binance/europeanoptions/marketdata/types/open_interest.go @@ -17,11 +17,18 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetOpenInterestParam struct { UnderlyingAsset string `url:"underlyingAsset" validate:"required"` Expiration string `url:"expiration" validate:"required"` } +type GetOpenInterestResp struct { + Http *utils.ApiResponse + Body []*OpenInterest +} + type OpenInterest struct { Symbol string `json:"symbol"` SumOpenInterest string `json:"sumOpenInterest"` diff --git a/binance/europeanoptions/marketdata/types/orderbook.go b/binance/europeanoptions/marketdata/types/orderbook.go index 03244ab..5a41b6a 100644 --- a/binance/europeanoptions/marketdata/types/orderbook.go +++ b/binance/europeanoptions/marketdata/types/orderbook.go @@ -17,11 +17,18 @@ 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=10 20 50 100 500 1000"` } +type GetOrderbookResp struct { + Http *utils.ApiResponse + Body *Orderbook +} + type Orderbook struct { TransactionTime int64 `json:"T"` UpdateID int64 `json:"u"` diff --git a/binance/europeanoptions/marketdata/types/ticker.go b/binance/europeanoptions/marketdata/types/ticker.go index 93f5a9e..2f60b7a 100644 --- a/binance/europeanoptions/marketdata/types/ticker.go +++ b/binance/europeanoptions/marketdata/types/ticker.go @@ -17,10 +17,17 @@ package types +import "github.com/linstohu/nexapi/utils" + type GetTickerPriceParam struct { Symbol string `url:"symbol,omitempty" validate:"omitempty"` } +type GetTickerPriceResp struct { + Http *utils.ApiResponse + Body []*TickerPrice +} + type TickerPrice struct { Symbol string `json:"symbol"` PriceChange string `json:"priceChange"` diff --git a/binance/europeanoptions/marketdata/types/trades.go b/binance/europeanoptions/marketdata/types/trades.go index 08f90a7..7cf6de4 100644 --- a/binance/europeanoptions/marketdata/types/trades.go +++ b/binance/europeanoptions/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=500"` } +type GetTradeResp struct { + Http *utils.ApiResponse + Body []*Trade +} + type Trade struct { ID int64 `json:"id"` Symbol string `json:"symbol"` diff --git a/binance/europeanoptions/utils/client.go b/binance/europeanoptions/utils/client.go index 8339e80..3cfaa62 100644 --- a/binance/europeanoptions/utils/client.go +++ b/binance/europeanoptions/utils/client.go @@ -31,6 +31,7 @@ import ( "github.com/go-playground/validator" "github.com/google/go-querystring/query" usdmutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" + "github.com/linstohu/nexapi/utils" ) type OptionsClient struct { @@ -132,7 +133,7 @@ func (o *OptionsClient) NeedSignature(t usdmutils.SecurityType) bool { } } -func (o *OptionsClient) SendHTTPRequest(ctx context.Context, req HTTPRequest) ([]byte, error) { +func (o *OptionsClient) SendHTTPRequest(ctx context.Context, req utils.HTTPRequest) (*utils.ApiResponse, error) { client := http.Client{} var body io.Reader @@ -178,7 +179,6 @@ func (o *OptionsClient) SendHTTPRequest(ctx context.Context, req HTTPRequest) ([ if err != nil { return nil, err } - defer resp.Body.Close() if o.GetDebug() { dump, err := httputil.DumpResponse(resp, true) @@ -195,5 +195,5 @@ func (o *OptionsClient) 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/europeanoptions/utils/request.go b/binance/europeanoptions/utils/request.go deleted file mode 100644 index e83c6d1..0000000 --- a/binance/europeanoptions/utils/request.go +++ /dev/null @@ -1,35 +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 eoutils - -import ( - usdmutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" -) - -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/voptions/en/#endpoint-security-type - SecurityType usdmutils.SecurityType - - BaseURL string - Path string - Method string - Headers map[string]string - Query any - Body any -} diff --git a/binance/europeanoptions/websocketuserdata/client.go b/binance/europeanoptions/websocketuserdata/client.go index bb6d467..f1e1453 100644 --- a/binance/europeanoptions/websocketuserdata/client.go +++ b/binance/europeanoptions/websocketuserdata/client.go @@ -165,7 +165,11 @@ func (o *OptionsUserDataStreamClient) genListenKey() (string, error) { return "", err } - return resp.ListenKey, nil + if resp.Body == nil { + return "", fmt.Errorf("unknown error") + } + + return resp.Body.ListenKey, nil } func (o *OptionsUserDataStreamClient) updateListenKey() error { diff --git a/binance/europeanoptions/websocketuserdata/listenkey.go b/binance/europeanoptions/websocketuserdata/listenkey.go index abd1d32..914cd5a 100644 --- a/binance/europeanoptions/websocketuserdata/listenkey.go +++ b/binance/europeanoptions/websocketuserdata/listenkey.go @@ -19,13 +19,13 @@ package websocketuserdata import ( "context" - "encoding/json" "log/slog" "net/http" "github.com/go-playground/validator" eoutils "github.com/linstohu/nexapi/binance/europeanoptions/utils" usdmutils "github.com/linstohu/nexapi/binance/usdmfutures/utils" + "github.com/linstohu/nexapi/utils" ) type httpAuthClient struct { @@ -67,18 +67,26 @@ func newHttpAuthClient(cfg *httpAuthClientCfg) (*httpAuthClient, error) { } type listenKeyResp struct { + Http *utils.ApiResponse + Body *listenKeyAPIResp +} + +type listenKeyAPIResp struct { ListenKey string `json:"listenKey,omitempty"` } func (h *httpAuthClient) genListenKey(ctx context.Context) (*listenKeyResp, error) { - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.USER_STREAM, - BaseURL: h.GetBaseURL(), - Path: "/eapi/v1/listenKey", - Method: http.MethodPost, + req := utils.HTTPRequest{ + Debug: h.GetDebug(), + BaseURL: h.GetBaseURL(), + Path: "/eapi/v1/listenKey", + Method: http.MethodPost, } + + securityType := usdmutils.USER_STREAM + { - headers, err := h.GenHeaders(req.SecurityType) + headers, err := h.GenHeaders(securityType) if err != nil { return nil, err } @@ -90,23 +98,31 @@ func (h *httpAuthClient) genListenKey(ctx context.Context) (*listenKeyResp, erro return nil, err } - var ret listenKeyResp - if err := json.Unmarshal(resp, &ret); err != nil { + var body listenKeyAPIResp + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + data := &listenKeyResp{ + Http: resp, + Body: &body, + } + + return data, nil } func (h *httpAuthClient) updateListenKey(ctx context.Context) error { - req := eoutils.HTTPRequest{ - SecurityType: usdmutils.USER_STREAM, - BaseURL: h.GetBaseURL(), - Path: "/eapi/v1/listenKey", - Method: http.MethodPut, + req := utils.HTTPRequest{ + Debug: h.GetDebug(), + BaseURL: h.GetBaseURL(), + Path: "/eapi/v1/listenKey", + Method: http.MethodPut, } + + securityType := usdmutils.USER_STREAM + { - headers, err := h.GenHeaders(req.SecurityType) + headers, err := h.GenHeaders(securityType) if err != nil { return err } diff --git a/binance/portfoliomargin/rest/client.go b/binance/portfoliomargin/rest/client.go index 8151c9e..53f390a 100644 --- a/binance/portfoliomargin/rest/client.go +++ b/binance/portfoliomargin/rest/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/portfoliomargin/rest/types" pmutils "github.com/linstohu/nexapi/binance/portfoliomargin/utils" bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type PortfolioMarginAccountClient struct { @@ -77,20 +77,23 @@ func NewPortfolioMarginAccountClient(cfg *pmutils.PortfolioMarginClientCfg) (*Po }, nil } -func (p *PortfolioMarginAccountClient) GetAssetBalance(ctx context.Context, param types.GetBalanceParam) (*types.Balance, error) { +func (p *PortfolioMarginAccountClient) GetAssetBalance(ctx context.Context, param types.GetBalanceParam) (*types.GetBalanceResp, error) { err := p.validate.Struct(param) if err != nil { return nil, err } - req := pmutils.HTTPRequest{ - SecurityType: pmutils.USER_DATA, - BaseURL: p.GetBaseURL(), - Path: "/papi/v1/balance", - Method: http.MethodGet, + req := utils.HTTPRequest{ + Debug: p.GetDebug(), + BaseURL: p.GetBaseURL(), + Path: "/papi/v1/balance", + Method: http.MethodGet, } + + securityType := pmutils.USER_DATA + { - headers, err := p.GenHeaders(req.SecurityType) + headers, err := p.GenHeaders(securityType) if err != nil { return nil, err } @@ -111,7 +114,7 @@ func (p *PortfolioMarginAccountClient) GetAssetBalance(ctx context.Context, para return nil, err } - if need := p.NeedSignature(req.SecurityType); need { + if need := p.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -132,23 +135,31 @@ func (p *PortfolioMarginAccountClient) GetAssetBalance(ctx context.Context, para 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 (p *PortfolioMarginAccountClient) GetBalance(ctx context.Context) ([]*types.Balance, error) { - req := pmutils.HTTPRequest{ - SecurityType: pmutils.USER_DATA, - BaseURL: p.GetBaseURL(), - Path: "/papi/v1/balance", - Method: http.MethodGet, +func (p *PortfolioMarginAccountClient) GetBalance(ctx context.Context) (*types.GetAllBalanceResp, error) { + req := utils.HTTPRequest{ + Debug: p.GetDebug(), + BaseURL: p.GetBaseURL(), + Path: "/papi/v1/balance", + Method: http.MethodGet, } + + securityType := pmutils.USER_DATA + { - headers, err := p.GenHeaders(req.SecurityType) + headers, err := p.GenHeaders(securityType) if err != nil { return nil, err } @@ -168,7 +179,7 @@ func (p *PortfolioMarginAccountClient) GetBalance(ctx context.Context) ([]*types return nil, err } - if need := p.NeedSignature(req.SecurityType); need { + if need := p.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -189,23 +200,31 @@ func (p *PortfolioMarginAccountClient) GetBalance(ctx context.Context) ([]*types 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.GetAllBalanceResp{ + Http: resp, + Body: body, + } + + return data, nil } -func (p *PortfolioMarginAccountClient) GetAccountInformation(ctx context.Context) (*types.Account, error) { - req := pmutils.HTTPRequest{ - SecurityType: pmutils.USER_DATA, - BaseURL: p.GetBaseURL(), - Path: "/papi/v1/account", - Method: http.MethodGet, +func (p *PortfolioMarginAccountClient) GetAccountInformation(ctx context.Context) (*types.GetAccountInfoResp, error) { + req := utils.HTTPRequest{ + Debug: p.GetDebug(), + BaseURL: p.GetBaseURL(), + Path: "/papi/v1/account", + Method: http.MethodGet, } + + securityType := pmutils.USER_DATA + { - headers, err := p.GenHeaders(req.SecurityType) + headers, err := p.GenHeaders(securityType) if err != nil { return nil, err } @@ -223,7 +242,7 @@ func (p *PortfolioMarginAccountClient) GetAccountInformation(ctx context.Context return nil, err } - if need := p.NeedSignature(req.SecurityType); need { + if need := p.NeedSignature(securityType); need { signString, err := bnutils.NormalizeRequestContent(query, nil) if err != nil { return nil, err @@ -244,10 +263,15 @@ func (p *PortfolioMarginAccountClient) 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 } diff --git a/binance/portfoliomargin/rest/types/account.go b/binance/portfoliomargin/rest/types/account.go index aa9c4f2..f7cb5b0 100644 --- a/binance/portfoliomargin/rest/types/account.go +++ b/binance/portfoliomargin/rest/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 { UniMMR string `json:"uniMMR"` AccountEquity string `json:"accountEquity"` diff --git a/binance/portfoliomargin/rest/types/balance.go b/binance/portfoliomargin/rest/types/balance.go index ed051e0..217a71b 100644 --- a/binance/portfoliomargin/rest/types/balance.go +++ b/binance/portfoliomargin/rest/types/balance.go @@ -19,6 +19,7 @@ package types import ( bnutils "github.com/linstohu/nexapi/binance/utils" + "github.com/linstohu/nexapi/utils" ) type GetBalanceParam struct { @@ -30,6 +31,11 @@ type GetBalanceParams struct { bnutils.DefaultParam } +type GetBalanceResp struct { + Http *utils.ApiResponse + Body *Balance +} + type GetAllBalanceParam struct { Asset string `url:"asset"` } @@ -39,6 +45,11 @@ type GetAllBalanceParams struct { bnutils.DefaultParam } +type GetAllBalanceResp struct { + Http *utils.ApiResponse + Body []*Balance +} + type Balance struct { Asset string `json:"asset"` TotalWalletBalance string `json:"totalWalletBalance"` diff --git a/binance/portfoliomargin/utils/client.go b/binance/portfoliomargin/utils/client.go index e81dfdc..9dd50f2 100644 --- a/binance/portfoliomargin/utils/client.go +++ b/binance/portfoliomargin/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 PortfolioMarginClient struct { @@ -131,7 +132,7 @@ func (p *PortfolioMarginClient) NeedSignature(t SecurityType) bool { } } -func (p *PortfolioMarginClient) SendHTTPRequest(ctx context.Context, req HTTPRequest) ([]byte, error) { +func (p *PortfolioMarginClient) SendHTTPRequest(ctx context.Context, req utils.HTTPRequest) (*utils.ApiResponse, error) { client := http.Client{} var body io.Reader @@ -177,7 +178,6 @@ func (p *PortfolioMarginClient) SendHTTPRequest(ctx context.Context, req HTTPReq if err != nil { return nil, err } - defer resp.Body.Close() if p.GetDebug() { dump, err := httputil.DumpResponse(resp, true) @@ -194,5 +194,5 @@ func (p *PortfolioMarginClient) SendHTTPRequest(ctx context.Context, req HTTPReq 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/portfoliomargin/utils/request.go b/binance/portfoliomargin/utils/request.go deleted file mode 100644 index 32c27c6..0000000 --- a/binance/portfoliomargin/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 cmutils - -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/pm/en/#endpoint-security-type - SecurityType SecurityType - - BaseURL string - Path string - Method string - Headers map[string]string - Query any - Body any -} diff --git a/bitfinex/restauth/api.go b/bitfinex/restauth/api.go index a55cda6..27623e7 100644 --- a/bitfinex/restauth/api.go +++ b/bitfinex/restauth/api.go @@ -22,6 +22,7 @@ import ( "net/http" "github.com/go-playground/validator" + "github.com/linstohu/nexapi/utils" ) var ( @@ -50,7 +51,8 @@ func NewRestAuthClient(cfg *BitfinexClientCfg) (*RestAuthClient, error) { } func (r *RestAuthClient) GetWallets(ctx context.Context) error { - req := HTTPRequest{ + req := utils.HTTPRequest{ + Debug: r.GetDebug(), BaseURL: r.GetBaseURL(), Path: "/v2/auth/r/wallets", Method: http.MethodPost, diff --git a/bitfinex/restauth/client.go b/bitfinex/restauth/client.go index ae95cb5..6016041 100644 --- a/bitfinex/restauth/client.go +++ b/bitfinex/restauth/client.go @@ -33,7 +33,8 @@ import ( "github.com/go-playground/validator" "github.com/google/go-querystring/query" - "github.com/linstohu/nexapi/bitfinex/utils" + bitutils "github.com/linstohu/nexapi/bitfinex/utils" + "github.com/linstohu/nexapi/utils" ) type BitfinexAuthClient struct { @@ -41,7 +42,7 @@ type BitfinexAuthClient struct { debug bool // logger logger *slog.Logger - nonce utils.NonceGenerator + nonce bitutils.NonceGenerator baseURL string key, secret string @@ -67,7 +68,7 @@ func NewBitfinexClient(cfg *BitfinexClientCfg) (*BitfinexAuthClient, error) { cli := BitfinexAuthClient{ debug: cfg.Debug, logger: cfg.Logger, - nonce: utils.NewEpochNonceGenerator(), + nonce: bitutils.NewEpochNonceGenerator(), baseURL: cfg.BaseURL, key: cfg.Key, secret: cfg.Secret, @@ -97,7 +98,7 @@ func (b *BitfinexAuthClient) GetBaseURL() string { return b.baseURL } -func (b *BitfinexAuthClient) GenHeaders(req HTTPRequest) (map[string]string, error) { +func (b *BitfinexAuthClient) GenHeaders(req utils.HTTPRequest) (map[string]string, error) { headers := map[string]string{ "Content-Type": "application/json", "Accept": "application/json", @@ -127,7 +128,7 @@ func (b *BitfinexAuthClient) GenHeaders(req HTTPRequest) (map[string]string, err return headers, nil } -func (b *BitfinexAuthClient) SendHTTPRequest(ctx context.Context, req HTTPRequest) ([]byte, error) { +func (b *BitfinexAuthClient) SendHTTPRequest(ctx context.Context, req utils.HTTPRequest) (*utils.ApiResponse, error) { client := http.Client{} var body io.Reader @@ -173,7 +174,6 @@ func (b *BitfinexAuthClient) SendHTTPRequest(ctx context.Context, req HTTPReques if err != nil { return nil, err } - defer resp.Body.Close() if b.GetDebug() { dump, err := httputil.DumpResponse(resp, true) @@ -190,5 +190,5 @@ func (b *BitfinexAuthClient) SendHTTPRequest(ctx context.Context, req HTTPReques 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/bitfinex/restpub/api.go b/bitfinex/restpub/api.go index d565d25..ece1833 100644 --- a/bitfinex/restpub/api.go +++ b/bitfinex/restpub/api.go @@ -22,6 +22,7 @@ import ( "net/http" "github.com/go-playground/validator" + "github.com/linstohu/nexapi/utils" ) var ( @@ -50,7 +51,8 @@ func NewRestPubClient(cfg *BitfinexClientCfg) (*RestPubClient, error) { } func (r *RestPubClient) PlatformStatus(ctx context.Context) error { - req := HTTPRequest{ + req := utils.HTTPRequest{ + Debug: r.GetDebug(), BaseURL: r.GetBaseURL(), Path: "/v2/platform/status", Method: http.MethodGet, diff --git a/bitfinex/restpub/client.go b/bitfinex/restpub/client.go index c2caaf3..72978f9 100644 --- a/bitfinex/restpub/client.go +++ b/bitfinex/restpub/client.go @@ -30,6 +30,7 @@ import ( "github.com/go-playground/validator" "github.com/google/go-querystring/query" + "github.com/linstohu/nexapi/utils" ) type BitfinexClient struct { @@ -71,15 +72,6 @@ func NewBitfinexClient(cfg *BitfinexClientCfg) (*BitfinexClient, error) { return &cli, nil } -type HTTPRequest struct { - BaseURL string - Path string - Method string - Headers map[string]string - Query any - Body any -} - func (b *BitfinexClient) GetDebug() bool { return b.debug } @@ -88,7 +80,7 @@ func (b *BitfinexClient) GetBaseURL() string { return b.baseURL } -func (b *BitfinexClient) SendHTTPRequest(ctx context.Context, req HTTPRequest) ([]byte, error) { +func (b *BitfinexClient) SendHTTPRequest(ctx context.Context, req utils.HTTPRequest) (*utils.ApiResponse, error) { client := http.Client{} var body io.Reader @@ -134,7 +126,6 @@ func (b *BitfinexClient) SendHTTPRequest(ctx context.Context, req HTTPRequest) ( if err != nil { return nil, err } - defer resp.Body.Close() if b.GetDebug() { dump, err := httputil.DumpResponse(resp, true) @@ -151,5 +142,5 @@ func (b *BitfinexClient) 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/okx/publicdata/client.go b/okx/publicdata/client.go index de8de75..0c25e93 100644 --- a/okx/publicdata/client.go +++ b/okx/publicdata/client.go @@ -19,12 +19,12 @@ package publicdata import ( "context" - "encoding/json" "net/http" "github.com/go-playground/validator" "github.com/linstohu/nexapi/okx/publicdata/types" okxutils "github.com/linstohu/nexapi/okx/utils" + "github.com/linstohu/nexapi/utils" ) type PublicDataClient struct { @@ -58,7 +58,8 @@ func NewPublicDataClient(cfg *okxutils.OKXRestClientCfg) (*PublicDataClient, err } func (p *PublicDataClient) GetInstruments(ctx context.Context, param types.GetInstrumentsParam) (*types.Instruments, error) { - req := okxutils.HTTPRequest{ + req := utils.HTTPRequest{ + Debug: p.GetDebug(), BaseURL: p.GetBaseURL(), Path: "/api/v5/public/instruments", Method: http.MethodGet, @@ -76,16 +77,17 @@ func (p *PublicDataClient) GetInstruments(ctx context.Context, param types.GetIn return nil, err } - var ret types.Instruments - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.Instruments + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + return &body, nil } func (p *PublicDataClient) GetMarketTickers(ctx context.Context, param types.GetMarketTickersParam) (*types.GetMarketTickersResp, error) { - req := okxutils.HTTPRequest{ + req := utils.HTTPRequest{ + Debug: p.GetDebug(), BaseURL: p.GetBaseURL(), Path: "/api/v5/market/tickers", Method: http.MethodGet, @@ -103,10 +105,10 @@ func (p *PublicDataClient) GetMarketTickers(ctx context.Context, param types.Get return nil, err } - var ret types.GetMarketTickersResp - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.GetMarketTickersResp + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + return &body, nil } diff --git a/okx/tradingaccount/client.go b/okx/tradingaccount/client.go index 3a3f11c..6964da7 100644 --- a/okx/tradingaccount/client.go +++ b/okx/tradingaccount/client.go @@ -19,13 +19,13 @@ package publicdata import ( "context" - "encoding/json" "log/slog" "net/http" "github.com/go-playground/validator" "github.com/linstohu/nexapi/okx/tradingaccount/types" okxutils "github.com/linstohu/nexapi/okx/utils" + "github.com/linstohu/nexapi/utils" ) type TradingAccountClient struct { @@ -77,7 +77,8 @@ func (t *TradingAccountClient) GetBalance(ctx context.Context, param types.GetBa return nil, err } - req := okxutils.HTTPRequest{ + req := utils.HTTPRequest{ + Debug: t.GetDebug(), BaseURL: t.GetBaseURL(), Path: "/api/v5/account/balance", Method: http.MethodGet, @@ -95,12 +96,12 @@ func (t *TradingAccountClient) GetBalance(ctx context.Context, param types.GetBa return nil, err } - var ret types.GetBalanceResp - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.GetBalanceResp + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + return &body, nil } func (t *TradingAccountClient) GetPositions(ctx context.Context, param types.GetPositionsParam) (*types.GetPositionsResp, error) { @@ -109,7 +110,8 @@ func (t *TradingAccountClient) GetPositions(ctx context.Context, param types.Get return nil, err } - req := okxutils.HTTPRequest{ + req := utils.HTTPRequest{ + Debug: t.GetDebug(), BaseURL: t.GetBaseURL(), Path: "/api/v5/account/positions", Method: http.MethodGet, @@ -127,10 +129,10 @@ func (t *TradingAccountClient) GetPositions(ctx context.Context, param types.Get return nil, err } - var ret types.GetPositionsResp - if err := json.Unmarshal(resp, &ret); err != nil { + var body types.GetPositionsResp + if err := resp.ReadJsonBody(&body); err != nil { return nil, err } - return &ret, nil + return &body, nil } diff --git a/okx/utils/request.go b/okx/utils/request.go index ee5ec57..f34620d 100644 --- a/okx/utils/request.go +++ b/okx/utils/request.go @@ -17,15 +17,6 @@ package okxutils -type HTTPRequest struct { - BaseURL string - Path string - Method string - Headers map[string]string - Query any - Body any -} - type Response struct { Code string `json:"code"` Message string `json:"messsage"` diff --git a/okx/utils/rest.go b/okx/utils/rest.go index 699bc64..7a4df55 100644 --- a/okx/utils/rest.go +++ b/okx/utils/rest.go @@ -34,6 +34,7 @@ import ( "github.com/go-playground/validator" "github.com/google/go-querystring/query" + "github.com/linstohu/nexapi/utils" ) type OKXRestClient struct { @@ -103,7 +104,7 @@ func (o *OKXRestClient) GetPassphrase() string { return o.passphrase } -func (o *OKXRestClient) SendHTTPRequest(ctx context.Context, req HTTPRequest) ([]byte, error) { +func (o *OKXRestClient) SendHTTPRequest(ctx context.Context, req utils.HTTPRequest) (*utils.ApiResponse, error) { client := http.Client{} var body io.Reader @@ -148,7 +149,6 @@ func (o *OKXRestClient) SendHTTPRequest(ctx context.Context, req HTTPRequest) ([ if err != nil { return nil, err } - defer resp.Body.Close() if o.debug { dump, err := httputil.DumpResponse(resp, true) @@ -165,7 +165,7 @@ func (o *OKXRestClient) 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 } func (o *OKXRestClient) GenPubHeaders() (map[string]string, error) { @@ -175,7 +175,7 @@ func (o *OKXRestClient) GenPubHeaders() (map[string]string, error) { }, nil } -func (o *OKXRestClient) GenAuthHeaders(req HTTPRequest) (map[string]string, error) { +func (o *OKXRestClient) GenAuthHeaders(req utils.HTTPRequest) (map[string]string, error) { if o.key == "" || o.secret == "" || o.passphrase == "" { return nil, fmt.Errorf("key, secret and passphrase needed when init client") }