mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-23 15:10:15 +00:00
Introduce request package and integrate with exchanges
This commit is contained in:
committed by
Adrian Gallagher
parent
52dfddbb18
commit
7fc9d20fd7
@@ -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)
|
||||
}
|
||||
|
||||
@@ -47,10 +47,11 @@ type Trades struct {
|
||||
|
||||
// Wallet contains specific wallet information
|
||||
type Wallet struct {
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"userId"`
|
||||
Name string `json:"name"`
|
||||
Balances []Balance `json:"balances"`
|
||||
ID string `json:"id"`
|
||||
UserID string `json:"userId"`
|
||||
Name string `json:"name"`
|
||||
Balances []Balance `json:"balances"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// Balance is a sub type holding balance information
|
||||
@@ -58,6 +59,7 @@ type Balance struct {
|
||||
Currency string `json:"currency"`
|
||||
AvailableBalance float64 `json:"availableBalance,string"`
|
||||
TotalBalance float64 `json:"totalBalance,string"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// Records embodies records of trade history information
|
||||
@@ -67,6 +69,7 @@ type Records struct {
|
||||
LatestExecutedID int64 `json:"latestExecutionId,string"`
|
||||
RecordsPerPage int `json:"recordsPerPage,string"`
|
||||
TradingHistory []TradeHistory `json:"tradingHistory"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// TradeHistory stores historic trade values
|
||||
@@ -93,6 +96,7 @@ type FundingRecords struct {
|
||||
LatestExecutedID int64 `json:"latestExecutionId,string"`
|
||||
RecordsPerPage int `json:"recordsPerPage,string"`
|
||||
FundingHistory []FundHistory `json:"fundingHistory"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// FundHistory stores historic funding transactions
|
||||
@@ -126,6 +130,7 @@ type Order struct {
|
||||
Status string `json:"Status"`
|
||||
Metadata interface{} `json:"metadata"`
|
||||
ClientOrderIdentifier string `json:"clientOrderIdentifier"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// CryptoCurrencyDeposit holds information about a new wallet
|
||||
@@ -134,6 +139,7 @@ type CryptoCurrencyDeposit struct {
|
||||
WalletID string `json:"walletID"`
|
||||
DepositAddress string `json:"depositAddress"`
|
||||
Metadata interface{} `json:"metadata"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// WalletTransfer holds wallet transfer information
|
||||
@@ -142,4 +148,5 @@ type WalletTransfer struct {
|
||||
DestinationWalletID string `json:"destinationWalletId"`
|
||||
Amount float64 `json:"amount,string"`
|
||||
CurrencyCode string `json:"currencyCode"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user