mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-24 15:10:19 +00:00
Introduce request package and integrate with exchanges
This commit is contained in:
committed by
Adrian Gallagher
parent
52dfddbb18
commit
7fc9d20fd7
@@ -1,9 +1,9 @@
|
||||
package exmo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/request"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
)
|
||||
|
||||
@@ -39,15 +40,18 @@ const (
|
||||
exmoExcodeCreate = "excode_create"
|
||||
exmoExcodeLoad = "excode_load"
|
||||
exmoWalletHistory = "wallet_history"
|
||||
|
||||
// Rate limit: 180 per/minute
|
||||
exmoAuthRate = 333
|
||||
exmoUnauthRate = 333
|
||||
)
|
||||
|
||||
// EXMO exchange struct
|
||||
type EXMO struct {
|
||||
exchange.Base
|
||||
*request.Handler
|
||||
}
|
||||
|
||||
// Rate limit: 180 per/minute
|
||||
|
||||
// SetDefaults sets the basic defaults for exmo
|
||||
func (e *EXMO) SetDefaults() {
|
||||
e.Name = "EXMO"
|
||||
@@ -62,6 +66,8 @@ func (e *EXMO) SetDefaults() {
|
||||
e.ConfigCurrencyPairFormat.Uppercase = true
|
||||
e.AssetTypes = []string{ticker.Spot}
|
||||
e.SupportsAutoPairUpdating = true
|
||||
e.Handler = new(request.Handler)
|
||||
e.SetRequestHandler(e.Name, exmoAuthRate, exmoUnauthRate, new(http.Client))
|
||||
}
|
||||
|
||||
// Setup takes in the supplied exchange configuration details and sets params
|
||||
@@ -99,8 +105,8 @@ func (e *EXMO) GetTrades(symbol string) (map[string][]Trades, error) {
|
||||
v.Set("pair", symbol)
|
||||
result := make(map[string][]Trades)
|
||||
url := fmt.Sprintf("%s/v%s/%s", exmoAPIURL, exmoAPIVersion, exmoTrades)
|
||||
err := common.SendHTTPGetRequest(common.EncodeURLValues(url, v), true, e.Verbose, &result)
|
||||
return result, err
|
||||
|
||||
return result, e.SendHTTPRequest(common.EncodeURLValues(url, v), &result)
|
||||
}
|
||||
|
||||
// GetOrderbook returns the orderbook for a symbol or symbols
|
||||
@@ -109,8 +115,8 @@ func (e *EXMO) GetOrderbook(symbol string) (map[string]Orderbook, error) {
|
||||
v.Set("pair", symbol)
|
||||
result := make(map[string]Orderbook)
|
||||
url := fmt.Sprintf("%s/v%s/%s", exmoAPIURL, exmoAPIVersion, exmoOrderbook)
|
||||
err := common.SendHTTPGetRequest(common.EncodeURLValues(url, v), true, e.Verbose, &result)
|
||||
return result, err
|
||||
|
||||
return result, e.SendHTTPRequest(common.EncodeURLValues(url, v), &result)
|
||||
}
|
||||
|
||||
// GetTicker returns the ticker for a symbol or symbols
|
||||
@@ -119,24 +125,24 @@ func (e *EXMO) GetTicker(symbol string) (map[string]Ticker, error) {
|
||||
v.Set("pair", symbol)
|
||||
result := make(map[string]Ticker)
|
||||
url := fmt.Sprintf("%s/v%s/%s", exmoAPIURL, exmoAPIVersion, exmoTicker)
|
||||
err := common.SendHTTPGetRequest(common.EncodeURLValues(url, v), true, e.Verbose, &result)
|
||||
return result, err
|
||||
|
||||
return result, e.SendHTTPRequest(common.EncodeURLValues(url, v), &result)
|
||||
}
|
||||
|
||||
// GetPairSettings returns the pair settings for a symbol or symbols
|
||||
func (e *EXMO) GetPairSettings() (map[string]PairSettings, error) {
|
||||
result := make(map[string]PairSettings)
|
||||
url := fmt.Sprintf("%s/v%s/%s", exmoAPIURL, exmoAPIVersion, exmoPairSettings)
|
||||
err := common.SendHTTPGetRequest(url, true, e.Verbose, &result)
|
||||
return result, err
|
||||
|
||||
return result, e.SendHTTPRequest(url, &result)
|
||||
}
|
||||
|
||||
// GetCurrency returns a list of currencies
|
||||
func (e *EXMO) GetCurrency() ([]string, error) {
|
||||
result := []string{}
|
||||
url := fmt.Sprintf("%s/v%s/%s", exmoAPIURL, exmoAPIVersion, exmoCurrency)
|
||||
err := common.SendHTTPGetRequest(url, true, e.Verbose, &result)
|
||||
return result, err
|
||||
|
||||
return result, e.SendHTTPRequest(url, &result)
|
||||
}
|
||||
|
||||
// GetUserInfo returns the user info
|
||||
@@ -311,6 +317,11 @@ func (e *EXMO) GetWalletHistory(date int64) (WalletHistory, error) {
|
||||
return result, err
|
||||
}
|
||||
|
||||
// SendHTTPRequest sends an unauthenticated HTTP request
|
||||
func (e *EXMO) SendHTTPRequest(path string, result interface{}) error {
|
||||
return e.SendPayload("GET", path, nil, nil, result, false, e.Verbose)
|
||||
}
|
||||
|
||||
// SendAuthenticatedHTTPRequest sends an authenticated HTTP request
|
||||
func (e *EXMO) SendAuthenticatedHTTPRequest(method, endpoint string, vals url.Values, result interface{}) error {
|
||||
if !e.AuthenticatedAPISupport {
|
||||
@@ -337,28 +348,6 @@ func (e *EXMO) SendAuthenticatedHTTPRequest(method, endpoint string, vals url.Va
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
|
||||
path := fmt.Sprintf("%s/v%s/%s", exmoAPIURL, exmoAPIVersion, endpoint)
|
||||
resp, err := common.SendHTTPRequest(method, path, headers, strings.NewReader(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if e.Verbose {
|
||||
log.Printf("Received raw: \n%s\n", resp)
|
||||
}
|
||||
|
||||
var authResp AuthResponse
|
||||
err = common.JSONDecode([]byte(resp), &authResp)
|
||||
if err != nil {
|
||||
return errors.New("unable to JSON Unmarshal auth response")
|
||||
}
|
||||
|
||||
if !authResp.Result && authResp.Error != "" {
|
||||
return fmt.Errorf("auth error: %s", authResp.Error)
|
||||
}
|
||||
|
||||
err = common.JSONDecode([]byte(resp), &result)
|
||||
if err != nil {
|
||||
return errors.New("unable to JSON Unmarshal response")
|
||||
}
|
||||
return nil
|
||||
return e.SendPayload(method, path, headers, strings.NewReader(payload), result, true, e.Verbose)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,10 @@ var (
|
||||
e EXMO
|
||||
)
|
||||
|
||||
func TestDefault(t *testing.T) {
|
||||
e.SetDefaults()
|
||||
}
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
e.AuthenticatedAPISupport = true
|
||||
e.APIKey = APIKey
|
||||
@@ -18,6 +22,7 @@ func TestSetup(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTrades(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := e.GetTrades("BTC_USD")
|
||||
if err != nil {
|
||||
t.Errorf("Test failed. Err: %s", err)
|
||||
|
||||
Reference in New Issue
Block a user