Skip to content

Commit

Permalink
fix: http headers
Browse files Browse the repository at this point in the history
  • Loading branch information
linstohu committed Jan 3, 2024
1 parent 741117c commit 80a66fe
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 84 deletions.
8 changes: 0 additions & 8 deletions binance/coinmfutures/utils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package utils

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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
}
16 changes: 8 additions & 8 deletions binance/europeanoptions/account/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 10 additions & 11 deletions binance/europeanoptions/marketdata/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
30 changes: 22 additions & 8 deletions binance/europeanoptions/utils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package utils

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
8 changes: 0 additions & 8 deletions binance/portfoliomargin/utils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package utils

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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
}
8 changes: 0 additions & 8 deletions binance/usdmfutures/utils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package utils

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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
}
7 changes: 0 additions & 7 deletions bitfinex/restauth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 0 additions & 8 deletions bitfinex/restpub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package restpub

import (
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -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
}
22 changes: 11 additions & 11 deletions htx/usdm/rest/types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
}
Expand All @@ -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"`
Expand All @@ -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"`
Expand Down
7 changes: 0 additions & 7 deletions okx/utils/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 80a66fe

Please sign in to comment.