mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 07:26:53 +00:00
Introduce request package and integrate with exchanges
This commit is contained in:
committed by
Adrian Gallagher
parent
52dfddbb18
commit
7fc9d20fd7
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -29,11 +31,15 @@ const (
|
||||
lakeBTCGetTrades = "getTrades"
|
||||
lakeBTCGetExternalAccounts = "getExternalAccounts"
|
||||
lakeBTCCreateWithdraw = "createWithdraw"
|
||||
|
||||
lakeBTCAuthRate = 0
|
||||
lakeBTCUnauth = 0
|
||||
)
|
||||
|
||||
// LakeBTC is the overarching type across the LakeBTC package
|
||||
type LakeBTC struct {
|
||||
exchange.Base
|
||||
*request.Handler
|
||||
}
|
||||
|
||||
// SetDefaults sets LakeBTC defaults
|
||||
@@ -51,6 +57,8 @@ func (l *LakeBTC) SetDefaults() {
|
||||
l.ConfigCurrencyPairFormat.Uppercase = true
|
||||
l.AssetTypes = []string{ticker.Spot}
|
||||
l.SupportsAutoPairUpdating = false
|
||||
l.Handler = new(request.Handler)
|
||||
l.SetRequestHandler(l.Name, lakeBTCAuthRate, lakeBTCUnauth, new(http.Client))
|
||||
}
|
||||
|
||||
// Setup sets exchange configuration profile
|
||||
@@ -95,7 +103,7 @@ func (l *LakeBTC) GetTicker() (map[string]Ticker, error) {
|
||||
response := make(map[string]TickerResponse)
|
||||
path := fmt.Sprintf("%s/%s", lakeBTCAPIURL, lakeBTCTicker)
|
||||
|
||||
if err := common.SendHTTPGetRequest(path, true, l.Verbose, &response); err != nil {
|
||||
if err := l.SendHTTPRequest(path, &response); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -137,7 +145,7 @@ func (l *LakeBTC) GetOrderBook(currency string) (Orderbook, error) {
|
||||
}
|
||||
path := fmt.Sprintf("%s/%s?symbol=%s", lakeBTCAPIURL, lakeBTCOrderbook, common.StringToLower(currency))
|
||||
resp := Response{}
|
||||
err := common.SendHTTPGetRequest(path, true, l.Verbose, &resp)
|
||||
err := l.SendHTTPRequest(path, &resp)
|
||||
if err != nil {
|
||||
return Orderbook{}, err
|
||||
}
|
||||
@@ -178,7 +186,7 @@ func (l *LakeBTC) GetTradeHistory(currency string) ([]TradeHistory, error) {
|
||||
path := fmt.Sprintf("%s/%s?symbol=%s", lakeBTCAPIURL, lakeBTCTrades, common.StringToLower(currency))
|
||||
resp := []TradeHistory{}
|
||||
|
||||
return resp, common.SendHTTPGetRequest(path, true, l.Verbose, &resp)
|
||||
return resp, l.SendHTTPRequest(path, &resp)
|
||||
}
|
||||
|
||||
// GetAccountInfo returns your current account information
|
||||
@@ -276,6 +284,11 @@ func (l *LakeBTC) CreateWithdraw(amount float64, accountID int64) (Withdraw, err
|
||||
return resp, l.SendAuthenticatedHTTPRequest(lakeBTCCreateWithdraw, params, &resp)
|
||||
}
|
||||
|
||||
// SendHTTPRequest sends an unauthenticated http request
|
||||
func (l *LakeBTC) SendHTTPRequest(path string, result interface{}) error {
|
||||
return l.SendPayload("GET", path, nil, nil, result, false, l.Verbose)
|
||||
}
|
||||
|
||||
// SendAuthenticatedHTTPRequest sends an autheticated HTTP request to a LakeBTC
|
||||
func (l *LakeBTC) SendAuthenticatedHTTPRequest(method, params string, result interface{}) (err error) {
|
||||
if !l.AuthenticatedAPISupport {
|
||||
@@ -310,32 +323,5 @@ func (l *LakeBTC) SendAuthenticatedHTTPRequest(method, params string, result int
|
||||
headers["Authorization"] = "Basic " + common.Base64Encode([]byte(l.APIKey+":"+common.HexEncodeToString(hmac)))
|
||||
headers["Content-Type"] = "application/json-rpc"
|
||||
|
||||
resp, err := common.SendHTTPRequest("POST", lakeBTCAPIURL, headers, strings.NewReader(string(data)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if l.Verbose {
|
||||
log.Printf("Received raw: %s\n", resp)
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
errResponse := ErrorResponse{}
|
||||
err = common.JSONDecode([]byte(resp), &errResponse)
|
||||
if err != nil {
|
||||
return errors.New("unable to check response for error")
|
||||
}
|
||||
|
||||
if errResponse.Error != "" {
|
||||
return errors.New(errResponse.Error)
|
||||
}
|
||||
|
||||
err = common.JSONDecode([]byte(resp), &result)
|
||||
if err != nil {
|
||||
return errors.New("unable to JSON Unmarshal response")
|
||||
}
|
||||
return nil
|
||||
return l.SendPayload("POST", lakeBTCAPIURL, headers, strings.NewReader(string(data)), result, true, l.Verbose)
|
||||
}
|
||||
|
||||
@@ -69,6 +69,9 @@ func TestGetTradeHistory(t *testing.T) {
|
||||
|
||||
func TestTrade(t *testing.T) {
|
||||
t.Parallel()
|
||||
if l.APIKey == "" || l.APISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
_, err := l.Trade(0, 0, 0, "USD")
|
||||
if err == nil {
|
||||
t.Error("Test Failed - Trade() error", err)
|
||||
@@ -77,6 +80,9 @@ func TestTrade(t *testing.T) {
|
||||
|
||||
func TestGetOpenOrders(t *testing.T) {
|
||||
t.Parallel()
|
||||
if l.APIKey == "" || l.APISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
_, err := l.GetOpenOrders()
|
||||
if err == nil {
|
||||
t.Error("Test Failed - GetOpenOrders() error", err)
|
||||
@@ -85,6 +91,9 @@ func TestGetOpenOrders(t *testing.T) {
|
||||
|
||||
func TestGetOrders(t *testing.T) {
|
||||
t.Parallel()
|
||||
if l.APIKey == "" || l.APISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
_, err := l.GetOrders([]int64{1, 2})
|
||||
if err == nil {
|
||||
t.Error("Test Failed - GetOrders() error", err)
|
||||
@@ -93,6 +102,9 @@ func TestGetOrders(t *testing.T) {
|
||||
|
||||
func TestCancelOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
if l.APIKey == "" || l.APISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
err := l.CancelOrder(1337)
|
||||
if err == nil {
|
||||
t.Error("Test Failed - CancelOrder() error", err)
|
||||
@@ -101,6 +113,9 @@ func TestCancelOrder(t *testing.T) {
|
||||
|
||||
func TestGetTrades(t *testing.T) {
|
||||
t.Parallel()
|
||||
if l.APIKey == "" || l.APISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
_, err := l.GetTrades(1337)
|
||||
if err == nil {
|
||||
t.Error("Test Failed - GetTrades() error", err)
|
||||
@@ -109,6 +124,9 @@ func TestGetTrades(t *testing.T) {
|
||||
|
||||
func TestGetExternalAccounts(t *testing.T) {
|
||||
t.Parallel()
|
||||
if l.APIKey == "" || l.APISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
_, err := l.GetExternalAccounts()
|
||||
if err == nil {
|
||||
t.Error("Test Failed - GetExternalAccounts() error", err)
|
||||
@@ -117,6 +135,9 @@ func TestGetExternalAccounts(t *testing.T) {
|
||||
|
||||
func TestCreateWithdraw(t *testing.T) {
|
||||
t.Parallel()
|
||||
if l.APIKey == "" || l.APISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
_, err := l.CreateWithdraw(0, 1337)
|
||||
if err == nil {
|
||||
t.Error("Test Failed - CreateWithdraw() error", err)
|
||||
|
||||
Reference in New Issue
Block a user