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"
)
@@ -46,6 +48,9 @@ const (
gdaxWithdrawalCrypto = "withdrawals/crypto"
gdaxCoinbaseAccounts = "coinbase-accounts"
gdaxTrailingVolume = "users/self/trailing-volume"
gdaxAuthRate = 0
gdaxUnauthRate = 0
)
var sometin []string
@@ -53,6 +58,7 @@ var sometin []string
// GDAX is the overarching type across the GDAX package
type GDAX struct {
exchange.Base
*request.Handler
}
// SetDefaults sets default values for the exchange
@@ -71,6 +77,8 @@ func (g *GDAX) SetDefaults() {
g.AssetTypes = []string{ticker.Spot}
g.APIUrl = gdaxAPIURL
g.SupportsAutoPairUpdating = true
g.Handler = new(request.Handler)
g.SetRequestHandler(g.Name, gdaxAuthRate, gdaxUnauthRate, new(http.Client))
}
// Setup initialises the exchange parameters with the current configuration
@@ -118,8 +126,7 @@ func (g *GDAX) GetFee(maker bool) float64 {
func (g *GDAX) GetProducts() ([]Product, error) {
products := []Product{}
return products,
common.SendHTTPGetRequest(g.APIUrl+gdaxProducts, true, g.Verbose, &products)
return products, g.SendHTTPRequest(g.APIUrl+gdaxProducts, &products)
}
// GetOrderbook returns orderbook by currency pair and level
@@ -132,7 +139,7 @@ func (g *GDAX) GetOrderbook(symbol string, level int) (interface{}, error) {
path = fmt.Sprintf("%s/%s/%s?level=%s", g.APIUrl+gdaxProducts, symbol, gdaxOrderbook, levelStr)
}
if err := common.SendHTTPGetRequest(path, true, g.Verbose, &orderbook); err != nil {
if err := g.SendHTTPRequest(path, &orderbook); err != nil {
return nil, err
}
@@ -201,7 +208,7 @@ func (g *GDAX) GetTicker(currencyPair string) (Ticker, error) {
path := fmt.Sprintf(
"%s/%s/%s", g.APIUrl+gdaxProducts, currencyPair, gdaxTicker)
return ticker, common.SendHTTPGetRequest(path, true, g.Verbose, &ticker)
return ticker, g.SendHTTPRequest(path, &ticker)
}
// GetTrades listd the latest trades for a product
@@ -211,7 +218,7 @@ func (g *GDAX) GetTrades(currencyPair string) ([]Trade, error) {
path := fmt.Sprintf(
"%s/%s/%s", g.APIUrl+gdaxProducts, currencyPair, gdaxTrades)
return trades, common.SendHTTPGetRequest(path, true, g.Verbose, &trades)
return trades, g.SendHTTPRequest(path, &trades)
}
// GetHistoricRates returns historic rates for a product. Rates are returned in
@@ -237,7 +244,7 @@ func (g *GDAX) GetHistoricRates(currencyPair string, start, end, granularity int
fmt.Sprintf("%s/%s/%s", g.APIUrl+gdaxProducts, currencyPair, gdaxHistory),
values)
if err := common.SendHTTPGetRequest(path, true, g.Verbose, &resp); err != nil {
if err := g.SendHTTPRequest(path, &resp); err != nil {
return history, err
}
@@ -268,7 +275,7 @@ func (g *GDAX) GetStats(currencyPair string) (Stats, error) {
path := fmt.Sprintf(
"%s/%s/%s", g.APIUrl+gdaxProducts, currencyPair, gdaxStats)
return stats, common.SendHTTPGetRequest(path, true, g.Verbose, &stats)
return stats, g.SendHTTPRequest(path, &stats)
}
// GetCurrencies returns a list of supported currency on the exchange
@@ -276,16 +283,14 @@ func (g *GDAX) GetStats(currencyPair string) (Stats, error) {
func (g *GDAX) GetCurrencies() ([]Currency, error) {
currencies := []Currency{}
return currencies,
common.SendHTTPGetRequest(g.APIUrl+gdaxCurrencies, true, g.Verbose, &currencies)
return currencies, g.SendHTTPRequest(g.APIUrl+gdaxCurrencies, &currencies)
}
// GetServerTime returns the API server time
func (g *GDAX) GetServerTime() (ServerTime, error) {
serverTime := ServerTime{}
return serverTime,
common.SendHTTPGetRequest(g.APIUrl+gdaxTime, true, g.Verbose, &serverTime)
return serverTime, g.SendHTTPRequest(g.APIUrl+gdaxTime, &serverTime)
}
// GetAccounts returns a list of trading accounts associated with the APIKEYS
@@ -766,6 +771,11 @@ func (g *GDAX) GetTrailingVolume() ([]Volume, error) {
g.SendAuthenticatedHTTPRequest("GET", gdaxTrailingVolume, nil, &resp)
}
// SendHTTPRequest sends an unauthenticated HTTP request
func (g *GDAX) SendHTTPRequest(path string, result interface{}) error {
return g.SendPayload("GET", path, nil, nil, result, false, g.Verbose)
}
// SendAuthenticatedHTTPRequest sends an authenticated HTTP reque
func (g *GDAX) SendAuthenticatedHTTPRequest(method, path string, params map[string]interface{}, result interface{}) (err error) {
if !g.AuthenticatedAPISupport {
@@ -795,24 +805,5 @@ func (g *GDAX) SendAuthenticatedHTTPRequest(method, path string, params map[stri
headers["CB-ACCESS-PASSPHRASE"] = g.ClientID
headers["Content-Type"] = "application/json"
resp, err := common.SendHTTPRequest(method, g.APIUrl+path, headers, bytes.NewBuffer(payload))
if err != nil {
return err
}
if g.Verbose {
log.Printf("Received raw: \n%s\n", resp)
}
type initialResponse struct {
Message string `json:"message"`
}
initialCheck := initialResponse{}
err = common.JSONDecode([]byte(resp), &initialCheck)
if err == nil && len(initialCheck.Message) != 0 {
return errors.New(initialCheck.Message)
}
return common.JSONDecode([]byte(resp), &result)
return g.SendPayload(method, g.APIUrl+path, headers, bytes.NewBuffer(payload), result, true, g.Verbose)
}