Coinut: Allow authenticated or unuathenticated requests, uncomment tests

This commit is contained in:
Adrian Gallagher
2018-01-20 14:50:06 +11:00
parent 5ca9db8ad4
commit 523551d4cf
2 changed files with 79 additions and 57 deletions

View File

@@ -88,7 +88,7 @@ func (c *COINUT) GetInstruments() (CoinutInstruments, error) {
var result CoinutInstruments
params := make(map[string]interface{})
params["sec_type"] = "SPOT"
err := c.SendAuthenticatedHTTPRequest(coinutInstruments, params, &result)
err := c.SendHTTPRequest(coinutInstruments, params, false, &result)
if err != nil {
return result, err
}
@@ -99,7 +99,7 @@ func (c *COINUT) GetInstrumentTicker(instrumentID int) (CoinutTicker, error) {
var result CoinutTicker
params := make(map[string]interface{})
params["inst_id"] = instrumentID
err := c.SendAuthenticatedHTTPRequest(coinutTicker, params, &result)
err := c.SendHTTPRequest(coinutTicker, params, false, &result)
if err != nil {
return result, err
}
@@ -113,7 +113,7 @@ func (c *COINUT) GetInstrumentOrderbook(instrumentID, limit int) (CoinutOrderboo
if limit > 0 {
params["top_n"] = limit
}
err := c.SendAuthenticatedHTTPRequest(coinutOrderbook, params, &result)
err := c.SendHTTPRequest(coinutOrderbook, params, false, &result)
if err != nil {
return result, err
}
@@ -124,7 +124,7 @@ func (c *COINUT) GetTrades(instrumentID int) (CoinutTrades, error) {
var result CoinutTrades
params := make(map[string]interface{})
params["inst_id"] = instrumentID
err := c.SendAuthenticatedHTTPRequest(coinutTrades, params, &result)
err := c.SendHTTPRequest(coinutTrades, params, false, &result)
if err != nil {
return result, err
}
@@ -133,7 +133,7 @@ func (c *COINUT) GetTrades(instrumentID int) (CoinutTrades, error) {
func (c *COINUT) GetUserBalance() (CoinutUserBalance, error) {
result := CoinutUserBalance{}
err := c.SendAuthenticatedHTTPRequest(coinutBalance, nil, &result)
err := c.SendHTTPRequest(coinutBalance, nil, true, &result)
if err != nil {
return result, err
}
@@ -152,7 +152,7 @@ func (c *COINUT) NewOrder(instrumentID int, quantity, price float64, buy bool, o
}
params["client_ord_id"] = orderID
err := c.SendAuthenticatedHTTPRequest(coinutOrder, params, &result)
err := c.SendHTTPRequest(coinutOrder, params, true, &result)
if err != nil {
return result, err
}
@@ -163,7 +163,7 @@ func (c *COINUT) NewOrders(orders []CoinutOrder) ([]CoinutOrdersBase, error) {
var result CoinutOrdersResponse
params := make(map[string]interface{})
params["orders"] = orders
err := c.SendAuthenticatedHTTPRequest(coinutOrders, params, &result.Data)
err := c.SendHTTPRequest(coinutOrders, params, true, &result.Data)
if err != nil {
return nil, err
}
@@ -174,7 +174,7 @@ func (c *COINUT) GetOpenOrders(instrumentID int) ([]CoinutOrdersResponse, error)
var result []CoinutOrdersResponse
params := make(map[string]interface{})
params["inst_id"] = instrumentID
err := c.SendAuthenticatedHTTPRequest(coinutOrdersOpen, params, &result)
err := c.SendHTTPRequest(coinutOrdersOpen, params, true, &result)
if err != nil {
return nil, err
}
@@ -186,7 +186,7 @@ func (c *COINUT) CancelOrder(instrumentID, orderID int) (bool, error) {
params := make(map[string]interface{})
params["inst_id"] = instrumentID
params["order_id"] = orderID
err := c.SendAuthenticatedHTTPRequest(coinutOrdersCancel, params, &result)
err := c.SendHTTPRequest(coinutOrdersCancel, params, true, &result)
if err != nil {
return false, err
}
@@ -197,7 +197,7 @@ func (c *COINUT) CancelOrders(orders []CoinutCancelOrders) (CoinutCancelOrdersRe
var result CoinutCancelOrdersResponse
params := make(map[string]interface{})
params["entries"] = orders
err := c.SendAuthenticatedHTTPRequest(coinutOrdersCancel, params, &result)
err := c.SendHTTPRequest(coinutOrdersCancel, params, true, &result)
if err != nil {
return result, err
}
@@ -214,7 +214,7 @@ func (c *COINUT) GetTradeHistory(instrumentID, start, limit int) (CoinutTradeHis
if limit >= 0 && start <= 100 {
params["limit"] = limit
}
err := c.SendAuthenticatedHTTPRequest(coinutTradeHistory, params, &result)
err := c.SendHTTPRequest(coinutTradeHistory, params, true, &result)
if err != nil {
return result, err
}
@@ -225,7 +225,7 @@ func (c *COINUT) GetIndexTicker(asset string) (CoinutIndexTicker, error) {
var result CoinutIndexTicker
params := make(map[string]interface{})
params["asset"] = asset
err := c.SendAuthenticatedHTTPRequest(coinutIndexTicker, params, &result)
err := c.SendHTTPRequest(coinutIndexTicker, params, false, &result)
if err != nil {
return result, err
}
@@ -236,7 +236,7 @@ func (c *COINUT) GetDerivativeInstruments(secType string) (interface{}, error) {
var result interface{} //to-do
params := make(map[string]interface{})
params["sec_type"] = secType
err := c.SendAuthenticatedHTTPRequest(coinutInstruments, params, &result)
err := c.SendHTTPRequest(coinutInstruments, params, false, &result)
if err != nil {
return result, err
}
@@ -248,7 +248,7 @@ func (c *COINUT) GetOptionChain(asset, secType string, expiry int64) (CoinutOpti
params := make(map[string]interface{})
params["asset"] = asset
params["sec_type"] = secType
err := c.SendAuthenticatedHTTPRequest(coinutOptionChain, params, &result)
err := c.SendHTTPRequest(coinutOptionChain, params, false, &result)
if err != nil {
return result, err
}
@@ -265,14 +265,14 @@ func (c *COINUT) GetPositionHistory(secType string, start, limit int) (CoinutPos
if limit >= 0 {
params["limit"] = limit
}
err := c.SendAuthenticatedHTTPRequest(coinutPositionHistory, params, &result)
err := c.SendHTTPRequest(coinutPositionHistory, params, true, &result)
if err != nil {
return result, err
}
return result, nil
}
func (c *COINUT) GetOpenPosition(instrumentID int) ([]CoinutOpenPosition, error) {
func (c *COINUT) GetOpenPositions(instrumentID int) ([]CoinutOpenPosition, error) {
type Response struct {
Positions []CoinutOpenPosition `json:"positions"`
}
@@ -280,7 +280,7 @@ func (c *COINUT) GetOpenPosition(instrumentID int) ([]CoinutOpenPosition, error)
params := make(map[string]interface{})
params["inst_id"] = instrumentID
err := c.SendAuthenticatedHTTPRequest(coinutPositionOpen, params, &result)
err := c.SendHTTPRequest(coinutPositionOpen, params, true, &result)
if err != nil {
return result.Positions, err
}
@@ -289,8 +289,8 @@ func (c *COINUT) GetOpenPosition(instrumentID int) ([]CoinutOpenPosition, error)
//to-do: user position update via websocket
func (c *COINUT) SendAuthenticatedHTTPRequest(apiRequest string, params map[string]interface{}, result interface{}) (err error) {
if !c.AuthenticatedAPISupport {
func (c *COINUT) SendHTTPRequest(apiRequest string, params map[string]interface{}, authenticated bool, result interface{}) (err error) {
if !c.AuthenticatedAPISupport && authenticated {
return fmt.Errorf(exchange.WarningAuthenticatedRequestWithoutCredentialsSet, c.Name)
}
@@ -308,19 +308,20 @@ func (c *COINUT) SendAuthenticatedHTTPRequest(apiRequest string, params map[stri
params["request"] = apiRequest
payload, err = common.JSONEncode(params)
if err != nil {
return errors.New("SendAuthenticatedHTTPRequest: Unable to JSON request")
return errors.New("SenddHTTPRequest: Unable to JSON request")
}
if c.Verbose {
log.Printf("Request JSON: %s\n", payload)
}
hmac := common.GetHMAC(common.HashSHA256, []byte(payload), []byte(c.APIKey))
headers := make(map[string]string)
headers["X-USER"] = c.ClientID
headers["X-SIGNATURE"] = common.HexEncodeToString(hmac)
if authenticated {
headers["X-USER"] = c.ClientID
hmac := common.GetHMAC(common.HashSHA256, []byte(payload), []byte(c.APIKey))
headers["X-SIGNATURE"] = common.HexEncodeToString(hmac)
}
headers["Content-Type"] = "application/json"
resp, err := common.SendHTTPRequest("POST", coinutAPIURL, headers, bytes.NewBuffer(payload))
@@ -333,9 +334,7 @@ func (c *COINUT) SendAuthenticatedHTTPRequest(apiRequest string, params map[stri
}
genResp := CoinutGenericResponse{}
err = common.JSONDecode([]byte(resp), &genResp)
if err != nil {
return errors.New("unable to JSON Unmarshal generic response")
}
@@ -345,7 +344,6 @@ func (c *COINUT) SendAuthenticatedHTTPRequest(apiRequest string, params map[stri
}
err = common.JSONDecode([]byte(resp), &result)
if err != nil {
return errors.New("unable to JSON Unmarshal response")
}

View File

@@ -1,32 +1,56 @@
package coinut
//
// const (
// apiKey = ""
// apiSecret = ""
// )
//
// var c COINUT
//
// func TestSetDefaults(t *testing.T) {
// c.SetDefaults()
// }
//
// func TestSetup(t *testing.T) {
// exch := config.ExchangeConfig{}
// c.Setup(exch)
//
// exch.Enabled = true
// exch.APIKey = apiKey
// exch.APISecret = apiSecret
// c.Setup(exch)
// }
//
// // func TestGetInstruments(t *testing.T) {
// // c.Verbose = true
// // resp, err := c.GetInstruments()
// // if err == nil {
// // t.Error("Test failed - GetInstruments() error", err)
// // }
// // log.Println(resp)
// // }
import (
"testing"
"time"
"github.com/thrasher-/gocryptotrader/config"
)
var c COINUT
// Please supply your own keys here to do better tests
const (
apiKey = ""
apiSecret = ""
)
func TestSetDefaults(t *testing.T) {
c.SetDefaults()
}
func TestSetup(t *testing.T) {
t.Parallel()
c := COINUT{}
c.Name = "Coinut"
cfg := config.GetConfig()
cfg.LoadConfig("../../testdata/configtest.json")
bConfig, err := cfg.GetExchangeConfig("COINUT")
if err != nil {
t.Error("Test Failed - Coinut Setup() init error")
}
c.SetDefaults()
c.Setup(bConfig)
if !c.IsEnabled() || c.AuthenticatedAPISupport || c.RESTPollingDelay != time.Duration(10) ||
c.Verbose || c.Websocket || len(c.BaseCurrencies) < 1 ||
len(c.AvailablePairs) < 1 || len(c.EnabledPairs) < 1 {
t.Error("Test Failed - Coinut Setup values not set correctly")
}
bConfig.Enabled = false
c.Setup(bConfig)
if c.IsEnabled() {
t.Error("Test failed - Coinut TestSetup incorrect value")
}
}
func TestGetInstruments(t *testing.T) {
c.Verbose = true
_, err := c.GetInstruments()
if err != nil {
t.Error("Test failed - GetInstruments() error", err)
}
}