From 80a66fe4bfd9ab4e79af12f9ad16e8598fbdc9d2 Mon Sep 17 00:00:00 2001 From: linstohu Date: Wed, 3 Jan 2024 11:53:38 +0800 Subject: [PATCH] fix: http headers --- binance/coinmfutures/utils/client.go | 8 ------ binance/europeanoptions/account/client.go | 16 +++++------ binance/europeanoptions/marketdata/client.go | 21 +++++++------- binance/europeanoptions/utils/client.go | 30 ++++++++++++++------ binance/portfoliomargin/utils/client.go | 8 ------ binance/usdmfutures/utils/client.go | 8 ------ bitfinex/restauth/client.go | 7 ----- bitfinex/restpub/client.go | 8 ------ htx/usdm/rest/types/account.go | 22 +++++++------- okx/utils/rest.go | 7 ----- 10 files changed, 51 insertions(+), 84 deletions(-) diff --git a/binance/coinmfutures/utils/client.go b/binance/coinmfutures/utils/client.go index d7683c2..b4dd600 100644 --- a/binance/coinmfutures/utils/client.go +++ b/binance/coinmfutures/utils/client.go @@ -18,7 +18,6 @@ package utils import ( - "bytes" "context" "fmt" "io" @@ -189,12 +188,5 @@ func (u *CoinMarginedClient) SendHTTPRequest(ctx context.Context, req utils.HTTP u.logger.Info(fmt.Sprintf("\n%s\n", string(dump))) } - buf := new(bytes.Buffer) - buf.ReadFrom(resp.Body) - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("API returned a non-200 status code: [%d] - [%s]", resp.StatusCode, buf.String()) - } - return utils.NewApiResponse(&req, resp), nil } diff --git a/binance/europeanoptions/account/client.go b/binance/europeanoptions/account/client.go index 87944ee..6e55773 100644 --- a/binance/europeanoptions/account/client.go +++ b/binance/europeanoptions/account/client.go @@ -90,7 +90,7 @@ func (o *OptionsAccountClient) GetAccountInfo(ctx context.Context) (*types.GetAc securityType := usdmutils.TRADE { - headers, err := o.GenHeaders(securityType) + headers, err := o.GenGetHeaders(securityType) if err != nil { return nil, err } @@ -233,7 +233,7 @@ func (o *OptionsAccountClient) GetSingleOrder(ctx context.Context, param types.G securityType := usdmutils.TRADE { - headers, err := o.GenHeaders(securityType) + headers, err := o.GenGetHeaders(securityType) if err != nil { return nil, err } @@ -501,7 +501,7 @@ func (o *OptionsAccountClient) GetOpenOrders(ctx context.Context, param types.Ge securityType := usdmutils.TRADE { - headers, err := o.GenHeaders(securityType) + headers, err := o.GenGetHeaders(securityType) if err != nil { return nil, err } @@ -572,7 +572,7 @@ func (o *OptionsAccountClient) GetOrderHistory(ctx context.Context, param types. securityType := usdmutils.TRADE { - headers, err := o.GenHeaders(securityType) + headers, err := o.GenGetHeaders(securityType) if err != nil { return nil, err } @@ -643,7 +643,7 @@ func (o *OptionsAccountClient) GetPositionInfo(ctx context.Context, param types. securityType := usdmutils.TRADE { - headers, err := o.GenHeaders(securityType) + headers, err := o.GenGetHeaders(securityType) if err != nil { return nil, err } @@ -714,7 +714,7 @@ func (o *OptionsAccountClient) GetTradeList(ctx context.Context, param types.Get securityType := usdmutils.TRADE { - headers, err := o.GenHeaders(securityType) + headers, err := o.GenGetHeaders(securityType) if err != nil { return nil, err } @@ -785,7 +785,7 @@ func (o *OptionsAccountClient) GetExerciseRecord(ctx context.Context, param type securityType := usdmutils.TRADE { - headers, err := o.GenHeaders(securityType) + headers, err := o.GenGetHeaders(securityType) if err != nil { return nil, err } @@ -856,7 +856,7 @@ func (o *OptionsAccountClient) GetFundingFlow(ctx context.Context, param types.G securityType := usdmutils.TRADE { - headers, err := o.GenHeaders(securityType) + headers, err := o.GenGetHeaders(securityType) if err != nil { return nil, err } diff --git a/binance/europeanoptions/marketdata/client.go b/binance/europeanoptions/marketdata/client.go index 9defe2b..58a52ad 100644 --- a/binance/europeanoptions/marketdata/client.go +++ b/binance/europeanoptions/marketdata/client.go @@ -59,7 +59,7 @@ func (o *OptionsMarketDataClient) Ping(ctx context.Context) error { Method: http.MethodGet, } - headers, err := o.GenHeaders(usdmutils.NONE) + headers, err := o.GenGetHeaders(usdmutils.NONE) if err != nil { return err } @@ -81,7 +81,7 @@ func (o *OptionsMarketDataClient) GetServerTime(ctx context.Context) (*spottypes Method: http.MethodGet, } - headers, err := o.GenHeaders(usdmutils.NONE) + headers, err := o.GenGetHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -113,7 +113,7 @@ func (o *OptionsMarketDataClient) GetExchangeInfo(ctx context.Context) (*types.G Method: http.MethodGet, } - headers, err := o.GenHeaders(usdmutils.NONE) + headers, err := o.GenGetHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -125,7 +125,6 @@ func (o *OptionsMarketDataClient) GetExchangeInfo(ctx context.Context) (*types.G } var body types.ExchangeInfo - if err := resp.ReadJsonBody(&body); err != nil { return nil, err } @@ -152,7 +151,7 @@ func (o *OptionsMarketDataClient) GetOrderbook(ctx context.Context, param types. Query: param, } - headers, err := o.GenHeaders(usdmutils.NONE) + headers, err := o.GenGetHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -191,7 +190,7 @@ func (o *OptionsMarketDataClient) GetRecentTradesList(ctx context.Context, param Query: param, } - headers, err := o.GenHeaders(usdmutils.NONE) + headers, err := o.GenGetHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -229,7 +228,7 @@ func (o *OptionsMarketDataClient) GetKlines(ctx context.Context, param usdmtypes Query: param, } - headers, err := o.GenHeaders(usdmutils.NONE) + headers, err := o.GenGetHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -267,7 +266,7 @@ func (o *OptionsMarketDataClient) GetMarkPrice(ctx context.Context, param types. Query: param, } - headers, err := o.GenHeaders(usdmutils.NONE) + headers, err := o.GenGetHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -305,7 +304,7 @@ func (o *OptionsMarketDataClient) GetTickerPrice(ctx context.Context, param type Query: param, } - headers, err := o.GenHeaders(usdmutils.NONE) + headers, err := o.GenGetHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -343,7 +342,7 @@ func (o *OptionsMarketDataClient) GetUnderlyingIndexPrice(ctx context.Context, p Query: param, } - headers, err := o.GenHeaders(usdmutils.NONE) + headers, err := o.GenGetHeaders(usdmutils.NONE) if err != nil { return nil, err } @@ -381,7 +380,7 @@ func (o *OptionsMarketDataClient) GetOpenInterest(ctx context.Context, param typ Query: param, } - headers, err := o.GenHeaders(usdmutils.NONE) + headers, err := o.GenGetHeaders(usdmutils.NONE) if err != nil { return nil, err } diff --git a/binance/europeanoptions/utils/client.go b/binance/europeanoptions/utils/client.go index a9ac951..592f329 100644 --- a/binance/europeanoptions/utils/client.go +++ b/binance/europeanoptions/utils/client.go @@ -18,7 +18,6 @@ package utils import ( - "bytes" "context" "fmt" "io" @@ -123,6 +122,28 @@ func (o *OptionsClient) GenHeaders(t usdmutils.SecurityType) (map[string]string, return headers, nil } +// For GET endpoints, parameters must be sent as a query string without setting content type in the http headers. +// doc: https://binance-docs.github.io/apidocs/voptions/en/#general-api-information +func (o *OptionsClient) GenGetHeaders(t usdmutils.SecurityType) (map[string]string, error) { + headers := map[string]string{ + "Accept": "application/json", + } + + // 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 + switch t { + case usdmutils.TRADE, usdmutils.USER_DATA, usdmutils.USER_STREAM, usdmutils.MARKET_DATA: + key := o.GetKey() + if key == "" { + return nil, fmt.Errorf("a valid API-Key required") + } + + headers["X-MBX-APIKEY"] = o.GetKey() + } + + return headers, nil +} + func (o *OptionsClient) NeedSignature(t usdmutils.SecurityType) bool { switch t { case usdmutils.TRADE, usdmutils.USER_DATA: @@ -188,12 +209,5 @@ func (o *OptionsClient) SendHTTPRequest(ctx context.Context, req utils.HTTPReque o.logger.Info(fmt.Sprintf("\n%s\n", string(dump))) } - buf := new(bytes.Buffer) - buf.ReadFrom(resp.Body) - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("API returned a non-200 status code: [%d] - [%s]", resp.StatusCode, buf.String()) - } - return utils.NewApiResponse(&req, resp), nil } diff --git a/binance/portfoliomargin/utils/client.go b/binance/portfoliomargin/utils/client.go index a2fd3ed..cef904f 100644 --- a/binance/portfoliomargin/utils/client.go +++ b/binance/portfoliomargin/utils/client.go @@ -18,7 +18,6 @@ package utils import ( - "bytes" "context" "fmt" "io" @@ -187,12 +186,5 @@ func (p *PortfolioMarginClient) SendHTTPRequest(ctx context.Context, req utils.H p.logger.Info(fmt.Sprintf("\n%s\n", string(dump))) } - buf := new(bytes.Buffer) - buf.ReadFrom(resp.Body) - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("API returned a non-200 status code: [%d] - [%s]", resp.StatusCode, buf.String()) - } - return utils.NewApiResponse(&req, resp), nil } diff --git a/binance/usdmfutures/utils/client.go b/binance/usdmfutures/utils/client.go index e9cee5e..bd80adf 100644 --- a/binance/usdmfutures/utils/client.go +++ b/binance/usdmfutures/utils/client.go @@ -18,7 +18,6 @@ package utils import ( - "bytes" "context" "fmt" "io" @@ -187,12 +186,5 @@ func (u *USDMarginedClient) SendHTTPRequest(ctx context.Context, req utils.HTTPR u.logger.Info(fmt.Sprintf("\n%s\n", string(dump))) } - buf := new(bytes.Buffer) - buf.ReadFrom(resp.Body) - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("API returned a non-200 status code: [%d] - [%s]", resp.StatusCode, buf.String()) - } - return utils.NewApiResponse(&req, resp), nil } diff --git a/bitfinex/restauth/client.go b/bitfinex/restauth/client.go index c5c22c1..1046862 100644 --- a/bitfinex/restauth/client.go +++ b/bitfinex/restauth/client.go @@ -183,12 +183,5 @@ func (b *BitfinexAuthClient) SendHTTPRequest(ctx context.Context, req utils.HTTP b.logger.Info(fmt.Sprintf("\n%s\n", string(dump))) } - buf := new(bytes.Buffer) - buf.ReadFrom(resp.Body) - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("API returned a non-200 status code: [%d] - [%s]", resp.StatusCode, buf.String()) - } - return utils.NewApiResponse(&req, resp), nil } diff --git a/bitfinex/restpub/client.go b/bitfinex/restpub/client.go index 40f82ac..3a9c866 100644 --- a/bitfinex/restpub/client.go +++ b/bitfinex/restpub/client.go @@ -18,7 +18,6 @@ package restpub import ( - "bytes" "context" "fmt" "io" @@ -135,12 +134,5 @@ func (b *BitfinexClient) SendHTTPRequest(ctx context.Context, req utils.HTTPRequ b.logger.Info(fmt.Sprintf("\n%s\n", string(dump))) } - buf := new(bytes.Buffer) - buf.ReadFrom(resp.Body) - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("API returned a non-200 status code: [%d] - [%s]", resp.StatusCode, buf.String()) - } - return utils.NewApiResponse(&req, resp), nil } diff --git a/htx/usdm/rest/types/account.go b/htx/usdm/rest/types/account.go index a0b566b..3da880c 100644 --- a/htx/usdm/rest/types/account.go +++ b/htx/usdm/rest/types/account.go @@ -46,22 +46,22 @@ type GetIsolatedAccountsResp struct { type IsolatedAccount struct { Symbol string `json:"symbol,omitempty"` + ContractCode string `json:"contract_code,omitempty"` + MarginAsset string `json:"margin_asset,omitempty"` MarginBalance float64 `json:"margin_balance,omitempty"` + MarginStatic float64 `json:"margin_static,omitempty"` MarginPosition float64 `json:"margin_position,omitempty"` MarginFrozen float64 `json:"margin_frozen,omitempty"` MarginAvailable float64 `json:"margin_available,omitempty"` - ProfitReal float64 `json:"profit_real,omitempty"` ProfitUnreal float64 `json:"profit_unreal,omitempty"` RiskRate float64 `json:"risk_rate,omitempty"` - NewRiskRate string `json:"new_risk_rate,omitempty"` + NewRiskRate float64 `json:"new_risk_rate,omitempty"` + ProfitReal float64 `json:"profit_real,omitempty"` TradePartition string `json:"trade_partition,omitempty"` - WithdrawAvailable float64 `json:"withdraw_available,omitempty"` LiquidationPrice float64 `json:"liquidation_price,omitempty"` + WithdrawAvailable float64 `json:"withdraw_available,omitempty"` LeverRate float64 `json:"lever_rate,omitempty"` AdjustFactor float64 `json:"adjust_factor,omitempty"` - MarginStatic float64 `json:"margin_static,omitempty"` - ContractCode string `json:"contract_code,omitempty"` - MarginAsset string `json:"margin_asset,omitempty"` MarginMode string `json:"margin_mode,omitempty"` MarginAccount string `json:"margin_account,omitempty"` PositionMode string `json:"position_mode,omitempty"` @@ -88,9 +88,9 @@ type CrossAccount struct { ProfitUnreal float64 `json:"profit_unreal,omitempty"` WithdrawAvailable float64 `json:"withdraw_available,omitempty"` RiskRate float64 `json:"risk_rate,omitempty"` - MoneyIn string `json:"money_in,omitempty"` - MoneyOut string `json:"money_out,omitempty"` - NewRiskRate string `json:"new_risk_rate,omitempty"` + MoneyIn float64 `json:"money_in,omitempty"` + MoneyOut float64 `json:"money_out,omitempty"` + NewRiskRate float64 `json:"new_risk_rate,omitempty"` PositionMode string `json:"position_mode,omitempty"` ContractDetail []ContractDetail `json:"contract_detail,omitempty"` } @@ -106,7 +106,7 @@ type FuturesContractDetail struct { LeverRate float64 `json:"lever_rate,omitempty"` AdjustFactor float64 `json:"adjust_factor,omitempty"` ContractType string `json:"contract_type,omitempty"` - CrossMaxAvailable string `json:"cross_max_available,omitempty"` + CrossMaxAvailable float64 `json:"cross_max_available,omitempty"` TradePartition string `json:"trade_partition,omitempty"` Pair string `json:"pair,omitempty"` BusinessType string `json:"business_type,omitempty"` @@ -123,7 +123,7 @@ type ContractDetail struct { LeverRate float64 `json:"lever_rate,omitempty"` AdjustFactor float64 `json:"adjust_factor,omitempty"` ContractType string `json:"contract_type,omitempty"` - CrossMaxAvailable string `json:"cross_max_available,omitempty"` + CrossMaxAvailable float64 `json:"cross_max_available,omitempty"` TradePartition string `json:"trade_partition,omitempty"` Pair string `json:"pair,omitempty"` BusinessType string `json:"business_type,omitempty"` diff --git a/okx/utils/rest.go b/okx/utils/rest.go index 42456ad..9bf4146 100644 --- a/okx/utils/rest.go +++ b/okx/utils/rest.go @@ -158,13 +158,6 @@ func (o *OKXRestClient) SendHTTPRequest(ctx context.Context, req utils.HTTPReque o.logger.Info(fmt.Sprintf("\n%s\n", string(dump))) } - buf := new(bytes.Buffer) - buf.ReadFrom(resp.Body) - - if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("API returned a non-200 status code: [%d] - [%s]", resp.StatusCode, buf.String()) - } - return utils.NewApiResponse(&req, resp), nil }