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"
"strconv"
"time"
"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"
)
@@ -27,11 +29,16 @@ const (
anxCreateAddress = "receive/create"
anxTicker = "money/ticker"
anxDepth = "money/depth/full"
// ANX rate limites for authenticated and unauthenticated requests
anxAuthRate = 1000
anxUnauthRate = 1000
)
// ANX is the overarching type across the alphapoint package
type ANX struct {
exchange.Base
*request.Handler
}
// SetDefaults sets current default settings
@@ -51,6 +58,8 @@ func (a *ANX) SetDefaults() {
a.ConfigCurrencyPairFormat.Index = "BTC"
a.AssetTypes = []string{ticker.Spot}
a.SupportsAutoPairUpdating = false
a.Handler = new(request.Handler)
a.SetRequestHandler(a.Name, anxAuthRate, anxUnauthRate, new(http.Client))
}
//Setup is run on startup to setup exchange with config values
@@ -95,7 +104,7 @@ func (a *ANX) GetTicker(currency string) (Ticker, error) {
var ticker Ticker
path := fmt.Sprintf("%sapi/2/%s/%s", anxAPIURL, currency, anxTicker)
return ticker, common.SendHTTPGetRequest(path, true, a.Verbose, &ticker)
return ticker, a.SendHTTPRequest(path, &ticker)
}
// GetDepth returns current orderbook depth.
@@ -103,7 +112,7 @@ func (a *ANX) GetDepth(currency string) (Depth, error) {
var depth Depth
path := fmt.Sprintf("%sapi/2/%s/%s", anxAPIURL, currency, anxDepth)
return depth, common.SendHTTPGetRequest(path, true, a.Verbose, &depth)
return depth, a.SendHTTPRequest(path, &depth)
}
// GetAPIKey returns a new generated API key set.
@@ -324,6 +333,11 @@ func (a *ANX) GetDepositAddress(currency, name string, new bool) (string, error)
return response.Address, nil
}
// SendHTTPRequest sends an unauthenticated HTTP request
func (a *ANX) SendHTTPRequest(path string, result interface{}) error {
return a.SendPayload("GET", path, nil, nil, result, false, a.Verbose)
}
// SendAuthenticatedHTTPRequest sends a authenticated HTTP request
func (a *ANX) SendAuthenticatedHTTPRequest(path string, params map[string]interface{}, result interface{}) error {
if !a.AuthenticatedAPISupport {
@@ -362,20 +376,5 @@ func (a *ANX) SendAuthenticatedHTTPRequest(path string, params map[string]interf
headers["Rest-Sign"] = common.Base64Encode([]byte(hmac))
headers["Content-Type"] = "application/json"
resp, err := common.SendHTTPRequest("POST", anxAPIURL+path, headers, bytes.NewBuffer(PayloadJSON))
if err != nil {
return err
}
if a.Verbose {
log.Printf("Received raw: \n%s\n", resp)
}
err = common.JSONDecode([]byte(resp), &result)
if err != nil {
return errors.New("unable to JSON Unmarshal response")
}
return nil
return a.SendPayload("POST", anxAPIURL+path, headers, bytes.NewBuffer(PayloadJSON), result, true, a.Verbose)
}

View File

@@ -6,92 +6,88 @@ import (
"github.com/thrasher-/gocryptotrader/config"
)
func TestSetDefaults(t *testing.T) {
setDefaults := ANX{}
setDefaults.SetDefaults()
var anx ANX
if setDefaults.Name != "ANX" {
func TestSetDefaults(t *testing.T) {
anx.SetDefaults()
if anx.Name != "ANX" {
t.Error("Test Failed - ANX SetDefaults() incorrect values set")
}
if setDefaults.Enabled != false {
if anx.Enabled != false {
t.Error("Test Failed - ANX SetDefaults() incorrect values set")
}
if setDefaults.TakerFee != 0.6 {
if anx.TakerFee != 0.6 {
t.Error("Test Failed - ANX SetDefaults() incorrect values set")
}
if setDefaults.MakerFee != 0.3 {
if anx.MakerFee != 0.3 {
t.Error("Test Failed - ANX SetDefaults() incorrect values set")
}
if setDefaults.Verbose != false {
if anx.Verbose != false {
t.Error("Test Failed - ANX SetDefaults() incorrect values set")
}
if setDefaults.Websocket != false {
if anx.Websocket != false {
t.Error("Test Failed - ANX SetDefaults() incorrect values set")
}
if setDefaults.RESTPollingDelay != 10 {
if anx.RESTPollingDelay != 10 {
t.Error("Test Failed - ANX SetDefaults() incorrect values set")
}
}
func TestSetup(t *testing.T) {
setup := ANX{}
setup.Name = "ANX"
anxSetupConfig := config.GetConfig()
anxSetupConfig.LoadConfig("../../testdata/configtest.json")
anxConfig, err := anxSetupConfig.GetExchangeConfig("ANX")
if err != nil {
t.Error("Test Failed - ANX Setup() init error")
}
setup.Setup(anxConfig)
anx.Setup(anxConfig)
if setup.Enabled != true {
if anx.Enabled != true {
t.Error("Test Failed - ANX Setup() incorrect values set")
}
if setup.AuthenticatedAPISupport != false {
if anx.AuthenticatedAPISupport != false {
t.Error("Test Failed - ANX Setup() incorrect values set")
}
if len(setup.APIKey) != 0 {
if len(anx.APIKey) != 0 {
t.Error("Test Failed - ANX Setup() incorrect values set")
}
if len(setup.APISecret) != 0 {
if len(anx.APISecret) != 0 {
t.Error("Test Failed - ANX Setup() incorrect values set")
}
if setup.RESTPollingDelay != 10 {
if anx.RESTPollingDelay != 10 {
t.Error("Test Failed - ANX Setup() incorrect values set")
}
if setup.Verbose != false {
if anx.Verbose != false {
t.Error("Test Failed - ANX Setup() incorrect values set")
}
if setup.Websocket != false {
if anx.Websocket != false {
t.Error("Test Failed - ANX Setup() incorrect values set")
}
if len(setup.BaseCurrencies) <= 0 {
if len(anx.BaseCurrencies) <= 0 {
t.Error("Test Failed - ANX Setup() incorrect values set")
}
if len(setup.AvailablePairs) <= 0 {
if len(anx.AvailablePairs) <= 0 {
t.Error("Test Failed - ANX Setup() incorrect values set")
}
if len(setup.EnabledPairs) <= 0 {
if len(anx.EnabledPairs) <= 0 {
t.Error("Test Failed - ANX Setup() incorrect values set")
}
}
func TestGetFee(t *testing.T) {
getFee := ANX{}
makerFeeExpected, takerFeeExpected := 0.3, 0.6
getFee.SetDefaults()
if getFee.GetFee(true) != makerFeeExpected {
if anx.GetFee(true) != makerFeeExpected {
t.Error("Test Failed - ANX GetFee() incorrect return value")
}
if getFee.GetFee(false) != takerFeeExpected {
if anx.GetFee(false) != takerFeeExpected {
t.Error("Test Failed - ANX GetFee() incorrect return value")
}
}
func TestGetTicker(t *testing.T) {
getTicker := ANX{}
ticker, err := getTicker.GetTicker("BTCUSD")
ticker, err := anx.GetTicker("BTCUSD")
if err != nil {
t.Errorf("Test Failed - ANX GetTicker() error: %s", err)
}
@@ -101,8 +97,7 @@ func TestGetTicker(t *testing.T) {
}
func TestGetDepth(t *testing.T) {
a := ANX{}
ticker, err := a.GetDepth("BTCUSD")
ticker, err := anx.GetDepth("BTCUSD")
if err != nil {
t.Errorf("Test Failed - ANX GetDepth() error: %s", err)
}
@@ -112,8 +107,7 @@ func TestGetDepth(t *testing.T) {
}
func TestGetAPIKey(t *testing.T) {
getAPIKey := ANX{}
apiKey, apiSecret, err := getAPIKey.GetAPIKey("userName", "passWord", "", "1337")
apiKey, apiSecret, err := anx.GetAPIKey("userName", "passWord", "", "1337")
if err == nil {
t.Error("Test Failed - ANX GetAPIKey() Incorrect")
}
@@ -124,38 +118,3 @@ func TestGetAPIKey(t *testing.T) {
t.Error("Test Failed - ANX GetAPIKey() Incorrect")
}
}
func TestGetDataToken(t *testing.T) {
// --- FAIL: TestGetDataToken (0.17s)
// anx_test.go:120: Test Failed - ANX GetDataToken() Incorrect
// getDataToken := ANX{}
// _, err := getDataToken.GetDataToken()
// if err != nil {
// t.Error("Test Failed - ANX GetDataToken() Incorrect")
// }
}
func TestNewOrder(t *testing.T) {
}
func TestOrderInfo(t *testing.T) {
}
func TestSend(t *testing.T) {
}
func TestCreateNewSubAccount(t *testing.T) {
}
func TestGetDepositAddress(t *testing.T) {
}
func TestSendAuthenticatedHTTPRequest(t *testing.T) {
}