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

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"net/http"
"net/url"
"reflect"
"strconv"
@@ -12,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"
)
const (
@@ -66,6 +68,9 @@ const (
// just your average return type from okex
returnTypeOne = "map[string]interface {}"
okexAuthRate = 0
okexUnauthRate = 0
)
var errMissValue = errors.New("warning - resp value is missing from exchange")
@@ -82,6 +87,8 @@ type OKEX struct {
CurrencyPairs []string
ContractPosition []string
Types []string
*request.Handler
}
// SetDefaults method assignes the default values for Bittrex
@@ -98,6 +105,8 @@ func (o *OKEX) SetDefaults() {
o.ConfigCurrencyPairFormat.Delimiter = "_"
o.ConfigCurrencyPairFormat.Uppercase = false
o.SupportsAutoPairUpdating = false
o.Handler = new(request.Handler)
o.SetRequestHandler(o.Name, okexAuthRate, okexUnauthRate, new(http.Client))
}
// Setup method sets current configuration details if enabled
@@ -149,7 +158,7 @@ func (o *OKEX) GetContractPrice(symbol, contractType string) (ContractPrice, err
path := fmt.Sprintf("%s%s%s.do?%s", apiURL, apiVersion, contractPrice, values.Encode())
err := common.SendHTTPGetRequest(path, true, o.Verbose, &resp)
err := o.SendHTTPRequest(path, &resp)
if err != nil {
return resp, err
}
@@ -183,7 +192,7 @@ func (o *OKEX) GetContractMarketDepth(symbol, contractType string) (ActualContra
path := fmt.Sprintf("%s%s%s.do?%s", apiURL, apiVersion, contractFutureDepth, values.Encode())
err := common.SendHTTPGetRequest(path, true, o.Verbose, &resp)
err := o.SendHTTPRequest(path, &resp)
if err != nil {
return fullDepth, err
}
@@ -247,7 +256,7 @@ func (o *OKEX) GetContractTradeHistory(symbol, contractType string) ([]ActualCon
path := fmt.Sprintf("%s%s%s.do?%s", apiURL, apiVersion, contractTradeHistory, values.Encode())
err := common.SendHTTPGetRequest(path, true, o.Verbose, &resp)
err := o.SendHTTPRequest(path, &resp)
if err != nil {
return actualTradeHistory, err
}
@@ -284,7 +293,7 @@ func (o *OKEX) GetContractIndexPrice(symbol string) (float64, error) {
path := fmt.Sprintf("%s%s%s.do?%s", apiURL, apiVersion, contractFutureIndex, values.Encode())
var resp interface{}
err := common.SendHTTPGetRequest(path, true, o.Verbose, &resp)
err := o.SendHTTPRequest(path, &resp)
if err != nil {
return 0, err
}
@@ -307,7 +316,7 @@ func (o *OKEX) GetContractExchangeRate() (float64, error) {
path := fmt.Sprintf("%s%s%s.do?", apiURL, apiVersion, contractExchangeRate)
var resp interface{}
if err := common.SendHTTPGetRequest(path, true, o.Verbose, &resp); err != nil {
if err := o.SendHTTPRequest(path, &resp); err != nil {
return 0, err
}
@@ -335,7 +344,7 @@ func (o *OKEX) GetContractFutureEstimatedPrice(symbol string) (float64, error) {
path := fmt.Sprintf("%s%s%s.do?%s", apiURL, apiVersion, contractFutureIndex, values.Encode())
var resp interface{}
if err := common.SendHTTPGetRequest(path, true, o.Verbose, &resp); err != nil {
if err := o.SendHTTPRequest(path, &resp); err != nil {
return 0, err
}
@@ -379,7 +388,7 @@ func (o *OKEX) GetContractCandlestickData(symbol, typeInput, contractType string
path := fmt.Sprintf("%s%s%s.do?%s", apiURL, apiVersion, contractCandleStick, values.Encode())
var resp interface{}
if err := common.SendHTTPGetRequest(path, true, o.Verbose, &resp); err != nil {
if err := o.SendHTTPRequest(path, &resp); err != nil {
return candleData, err
}
@@ -434,7 +443,7 @@ func (o *OKEX) GetContractHoldingsNumber(symbol, contractType string) (map[strin
path := fmt.Sprintf("%s%s%s.do?%s", apiURL, apiVersion, contractFutureHoldAmount, values.Encode())
var resp interface{}
if err := common.SendHTTPGetRequest(path, true, o.Verbose, &resp); err != nil {
if err := o.SendHTTPRequest(path, &resp); err != nil {
return holdingsNumber, err
}
@@ -470,7 +479,7 @@ func (o *OKEX) GetContractlimit(symbol, contractType string) (map[string]float64
path := fmt.Sprintf("%s%s%s.do?%s", apiURL, apiVersion, contractFutureLimits, values.Encode())
var resp interface{}
if err := common.SendHTTPGetRequest(path, true, o.Verbose, &resp); err != nil {
if err := o.SendHTTPRequest(path, &resp); err != nil {
return contractLimits, err
}
@@ -782,6 +791,11 @@ func (o *OKEX) GetErrorCode(code interface{}) error {
return errors.New("unable to find SPOT error code")
}
// SendHTTPRequest sends an unauthenticated HTTP request
func (o *OKEX) SendHTTPRequest(path string, result interface{}) error {
return o.SendPayload("GET", path, nil, nil, result, false, o.Verbose)
}
// SendAuthenticatedHTTPRequest sends an authenticated http request to a desired
// path
func (o *OKEX) SendAuthenticatedHTTPRequest(method string, values url.Values, result interface{}) (err error) {
@@ -803,20 +817,7 @@ func (o *OKEX) SendAuthenticatedHTTPRequest(method string, values url.Values, re
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
resp, err := common.SendHTTPRequest("POST", path, headers, strings.NewReader(encoded))
if err != nil {
return err
}
if o.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 o.SendPayload("POST", path, headers, strings.NewReader(encoded), result, true, o.Verbose)
}
// SetErrorDefaults sets the full error default list