mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-29 15:10:37 +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"
|
||||
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/request"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
|
||||
@@ -22,6 +24,7 @@ type Binance struct {
|
||||
// valid string list that a required by the exchange
|
||||
validLimits []string
|
||||
validIntervals []string
|
||||
*request.Handler
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -39,10 +42,13 @@ const (
|
||||
bestPrice = "/api/v3/ticker/bookTicker"
|
||||
|
||||
// Authenticated endpoints
|
||||
|
||||
newOrderTest = "/api/v3/order/test"
|
||||
newOrder = "/api/v3/order"
|
||||
queryOrder = "/api/v3/order"
|
||||
|
||||
// binance authenticated and unauthenticated limit rates
|
||||
binanceAuthRate = 1000
|
||||
binanceUnauthRate = 1000
|
||||
)
|
||||
|
||||
// SetDefaults sets the basic defaults for Binance
|
||||
@@ -59,6 +65,8 @@ func (b *Binance) SetDefaults() {
|
||||
b.AssetTypes = []string{ticker.Spot}
|
||||
b.SupportsAutoPairUpdating = true
|
||||
b.SetValues()
|
||||
b.Handler = new(request.Handler)
|
||||
b.SetRequestHandler(b.Name, binanceAuthRate, binanceUnauthRate, new(http.Client))
|
||||
}
|
||||
|
||||
// Setup takes in the supplied exchange configuration details and sets params
|
||||
@@ -114,7 +122,7 @@ func (b *Binance) GetExchangeInfo() (ExchangeInfo, error) {
|
||||
var resp ExchangeInfo
|
||||
path := apiURL + exchangeInfo
|
||||
|
||||
return resp, common.SendHTTPGetRequest(path, true, b.Verbose, &resp)
|
||||
return resp, b.SendHTTPRequest(path, &resp)
|
||||
}
|
||||
|
||||
// GetOrderBook returns full orderbook information
|
||||
@@ -137,7 +145,7 @@ func (b *Binance) GetOrderBook(symbol string, limit int64) (OrderBook, error) {
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, orderBookDepth, params.Encode())
|
||||
|
||||
if err := common.SendHTTPGetRequest(path, true, b.Verbose, &resp); err != nil {
|
||||
if err := b.SendHTTPRequest(path, &resp); err != nil {
|
||||
return orderbook, err
|
||||
}
|
||||
|
||||
@@ -195,7 +203,7 @@ func (b *Binance) GetRecentTrades(symbol string, limit int64) ([]RecentTrade, er
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, recentTrades, params.Encode())
|
||||
|
||||
return resp, common.SendHTTPGetRequest(path, true, b.Verbose, &resp)
|
||||
return resp, b.SendHTTPRequest(path, &resp)
|
||||
}
|
||||
|
||||
// GetHistoricalTrades returns historical trade activity
|
||||
@@ -220,7 +228,7 @@ func (b *Binance) GetHistoricalTrades(symbol string, limit, fromID int64) ([]His
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, historicalTrades, params.Encode())
|
||||
|
||||
return resp, common.SendHTTPGetRequest(path, true, b.Verbose, &resp)
|
||||
return resp, b.SendHTTPRequest(path, &resp)
|
||||
}
|
||||
|
||||
// GetAggregatedTrades returns aggregated trade activity
|
||||
@@ -243,7 +251,7 @@ func (b *Binance) GetAggregatedTrades(symbol string, limit int64) ([]AggregatedT
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, aggregatedTrades, params.Encode())
|
||||
|
||||
return resp, common.SendHTTPGetRequest(path, true, b.Verbose, &resp)
|
||||
return resp, b.SendHTTPRequest(path, &resp)
|
||||
}
|
||||
|
||||
// GetCandleStickData returns candle stick data
|
||||
@@ -272,7 +280,7 @@ func (b *Binance) GetCandleStickData(symbol, interval string, limit int64) ([]Ca
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, candleStick, params.Encode())
|
||||
|
||||
if err := common.SendHTTPGetRequest(path, true, b.Verbose, &resp); err != nil {
|
||||
if err := b.SendHTTPRequest(path, &resp); err != nil {
|
||||
return kline, err
|
||||
}
|
||||
|
||||
@@ -324,14 +332,14 @@ func (b *Binance) GetPriceChangeStats(symbol string) (PriceChangeStats, error) {
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, priceChange, params.Encode())
|
||||
|
||||
return resp, common.SendHTTPGetRequest(path, true, b.Verbose, &resp)
|
||||
return resp, b.SendHTTPRequest(path, &resp)
|
||||
}
|
||||
|
||||
// GetTickers returns the ticker data for the last 24 hrs
|
||||
func (b *Binance) GetTickers() ([]PriceChangeStats, error) {
|
||||
var resp []PriceChangeStats
|
||||
path := fmt.Sprintf("%s%s", apiURL, priceChange)
|
||||
return resp, common.SendHTTPGetRequest(path, true, b.Verbose, &resp)
|
||||
return resp, b.SendHTTPRequest(path, &resp)
|
||||
}
|
||||
|
||||
// GetLatestSpotPrice returns latest spot price of symbol
|
||||
@@ -349,7 +357,7 @@ func (b *Binance) GetLatestSpotPrice(symbol string) (SymbolPrice, error) {
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, symbolPrice, params.Encode())
|
||||
|
||||
return resp, common.SendHTTPGetRequest(path, true, b.Verbose, &resp)
|
||||
return resp, b.SendHTTPRequest(path, &resp)
|
||||
}
|
||||
|
||||
// GetBestPrice returns the latest best price for symbol
|
||||
@@ -367,7 +375,7 @@ func (b *Binance) GetBestPrice(symbol string) (BestPrice, error) {
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, bestPrice, params.Encode())
|
||||
|
||||
return resp, common.SendHTTPGetRequest(path, true, b.Verbose, &resp)
|
||||
return resp, b.SendHTTPRequest(path, &resp)
|
||||
}
|
||||
|
||||
// NewOrderTest sends a new order
|
||||
@@ -434,7 +442,12 @@ func (b *Binance) QueryOrder(symbol, origClientOrderID string, orderID int64) (Q
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// SendAuthHTTPRequest something
|
||||
// SendHTTPRequest sends an unauthenticated request
|
||||
func (b *Binance) SendHTTPRequest(path string, result interface{}) error {
|
||||
return b.SendPayload("GET", path, nil, nil, result, false, b.Verbose)
|
||||
}
|
||||
|
||||
// SendAuthHTTPRequest sends an authenticated HTTP request
|
||||
func (b *Binance) SendAuthHTTPRequest(method, path string, params url.Values, result interface{}) error {
|
||||
if !b.AuthenticatedAPISupport {
|
||||
return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet, b.Name)
|
||||
@@ -459,19 +472,7 @@ func (b *Binance) SendAuthHTTPRequest(method, path string, params url.Values, re
|
||||
log.Printf("sent path: \n%s\n", path)
|
||||
}
|
||||
|
||||
resp, err := common.SendHTTPRequest(method, path, headers, bytes.NewBufferString(params.Encode()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if b.Verbose {
|
||||
log.Printf("Received raw: \n%s\n", resp)
|
||||
}
|
||||
|
||||
if err = common.JSONDecode([]byte(resp), &result); err != nil {
|
||||
return errors.New("sendAuthenticatedHTTPRequest: Unable to JSON Unmarshal response." + err.Error())
|
||||
}
|
||||
return nil
|
||||
return b.SendPayload(method, path, headers, bytes.NewBufferString(params.Encode()), result, true, b.Verbose)
|
||||
}
|
||||
|
||||
// CheckLimit checks value against a variable list
|
||||
|
||||
Reference in New Issue
Block a user