Skip to content

Commit

Permalink
Introduce request package and integrate with exchanges
Browse files Browse the repository at this point in the history
  • Loading branch information
shazbert authored and thrasher- committed Mar 27, 2018
1 parent 52dfddb commit 7fc9d20
Show file tree
Hide file tree
Showing 52 changed files with 1,989 additions and 1,543 deletions.
55 changes: 18 additions & 37 deletions exchanges/alphapoint/alphapoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
35 changes: 17 additions & 18 deletions exchanges/anx/anx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"errors"
"fmt"
"log"
"net/http"
"strconv"
"time"

"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"
)

Expand All @@ -27,11 +29,16 @@ const (
anxCreateAddress = "receive/create"
anxTicker = "money/ticker"
anxDepth = "money/depth/full"

// ANX rate limites for authenticated and unauthenticated requests
anxAuthRate = 1000
anxUnauthRate = 1000
)

// ANX is the overarching type across the alphapoint package
type ANX struct {
exchange.Base
*request.Handler
}

// SetDefaults sets current default settings
Expand All @@ -51,6 +58,8 @@ func (a *ANX) SetDefaults() {
a.ConfigCurrencyPairFormat.Index = "BTC"
a.AssetTypes = []string{ticker.Spot}
a.SupportsAutoPairUpdating = false
a.Handler = new(request.Handler)
a.SetRequestHandler(a.Name, anxAuthRate, anxUnauthRate, new(http.Client))
}

//Setup is run on startup to setup exchange with config values
Expand Down Expand Up @@ -95,15 +104,15 @@ func (a *ANX) GetTicker(currency string) (Ticker, error) {
var ticker Ticker
path := fmt.Sprintf("%sapi/2/%s/%s", anxAPIURL, currency, anxTicker)

return ticker, common.SendHTTPGetRequest(path, true, a.Verbose, &ticker)
return ticker, a.SendHTTPRequest(path, &ticker)
}

// GetDepth returns current orderbook depth.
func (a *ANX) GetDepth(currency string) (Depth, error) {
var depth Depth
path := fmt.Sprintf("%sapi/2/%s/%s", anxAPIURL, currency, anxDepth)

return depth, common.SendHTTPGetRequest(path, true, a.Verbose, &depth)
return depth, a.SendHTTPRequest(path, &depth)
}

// GetAPIKey returns a new generated API key set.
Expand Down Expand Up @@ -324,6 +333,11 @@ func (a *ANX) GetDepositAddress(currency, name string, new bool) (string, error)
return response.Address, nil
}

// SendHTTPRequest sends an unauthenticated HTTP request
func (a *ANX) SendHTTPRequest(path string, result interface{}) error {
return a.SendPayload("GET", path, nil, nil, result, false, a.Verbose)
}

// SendAuthenticatedHTTPRequest sends a authenticated HTTP request
func (a *ANX) SendAuthenticatedHTTPRequest(path string, params map[string]interface{}, result interface{}) error {
if !a.AuthenticatedAPISupport {
Expand Down Expand Up @@ -362,20 +376,5 @@ func (a *ANX) SendAuthenticatedHTTPRequest(path string, params map[string]interf
headers["Rest-Sign"] = common.Base64Encode([]byte(hmac))
headers["Content-Type"] = "application/json"

resp, err := common.SendHTTPRequest("POST", anxAPIURL+path, headers, bytes.NewBuffer(PayloadJSON))
if err != nil {
return err
}

if a.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 a.SendPayload("POST", anxAPIURL+path, headers, bytes.NewBuffer(PayloadJSON), result, true, a.Verbose)
}
Loading

0 comments on commit 7fc9d20

Please sign in to comment.