mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-07 07:26:48 +00:00
Introduce request package and integrate with exchanges
This commit is contained in:
committed by
Adrian Gallagher
parent
52dfddbb18
commit
7fc9d20fd7
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/request"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
|
||||
@@ -45,11 +47,15 @@ const (
|
||||
huobiMarginAccountBalance = "margin/accounts/balance"
|
||||
huobiWithdrawCreate = "dw/withdraw/api/create"
|
||||
huobiWithdrawCancel = "dw/withdraw-virtual/%s/cancel"
|
||||
|
||||
huobiAuthRate = 0
|
||||
huobiUnauthRate = 0
|
||||
)
|
||||
|
||||
// HUOBI is the overarching type across this package
|
||||
type HUOBI struct {
|
||||
exchange.Base
|
||||
*request.Handler
|
||||
}
|
||||
|
||||
// SetDefaults sets default values for the exchange
|
||||
@@ -66,6 +72,8 @@ func (h *HUOBI) SetDefaults() {
|
||||
h.ConfigCurrencyPairFormat.Uppercase = true
|
||||
h.AssetTypes = []string{ticker.Spot}
|
||||
h.SupportsAutoPairUpdating = true
|
||||
h.Handler = new(request.Handler)
|
||||
h.SetRequestHandler(h.Name, huobiAuthRate, huobiUnauthRate, new(http.Client))
|
||||
}
|
||||
|
||||
// Setup sets user configuration
|
||||
@@ -122,8 +130,8 @@ func (h *HUOBI) GetKline(symbol, period, size string) ([]Klines, error) {
|
||||
|
||||
var result response
|
||||
url := fmt.Sprintf("%s/%s", huobiAPIURL, huobiMarketHistoryKline)
|
||||
err := common.SendHTTPGetRequest(common.EncodeURLValues(url, vals), true, h.Verbose, &result)
|
||||
|
||||
err := h.SendHTTPRequest(common.EncodeURLValues(url, vals), &result)
|
||||
if result.ErrorMessage != "" {
|
||||
return nil, errors.New(result.ErrorMessage)
|
||||
}
|
||||
@@ -142,8 +150,8 @@ func (h *HUOBI) GetMarketDetailMerged(symbol string) (DetailMerged, error) {
|
||||
|
||||
var result response
|
||||
url := fmt.Sprintf("%s/%s", huobiAPIURL, huobiMarketDetailMerged)
|
||||
err := common.SendHTTPGetRequest(common.EncodeURLValues(url, vals), true, h.Verbose, &result)
|
||||
|
||||
err := h.SendHTTPRequest(common.EncodeURLValues(url, vals), &result)
|
||||
if result.ErrorMessage != "" {
|
||||
return result.Tick, errors.New(result.ErrorMessage)
|
||||
}
|
||||
@@ -166,8 +174,8 @@ func (h *HUOBI) GetDepth(symbol, depthType string) (Orderbook, error) {
|
||||
|
||||
var result response
|
||||
url := fmt.Sprintf("%s/%s", huobiAPIURL, huobiMarketDepth)
|
||||
err := common.SendHTTPGetRequest(common.EncodeURLValues(url, vals), true, h.Verbose, &result)
|
||||
|
||||
err := h.SendHTTPRequest(common.EncodeURLValues(url, vals), &result)
|
||||
if result.ErrorMessage != "" {
|
||||
return result.Depth, errors.New(result.ErrorMessage)
|
||||
}
|
||||
@@ -188,8 +196,8 @@ func (h *HUOBI) GetTrades(symbol string) ([]Trade, error) {
|
||||
|
||||
var result response
|
||||
url := fmt.Sprintf("%s/%s", huobiAPIURL, huobiMarketTrade)
|
||||
err := common.SendHTTPGetRequest(common.EncodeURLValues(url, vals), true, h.Verbose, &result)
|
||||
|
||||
err := h.SendHTTPRequest(common.EncodeURLValues(url, vals), &result)
|
||||
if result.ErrorMessage != "" {
|
||||
return nil, errors.New(result.ErrorMessage)
|
||||
}
|
||||
@@ -212,8 +220,8 @@ func (h *HUOBI) GetTradeHistory(symbol, size string) ([]TradeHistory, error) {
|
||||
|
||||
var result response
|
||||
url := fmt.Sprintf("%s/%s", huobiAPIURL, huobiMarketTradeHistory)
|
||||
err := common.SendHTTPGetRequest(common.EncodeURLValues(url, vals), true, h.Verbose, &result)
|
||||
|
||||
err := h.SendHTTPRequest(common.EncodeURLValues(url, vals), &result)
|
||||
if result.ErrorMessage != "" {
|
||||
return nil, errors.New(result.ErrorMessage)
|
||||
}
|
||||
@@ -232,8 +240,8 @@ func (h *HUOBI) GetMarketDetail(symbol string) (Detail, error) {
|
||||
|
||||
var result response
|
||||
url := fmt.Sprintf("%s/%s", huobiAPIURL, huobiMarketDetail)
|
||||
err := common.SendHTTPGetRequest(common.EncodeURLValues(url, vals), true, h.Verbose, &result)
|
||||
|
||||
err := h.SendHTTPRequest(common.EncodeURLValues(url, vals), &result)
|
||||
if result.ErrorMessage != "" {
|
||||
return result.Tick, errors.New(result.ErrorMessage)
|
||||
}
|
||||
@@ -249,8 +257,8 @@ func (h *HUOBI) GetSymbols() ([]Symbol, error) {
|
||||
|
||||
var result response
|
||||
url := fmt.Sprintf("%s/v%s/%s", huobiAPIURL, huobiAPIVersion, huobiSymbols)
|
||||
err := common.SendHTTPGetRequest(url, true, h.Verbose, &result)
|
||||
|
||||
err := h.SendHTTPRequest(url, &result)
|
||||
if result.ErrorMessage != "" {
|
||||
return nil, errors.New(result.ErrorMessage)
|
||||
}
|
||||
@@ -266,8 +274,8 @@ func (h *HUOBI) GetCurrencies() ([]string, error) {
|
||||
|
||||
var result response
|
||||
url := fmt.Sprintf("%s/v%s/%s", huobiAPIURL, huobiAPIVersion, huobiCurrencies)
|
||||
err := common.SendHTTPGetRequest(url, true, h.Verbose, &result)
|
||||
|
||||
err := h.SendHTTPRequest(url, &result)
|
||||
if result.ErrorMessage != "" {
|
||||
return nil, errors.New(result.ErrorMessage)
|
||||
}
|
||||
@@ -283,8 +291,8 @@ func (h *HUOBI) GetTimestamp() (int64, error) {
|
||||
|
||||
var result response
|
||||
url := fmt.Sprintf("%s/v%s/%s", huobiAPIURL, huobiAPIVersion, huobiTimestamp)
|
||||
err := common.SendHTTPGetRequest(url, true, h.Verbose, &result)
|
||||
|
||||
err := h.SendHTTPRequest(url, &result)
|
||||
if result.ErrorMessage != "" {
|
||||
return 0, errors.New(result.ErrorMessage)
|
||||
}
|
||||
@@ -692,6 +700,11 @@ func (h *HUOBI) CancelWithdraw(withdrawID int64) (int64, error) {
|
||||
return result.WithdrawID, err
|
||||
}
|
||||
|
||||
// SendHTTPRequest sends an unauthenticated HTTP request
|
||||
func (h *HUOBI) SendHTTPRequest(path string, result interface{}) error {
|
||||
return h.SendPayload("GET", path, nil, nil, result, false, h.Verbose)
|
||||
}
|
||||
|
||||
// SendAuthenticatedHTTPRequest sends authenticated requests to the HUOBI API
|
||||
func (h *HUOBI) SendAuthenticatedHTTPRequest(method, endpoint string, values url.Values, result interface{}) error {
|
||||
if !h.AuthenticatedAPISupport {
|
||||
@@ -715,16 +728,6 @@ func (h *HUOBI) SendAuthenticatedHTTPRequest(method, endpoint string, values url
|
||||
|
||||
url := fmt.Sprintf("%s%s", huobiAPIURL, endpoint)
|
||||
url = common.EncodeURLValues(url, values)
|
||||
resp, err := common.SendHTTPRequest(method, url, headers, bytes.NewBufferString(""))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = common.JSONDecode([]byte(resp), &result)
|
||||
if err != nil {
|
||||
return errors.New("unable to JSON Unmarshal response")
|
||||
}
|
||||
|
||||
return nil
|
||||
return h.SendPayload(method, url, headers, bytes.NewBufferString(""), result, true, h.Verbose)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user