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/gorilla/websocket"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/request"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)
@@ -36,14 +38,16 @@ const (
alphapointOpenOrders = "GetAccountOpenOrders"
alphapointOrderFee = "GetOrderFee"
// Anymore and you get IP banned
alphapointMaxRequestsPer10minutes = 500
// alphapoint rate times
alphapointAuthRate = 1200
alphapointUnauthRate = 1200
)
// Alphapoint is the overarching type across the alphapoint package
type Alphapoint struct {
exchange.Base
WebsocketConn *websocket.Conn
*request.Handler
}
// SetDefaults sets current default settings
@@ -52,6 +56,8 @@ func (a *Alphapoint) SetDefaults() {
a.WebsocketURL = alphapointDefaultWebsocketURL
a.AssetTypes = []string{ticker.Spot}
a.SupportsAutoPairUpdating = false
a.Handler = new(request.Handler)
a.SetRequestHandler(a.Name, alphapointAuthRate, alphapointUnauthRate, new(http.Client))
}
// GetTicker returns current ticker information from Alphapoint for a selected
@@ -61,7 +67,7 @@ func (a *Alphapoint) GetTicker(currencyPair string) (Ticker, error) {
request["productPair"] = currencyPair
response := Ticker{}
err := a.SendRequest("POST", alphapointTicker, request, &response)
err := a.SendHTTPRequest("POST", alphapointTicker, request, &response)
if err != nil {
return response, err
}
@@ -84,7 +90,7 @@ func (a *Alphapoint) GetTrades(currencyPair string, startIndex, count int) (Trad
request["Count"] = count
response := Trades{}
err := a.SendRequest("POST", alphapointTrades, request, &response)
err := a.SendHTTPRequest("POST", alphapointTrades, request, &response)
if err != nil {
return response, err
}
@@ -105,7 +111,7 @@ func (a *Alphapoint) GetTradesByDate(currencyPair string, startDate, endDate int
request["endDate"] = endDate
response := Trades{}
err := a.SendRequest("POST", alphapointTradesByDate, request, &response)
err := a.SendHTTPRequest("POST", alphapointTradesByDate, request, &response)
if err != nil {
return response, err
}
@@ -122,7 +128,7 @@ func (a *Alphapoint) GetOrderbook(currencyPair string) (Orderbook, error) {
request["productPair"] = currencyPair
response := Orderbook{}
err := a.SendRequest("POST", alphapointOrderbook, request, &response)
err := a.SendHTTPRequest("POST", alphapointOrderbook, request, &response)
if err != nil {
return response, err
}
@@ -136,7 +142,7 @@ func (a *Alphapoint) GetOrderbook(currencyPair string) (Orderbook, error) {
func (a *Alphapoint) GetProductPairs() (ProductPairs, error) {
response := ProductPairs{}
err := a.SendRequest("POST", alphapointProductPairs, nil, &response)
err := a.SendHTTPRequest("POST", alphapointProductPairs, nil, &response)
if err != nil {
return response, err
}
@@ -150,7 +156,7 @@ func (a *Alphapoint) GetProductPairs() (ProductPairs, error) {
func (a *Alphapoint) GetProducts() (Products, error) {
response := Products{}
err := a.SendRequest("POST", alphapointProducts, nil, &response)
err := a.SendHTTPRequest("POST", alphapointProducts, nil, &response)
if err != nil {
return response, err
}
@@ -504,8 +510,8 @@ func (a *Alphapoint) GetOrderFee(symbol, side string, quantity, price float64) (
return response.Fee, nil
}
// SendRequest sends an unauthenticated request
func (a *Alphapoint) SendRequest(method, path string, data map[string]interface{}, result interface{}) error {
// SendHTTPRequest sends an unauthenticated HTTP request
func (a *Alphapoint) SendHTTPRequest(method, path string, data map[string]interface{}, result interface{}) error {
headers := make(map[string]string)
headers["Content-Type"] = "application/json"
path = fmt.Sprintf("%s/ajax/v%s/%s", a.APIUrl, alphapointAPIVersion, path)
@@ -515,21 +521,7 @@ func (a *Alphapoint) SendRequest(method, path string, data map[string]interface{
return errors.New("SendHTTPRequest: Unable to JSON request")
}
resp, err := common.SendHTTPRequest(
method,
path,
headers,
bytes.NewBuffer(PayloadJSON),
)
if err != nil {
return err
}
err = common.JSONDecode([]byte(resp), &result)
if err != nil {
return errors.New("unable to JSON Unmarshal response")
}
return nil
return a.SendPayload(method, path, headers, bytes.NewBuffer(PayloadJSON), result, false, a.Verbose)
}
// SendAuthenticatedHTTPRequest sends an authenticated request
@@ -557,16 +549,5 @@ func (a *Alphapoint) SendAuthenticatedHTTPRequest(method, path string, data map[
return errors.New("SendAuthenticatedHTTPRequest: Unable to JSON request")
}
resp, err := common.SendHTTPRequest(
method, path, headers, bytes.NewBuffer(PayloadJSON),
)
if err != nil {
return err
}
err = common.JSONDecode([]byte(resp), &result)
if err != nil {
return errors.New("unable to JSON Unmarshal response")
}
return nil
return a.SendPayload(method, path, headers, bytes.NewBuffer(PayloadJSON), result, true, a.Verbose)
}