67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package gdax
|
|
|
|
import (
|
|
"github.com/kcotugno/tacitus"
|
|
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
endpoint = "https://api.gdax.com"
|
|
)
|
|
|
|
type PublicClient struct {
|
|
client http.Client
|
|
}
|
|
|
|
func NewPublicClient() *PublicClient {
|
|
c := PublicClient{}
|
|
c.client = http.Client{Timeout: 30 * time.Second}
|
|
|
|
return &c
|
|
}
|
|
|
|
func (c *PublicClient) GetTradesBefore(product string, id int) ([]tacitus.Trade, error) {
|
|
url := strings.Join([]string{endpoint, "products", product, "trades"}, "/")
|
|
url = strings.Join([]string{url, "?after=", strconv.Itoa(id)}, "")
|
|
|
|
resp, err := c.client.Get(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
results := make([]tradeResponse, 0)
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
if err = decoder.Decode(&results); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
trades := make([]tacitus.Trade, 0)
|
|
for _, t := range results {
|
|
var trade tacitus.Trade
|
|
|
|
trade.TradeId = t.TradeId
|
|
trade.Product = strings.ToUpper(product)
|
|
trade.Price = t.Price
|
|
trade.Size = t.Size
|
|
trade.Timestamp = t.Time
|
|
|
|
switch t.Side {
|
|
case "buy":
|
|
trade.Buy = true
|
|
case "sell":
|
|
trade.Sell = true
|
|
}
|
|
|
|
trades = append(trades, trade)
|
|
}
|
|
|
|
return trades, nil
|
|
}
|