Introduce request package and integrate with exchanges

This commit is contained in:
Ryan O'Hara-Reid
2018-03-27 14:05:15 +11:00
committed by Adrian Gallagher
parent 52dfddbb18
commit 7fc9d20fd7
52 changed files with 1990 additions and 1544 deletions

View File

@@ -5,12 +5,14 @@ import (
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"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"
)
@@ -39,11 +41,15 @@ const (
orderMove = "moveOrder"
tradableBalances = "returnTradableBalances"
transferBalance = "transferBalance"
hitbtcAuthRate = 0
hitbtcUnauthRate = 0
)
// HitBTC is the overarching type across the hitbtc package
type HitBTC struct {
exchange.Base
*request.Handler
}
// SetDefaults sets default settings for hitbtc
@@ -60,6 +66,8 @@ func (p *HitBTC) SetDefaults() {
p.ConfigCurrencyPairFormat.Uppercase = true
p.AssetTypes = []string{ticker.Spot}
p.SupportsAutoPairUpdating = true
p.Handler = new(request.Handler)
p.SetRequestHandler(p.Name, hitbtcAuthRate, hitbtcUnauthRate, new(http.Client))
}
// Setup sets user exchange configuration settings
@@ -107,12 +115,16 @@ func (p *HitBTC) GetCurrencies(currency string) (map[string]Currencies, error) {
}
resp := Response{}
path := fmt.Sprintf("%s/%s/%s", apiURL, apiV2Currency, currency)
err := common.SendHTTPGetRequest(path, true, p.Verbose, &resp.Data)
ret := make(map[string]Currencies)
err := p.SendHTTPRequest(path, &resp.Data)
if err != nil {
return ret, err
}
for _, id := range resp.Data {
ret[id.ID] = id
}
return ret, err
}
@@ -124,12 +136,16 @@ func (p *HitBTC) GetCurrencies(currency string) (map[string]Currencies, error) {
func (p *HitBTC) GetSymbols(symbol string) ([]string, error) {
resp := []Symbol{}
path := fmt.Sprintf("%s/%s/%s", apiURL, apiV2Symbol, symbol)
err := common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
ret := make([]string, 0, len(resp))
err := p.SendHTTPRequest(path, &resp)
if err != nil {
return ret, err
}
for _, x := range resp {
ret = append(ret, x.ID)
}
return ret, err
}
@@ -138,7 +154,8 @@ func (p *HitBTC) GetSymbols(symbol string) ([]string, error) {
func (p *HitBTC) GetSymbolsDetailed() ([]Symbol, error) {
resp := []Symbol{}
path := fmt.Sprintf("%s/%s", apiURL, apiV2Symbol)
return resp, common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
return resp, p.SendHTTPRequest(path, &resp)
}
// GetTicker returns ticker information
@@ -151,7 +168,7 @@ func (p *HitBTC) GetTicker(symbol string) (map[string]Ticker, error) {
var err error
if symbol == "" {
err = common.SendHTTPGetRequest(path, true, false, &resp1)
err = p.SendHTTPRequest(path, &resp1)
if err != nil {
return nil, err
}
@@ -162,7 +179,7 @@ func (p *HitBTC) GetTicker(symbol string) (map[string]Ticker, error) {
}
}
} else {
err = common.SendHTTPGetRequest(path, true, false, &resp2)
err = p.SendHTTPRequest(path, &resp2)
ret[resp2.Symbol] = resp2
}
@@ -240,7 +257,7 @@ func (p *HitBTC) GetTrades(currencyPair, from, till, limit, offset, by, sort str
resp := []TradeHistory{}
path := fmt.Sprintf("%s/%s/%s?%s", apiURL, apiV2Trades, currencyPair, vals.Encode())
return resp, common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
return resp, p.SendHTTPRequest(path, &resp)
}
// GetOrderbook an order book is an electronic list of buy and sell orders for a
@@ -256,7 +273,7 @@ func (p *HitBTC) GetOrderbook(currencyPair string, limit int) (Orderbook, error)
resp := OrderbookResponse{}
path := fmt.Sprintf("%s/%s/%s?%s", apiURL, apiV2Orderbook, currencyPair, vals.Encode())
err := common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
err := p.SendHTTPRequest(path, &resp)
if err != nil {
return Orderbook{}, err
}
@@ -290,7 +307,7 @@ func (p *HitBTC) GetCandles(currencyPair, limit, period string) ([]ChartData, er
resp := []ChartData{}
path := fmt.Sprintf("%s/%s/%s?%s", apiURL, apiV2Candles, currencyPair, vals.Encode())
return resp, common.SendHTTPGetRequest(path, true, p.Verbose, &resp)
return resp, p.SendHTTPRequest(path, &resp)
}
// Authenticated Market Data
@@ -516,6 +533,11 @@ func (p *HitBTC) TransferBalance(currency, from, to string, amount float64) (boo
return true, nil
}
// SendHTTPRequest sends an unauthenticated HTTP request
func (p *HitBTC) SendHTTPRequest(path string, result interface{}) error {
return p.SendPayload("GET", path, nil, nil, result, false, p.Verbose)
}
// SendAuthenticatedHTTPRequest sends an authenticated http request
func (p *HitBTC) SendAuthenticatedHTTPRequest(method, endpoint string, values url.Values, result interface{}) error {
if !p.AuthenticatedAPISupport {
@@ -526,15 +548,5 @@ func (p *HitBTC) SendAuthenticatedHTTPRequest(method, endpoint string, values ur
path := fmt.Sprintf("%s/%s", apiURL, endpoint)
resp, err := common.SendHTTPRequest(method, path, headers, bytes.NewBufferString(values.Encode()))
if err != nil {
return err
}
err = common.JSONDecode([]byte(resp), &result)
if err != nil {
return err
}
return nil
return p.SendPayload(method, path, headers, bytes.NewBufferString(values.Encode()), result, true, p.Verbose)
}