Skip to content

Commit

Permalink
[http client] 接口协议变更
Browse files Browse the repository at this point in the history
  • Loading branch information
nntaoli committed Oct 31, 2022
1 parent 1493cc9 commit 4dda40a
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 31 deletions.
1 change: 0 additions & 1 deletion adaper.go

This file was deleted.

2 changes: 1 addition & 1 deletion api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type IHttpClient interface {
DoRequest(method, rqUrl string, params *url.Values, headers map[string]string) (data []byte, err error)
DoRequest(method, rqUrl string, reqBody string, headers map[string]string) (data []byte, err error)
}

// IMarketRest 行情接口,不需要授权
Expand Down
21 changes: 19 additions & 2 deletions binance/spot/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
. "github.com/nntaoli-project/goex/v2"
"net/http"
"net/url"
)

Expand All @@ -17,7 +18,6 @@ func (s *spotImpl) GetDepth(pair CurrencyPair, limit int, opt ...OptionParameter
}

func (s *spotImpl) GetTicker(pair CurrencyPair, opt ...OptionParameter) (*Ticker, error) {
cli := GetHttpCli()
params := url.Values{}
params.Set("symbol", pair.Symbol)

Expand All @@ -30,7 +30,7 @@ func (s *spotImpl) GetTicker(pair CurrencyPair, opt ...OptionParameter) (*Ticker
}
}

data, err := cli.DoRequest("GET", s.uriOpts.Endpoint+s.uriOpts.TickerUri, &params, nil)
data, err := s.doNoAuthRequest(http.MethodGet, fmt.Sprintf("%s%s", s.uriOpts.Endpoint, s.uriOpts.TickerUri), &params, nil)
if err != nil {
return nil, fmt.Errorf("%w%s", err, errors.New(string(data)))
}
Expand All @@ -51,3 +51,20 @@ func (s *spotImpl) GetKline(pair CurrencyPair, period KlinePeriod, opt ...Option

panic("implement me")
}

func (s *spotImpl) doNoAuthRequest(method, reqUrl string, params *url.Values, headers map[string]string) ([]byte, error) {
var reqBody string

if method == http.MethodGet {
reqUrl += "?" + params.Encode()
} else {
reqBody = params.Encode()
}

responseData, err := GetHttpCli().DoRequest(method, reqUrl, reqBody, headers)
if err != nil {
return nil, fmt.Errorf("%w%s", err, errors.New(string(responseData)))
}

return responseData, err
}
40 changes: 31 additions & 9 deletions huobi/spot/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@ import (
"errors"
"fmt"
. "github.com/nntaoli-project/goex/v2"
"net/http"
"net/url"
)

