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,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"time"
@@ -12,6 +13,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"
)
@@ -28,11 +30,15 @@ const (
itbitOrders = "orders"
itbitCryptoDeposits = "cryptocurrency_deposits"
itbitWalletTransfer = "wallet_transfers"
itbitAuthRate = 0
itbitUnauthRate = 0
)
// ItBit is the overarching type across the ItBit package
type ItBit struct {
exchange.Base
*request.Handler
}
// SetDefaults sets the defaults for the exchange
@@ -50,6 +56,8 @@ func (i *ItBit) SetDefaults() {
i.ConfigCurrencyPairFormat.Uppercase = true
i.AssetTypes = []string{ticker.Spot}
i.SupportsAutoPairUpdating = false
i.Handler = new(request.Handler)
i.SetRequestHandler(i.Name, itbitAuthRate, itbitUnauthRate, new(http.Client))
}
// Setup sets the exchange parameters from exchange config
@@ -95,8 +103,7 @@ func (i *ItBit) GetTicker(currencyPair string) (Ticker, error) {
var response Ticker
path := fmt.Sprintf("%s/%s/%s/%s", itbitAPIURL, itbitMarkets, currencyPair, itbitTicker)
return response,
common.SendHTTPGetRequest(path, true, i.Verbose, &response)
return response, i.SendHTTPRequest(path, &response)
}
// GetOrderbook returns full order book for the specified market.
@@ -105,8 +112,7 @@ func (i *ItBit) GetOrderbook(currencyPair string) (OrderbookResponse, error) {
response := OrderbookResponse{}
path := fmt.Sprintf("%s/%s/%s/%s", itbitAPIURL, itbitMarkets, currencyPair, itbitOrderbook)
return response,
common.SendHTTPGetRequest(path, true, i.Verbose, &response)
return response, i.SendHTTPRequest(path, &response)
}
// GetTradeHistory returns recent trades for a specified market.
@@ -118,8 +124,7 @@ func (i *ItBit) GetTradeHistory(currencyPair, timestamp string) (Trades, error)
req := "trades?since=" + timestamp
path := fmt.Sprintf("%s/%s/%s/%s", itbitAPIURL, itbitMarkets, currencyPair, req)
return response,
common.SendHTTPGetRequest(path, true, i.Verbose, &response)
return response, i.SendHTTPRequest(path, &response)
}
// GetWallets returns information about all wallets associated with the account.
@@ -142,8 +147,14 @@ func (i *ItBit) CreateWallet(walletName string) (Wallet, error) {
params["userId"] = i.ClientID
params["name"] = walletName
return resp,
i.SendAuthenticatedHTTPRequest("POST", "/"+itbitWallets, params, &resp)
err := i.SendAuthenticatedHTTPRequest("POST", "/"+itbitWallets, params, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// GetWallet returns wallet information by walletID
@@ -151,7 +162,14 @@ func (i *ItBit) GetWallet(walletID string) (Wallet, error) {
resp := Wallet{}
path := fmt.Sprintf("/%s/%s", itbitWallets, walletID)
return resp, i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
err := i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// GetWalletBalance returns balance information for a specific currency in a
@@ -160,7 +178,14 @@ func (i *ItBit) GetWalletBalance(walletID, currency string) (Balance, error) {
resp := Balance{}
path := fmt.Sprintf("/%s/%s/%s/%s", itbitWallets, walletID, itbitBalances, currency)
return resp, i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
err := i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// GetWalletTrades returns all trades for a specified wallet.
@@ -169,7 +194,14 @@ func (i *ItBit) GetWalletTrades(walletID string, params url.Values) (Records, er
url := fmt.Sprintf("/%s/%s/%s", itbitWallets, walletID, itbitTrades)
path := common.EncodeURLValues(url, params)
return resp, i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
err := i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// GetFundingHistory returns all funding history for a specified wallet.
@@ -178,7 +210,14 @@ func (i *ItBit) GetFundingHistory(walletID string, params url.Values) (FundingRe
url := fmt.Sprintf("/%s/%s/%s", itbitWallets, walletID, itbitFundingHistory)
path := common.EncodeURLValues(url, params)
return resp, i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
err := i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// PlaceOrder places a new order
@@ -198,7 +237,14 @@ func (i *ItBit) PlaceOrder(walletID, side, orderType, currency string, amount, p
params["clientOrderIdentifier"] = clientRef
}
return resp, i.SendAuthenticatedHTTPRequest("POST", path, params, &resp)
err := i.SendAuthenticatedHTTPRequest("POST", path, params, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// GetOrder returns an order by id.
@@ -207,7 +253,14 @@ func (i *ItBit) GetOrder(walletID string, params url.Values) (Order, error) {
url := fmt.Sprintf("/%s/%s/%s", itbitWallets, walletID, itbitOrders)
path := common.EncodeURLValues(url, params)
return resp, i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
err := i.SendAuthenticatedHTTPRequest("GET", path, nil, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// CancelOrder cancels and open order. *This is not a guarantee that the order
@@ -225,7 +278,14 @@ func (i *ItBit) GetDepositAddress(walletID, currency string) (CryptoCurrencyDepo
params := make(map[string]interface{})
params["currency"] = currency
return resp, i.SendAuthenticatedHTTPRequest("POST", path, params, &resp)
err := i.SendAuthenticatedHTTPRequest("POST", path, params, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// WalletTransfer transfers funds between wallets.
@@ -239,7 +299,19 @@ func (i *ItBit) WalletTransfer(walletID, sourceWallet, destWallet string, amount
params["amount"] = strconv.FormatFloat(amount, 'f', -1, 64)
params["currencyCode"] = currency
return resp, i.SendAuthenticatedHTTPRequest("POST", path, params, &resp)
err := i.SendAuthenticatedHTTPRequest("POST", path, params, &resp)
if err != nil {
return resp, err
}
if resp.Description != "" {
return resp, errors.New(resp.Description)
}
return resp, nil
}
// SendHTTPRequest sends an unauthenticated HTTP request
func (i *ItBit) SendHTTPRequest(path string, result interface{}) error {
return i.SendPayload("GET", path, nil, nil, result, false, i.Verbose)
}
// SendAuthenticatedHTTPRequest sends an authenticated request to itBit
@@ -290,19 +362,5 @@ func (i *ItBit) SendAuthenticatedHTTPRequest(method string, path string, params
headers["X-Auth-Nonce"] = nonce
headers["Content-Type"] = "application/json"
resp, err := common.SendHTTPRequest(method, url, headers, bytes.NewBuffer([]byte(PayloadJSON)))
if err != nil {
return err
}
if i.Verbose {
log.Printf("Received raw: \n%s\n", resp)
}
errCapture := GeneralReturn{}
if err := common.JSONDecode([]byte(resp), &errCapture); err == nil {
return errors.New(errCapture.Description)
}
return common.JSONDecode([]byte(resp), result)
return i.SendPayload(method, url, headers, bytes.NewBuffer([]byte(PayloadJSON)), result, true, i.Verbose)
}