func (s spotImpl) GetName() string {
func (s *spotImpl) GetName() string {
return "huobi.com"
}

func (s spotImpl) GetDepth(pair CurrencyPair, limit int, opt ...OptionParameter) (*Depth, error) {
func (s *spotImpl) GetDepth(pair CurrencyPair, limit int, opt ...OptionParameter) (*Depth, error) {
//TODO implement me
panic("implement me")
}

func (s spotImpl) GetTicker(pair CurrencyPair, opt ...OptionParameter) (*Ticker, error) {
cli := GetHttpCli()
params := url.Values{}
params.Set("symbol", pair.Symbol)

data, err := cli.DoRequest("GET", s.uriOpts.Endpoint+s.uriOpts.TickerUri, &params, nil)
func (s *spotImpl) GetTicker(pair CurrencyPair, opt ...OptionParameter) (*Ticker, error) {
data, err := s.doNoAuthRequest(http.MethodGet,
fmt.Sprintf("%s%s?symbol=%s", s.uriOpts.Endpoint, s.uriOpts.TickerUri, pair.Symbol), nil, nil)
if err != nil {
return nil, fmt.Errorf("%w%s", err, errors.New(string(data)))
}
Expand All @@ -37,7 +35,31 @@ func (s spotImpl) GetTicker(pair CurrencyPair, opt ...OptionParameter) (*Ticker,
return tk, err
}

func (s spotImpl) GetKline(pair CurrencyPair, period KlinePeriod, opt ...OptionParameter) ([]Kline, error) {
func (s *spotImpl) GetKline(pair CurrencyPair, period KlinePeriod, opt ...OptionParameter) ([]Kline, error) {
//TODO implement me
panic("implement me")
}

func (s *spotImpl) doNoAuthRequest(method, reqUrl string, params *url.Values, headers map[string]string) ([]byte, error) {
if method == http.MethodGet && params != nil {
reqUrl += "?" + params.Encode()
}

responseData, err := GetHttpCli().DoRequest(method, reqUrl, "", headers)
if err != nil {
return nil, fmt.Errorf("%w%s", err, errors.New(string(responseData)))
}

var resp BaseResponse

err = s.unmarshalerOpts.ResponseUnmarshaler(responseData, &resp)
if err != nil {
return nil, err
}

if resp.Status != "ok" {
return nil, errors.New(string(responseData))
}

return responseData, nil
}
13 changes: 9 additions & 4 deletions huobi/spot/spot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ var (
currencyPaircacheMap = make(map[string]*CurrencyPair, 6)
)

type BaseResponse struct {
Status string `json:"status"`
ErrCode int `json:"err_code"`
ErrMsg string `json:"err_msg"`
}

type Spot struct {
unmarshalerOpts UnmarshalerOptions
}
Expand All @@ -18,12 +24,11 @@ type spotImpl struct {
}

func New(opts ...UnmarshalerOption) *Spot {
unmarshaler := new(RespUnmarshaler)
s := &Spot{
unmarshalerOpts: UnmarshalerOptions{
ResponseUnmarshaler: nil,
TickerUnmarshaler: unmarshaler.UnmarshalTicker,
DepthUnmarshaler: unmarshaler.UnmarshalDepth,
ResponseUnmarshaler: UnmarshalResponse,
TickerUnmarshaler: UnmarshalTicker,
DepthUnmarshaler: UnmarshalDepth,
},
}
for _, opt := range opts {
Expand Down
22 changes: 19 additions & 3 deletions huobi/spot/unmarshaler.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package spot

import (
"encoding/json"
"github.com/buger/jsonparser"
. "github.com/nntaoli-project/goex/v2"
"github.com/spf13/cast"
)

type RespUnmarshaler struct {
func UnmarshalResponse(data []byte, i interface{}) error {
return json.Unmarshal(data, i)
}

func (r RespUnmarshaler) UnmarshalDepth(data []byte) (*Depth, error) {
func UnmarshalDepth(data []byte) (*Depth, error) {
//TODO implement me
panic("implement me")
}

func (r RespUnmarshaler) UnmarshalTicker(data []byte) (*Ticker, error) {
func UnmarshalTicker(data []byte) (*Ticker, error) {
var (
tk = new(Ticker)
open float64
Expand All @@ -34,6 +36,20 @@ func (r RespUnmarshaler) UnmarshalTicker(data []byte) (*Ticker, error) {
tk.Vol = cast.ToFloat64(string(value))
case "open":
open = cast.ToFloat64(string(value))
case "bid":
var bids []float64
err := UnmarshalResponse(value, &bids)
if err != nil {
return err
}
tk.Buy = bids[0]
case "ask":
var asks []float64
err := UnmarshalResponse(value, &asks)
if err != nil {
return err
}
tk.Sell = asks[0]
}
return nil
})
Expand Down
11 changes: 1 addition & 10 deletions internal/lib/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,7 @@ func (cli *DefaultHttpClient) init() {
}
}

func (cli *DefaultHttpClient) DoRequest(method, rqUrl string, params *url.Values, headers map[string]string) (data []byte, err error) {
reqBody := ""
if params != nil {
if method == http.MethodGet {
rqUrl = fmt.Sprintf("%s?%s", rqUrl, params.Encode())
} else {
reqBody = params.Encode()
}
}

func (cli *DefaultHttpClient) DoRequest(method, rqUrl string, reqBody string, headers map[string]string) (data []byte, err error) {
logger.Log.Debugf("[http utils] [%s] request url: %s", method, rqUrl)

reqTimeoutCtx, _ := context.WithTimeout(context.TODO(), config.C.HttpConf.Timeout)
Expand Down
7 changes: 6 additions & 1 deletion okx/spot/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
. "github.com/nntaoli-project/goex/v2"
"net/http"
"net/url"
)

Expand Down Expand Up @@ -44,7 +45,11 @@ func (s *spotImpl) GetKline(pair CurrencyPair, period KlinePeriod, opt ...Option
}

func (s *spotImpl) doNoAuthRequest(method, reqUrl string, params *url.Values, headers map[string]string) ([]byte, error) {
responseData, err := GetHttpCli().DoRequest(method, reqUrl, params, headers)
if method == http.MethodGet {
reqUrl += "?" + params.Encode()
}

responseData, err := GetHttpCli().DoRequest(method, reqUrl, "", headers)
if err != nil {
return nil, fmt.Errorf("%w%s", err, errors.New(string(responseData)))
}
Expand Down

0 comments on commit 4dda40a

Please sign in to comment.