Trades api
This commit is contained in:
parent
28dd7e3190
commit
a5597382b0
7
errors.go
Normal file
7
errors.go
Normal file
@ -0,0 +1,7 @@
|
||||
package tacitus
|
||||
|
||||
type Error string
|
||||
|
||||
func (e Error) Error() string {
|
||||
return string(e)
|
||||
}
|
23
http/mux.go
Normal file
23
http/mux.go
Normal file
@ -0,0 +1,23 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Mux struct {
|
||||
*http.ServeMux
|
||||
|
||||
TradeHandler TradeHandler
|
||||
}
|
||||
|
||||
type errorResponse struct {
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func NewMux() *Mux {
|
||||
m := Mux{ServeMux: http.NewServeMux()}
|
||||
|
||||
m.Handle("/api/trades/", &m.TradeHandler)
|
||||
|
||||
return &m
|
||||
}
|
@ -9,7 +9,7 @@ const DefaultAddr = ":8080"
|
||||
|
||||
type Server struct {
|
||||
Addr string
|
||||
Handler *http.Handler
|
||||
Handler *Mux
|
||||
|
||||
Err chan error
|
||||
|
||||
@ -17,7 +17,8 @@ type Server struct {
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
return &Server{Addr: DefaultAddr}
|
||||
return &Server{Addr: DefaultAddr,
|
||||
Handler: NewMux()}
|
||||
}
|
||||
|
||||
func (s *Server) Open() error {
|
||||
|
73
http/trade_handler.go
Normal file
73
http/trade_handler.go
Normal file
@ -0,0 +1,73 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"tacitus"
|
||||
)
|
||||
|
||||
type TradeHandler struct {
|
||||
TradeService tacitus.TradeService
|
||||
}
|
||||
|
||||
type tradesResponse struct {
|
||||
Trades []tacitus.Trade `json:"trades,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (t *TradeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
paths := strings.Split(r.URL.Path, "/")
|
||||
|
||||
if len(paths) < 4 && !tacitus.ValidProduct(paths[3]) {
|
||||
errorResp(w, tacitus.Error("Invalid product"))
|
||||
return
|
||||
}
|
||||
|
||||
params := r.URL.Query()
|
||||
params.Set("product", paths[3])
|
||||
|
||||
switch r.Method {
|
||||
case "GET":
|
||||
t.getTrades(w, params)
|
||||
default:
|
||||
errorResp(w, tacitus.Error("Invalid request"))
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TradeHandler) getTrades(w http.ResponseWriter, params url.Values) {
|
||||
var trades []tacitus.Trade
|
||||
var err error
|
||||
limit := parseLimit(params.Get("limit"))
|
||||
prod := params.Get("product")
|
||||
after, _ := strconv.ParseInt(params.Get("after"), 10, 0)
|
||||
before, _ := strconv.ParseInt(params.Get("before"), 10, 0)
|
||||
|
||||
if params.Get("after") != "" {
|
||||
trades, err = t.TradeService.TradesAfter(prod, int(after), limit)
|
||||
} else if params.Get("before") != "" {
|
||||
trades, err = t.TradeService.TradesBefore(prod, int(before), limit)
|
||||
} else {
|
||||
trades, err = t.TradeService.LastTrades(prod, limit)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
encodeJson(w, &tradesResponse{Trades: trades})
|
||||
} else {
|
||||
errorResp(w, tacitus.Error("Invalid request"))
|
||||
}
|
||||
}
|
||||
|
||||
func parseLimit(limit string) int {
|
||||
l, err := strconv.ParseInt(limit, 10, 0)
|
||||
if err != nil {
|
||||
l = 1000
|
||||
}
|
||||
|
||||
if l > 1000 {
|
||||
l = 1000
|
||||
}
|
||||
|
||||
return int(l)
|
||||
}
|
17
http/util.go
Normal file
17
http/util.go
Normal file
@ -0,0 +1,17 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func encodeJson(w http.ResponseWriter, data interface{}) {
|
||||
if buf, err := json.Marshal(data); err == nil {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Write(buf)
|
||||
}
|
||||
}
|
||||
|
||||
func errorResp(w http.ResponseWriter, err error) {
|
||||
json.NewEncoder(w).Encode(&errorResponse{Error: err.Error()})
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"tacitus"
|
||||
@ -14,12 +15,16 @@ const (
|
||||
trade_find_trade_id = `SELECT id, ` + trade_columns + ` FROM trades ` +
|
||||
`WHERE trade_id = $1 AND product = $2;`
|
||||
trade_delete = `DELETE FROM trades WHERE id = $1;`
|
||||
trades_in_date_range = `SELECT id, ` + trade_columns + ` FROM ` +
|
||||
trade_in_date_range = `SELECT id, ` + trade_columns + ` FROM ` +
|
||||
`trades WHERE product = $1 AND timestamp >= $2 AND timestamp < $3;`
|
||||
trade_first = `SELECT id, ` + trade_columns + ` FROM trades WHERE ` +
|
||||
`product = $1 ORDER BY trade_id ASC LIMIT 1;`
|
||||
`product = $1 ORDER BY trade_id ASC LIMIT $2;`
|
||||
trade_last = `SELECT id, ` + trade_columns + ` FROM trades WHERE ` +
|
||||
`product = $1 ORDER BY trade_id DESC LIMIT 1;`
|
||||
`product = $1 ORDER BY trade_id DESC LIMIT $2;`
|
||||
trade_after = `SELECT id, ` + trade_columns + ` FROM trades ` +
|
||||
`WHERE product = $1 AND trade_id < $2 ORDER BY trade_id DESC LIMIT $3;`
|
||||
trade_before = `SELECT id, ` + trade_columns + ` FROM trades ` +
|
||||
`WHERE product = $1 AND trade_id > $2 ORDER BY trade_id ASC LIMIT $3;`
|
||||
)
|
||||
|
||||
type TradeService struct {
|
||||
@ -72,17 +77,60 @@ func (t *TradeService) DeleteTrade(id int) {
|
||||
func (t *TradeService) TradesInDateRange(product string, start,
|
||||
end time.Time) ([]tacitus.Trade, error) {
|
||||
|
||||
trades := make([]tacitus.Trade, 0)
|
||||
|
||||
rows, err := t.client.db.Query(trades_in_date_range, product, start, end)
|
||||
rows, err := t.client.db.Query(trade_in_date_range, product, start, end)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return deserializeTrades(rows)
|
||||
}
|
||||
|
||||
func (t *TradeService) FirstTrades(product string, limit int) ([]tacitus.Trade, error) {
|
||||
rows, err := t.client.db.Query(trade_first, product, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return deserializeTrades(rows)
|
||||
}
|
||||
|
||||
func (t *TradeService) LastTrades(product string, limit int) ([]tacitus.Trade, error) {
|
||||
rows, err := t.client.db.Query(trade_last, product, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return deserializeTrades(rows)
|
||||
}
|
||||
|
||||
func (t *TradeService) TradesAfter(product string, id, limit int) ([]tacitus.Trade, error) {
|
||||
rows, err := t.client.db.Query(trade_after, product, id, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return deserializeTrades(rows)
|
||||
|
||||
}
|
||||
|
||||
func (t *TradeService) TradesBefore(product string, id, limit int) ([]tacitus.Trade, error) {
|
||||
rows, err := t.client.db.Query(trade_before, product, id, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return deserializeTrades(rows)
|
||||
|
||||
}
|
||||
|
||||
func deserializeTrades(rows *sql.Rows) ([]tacitus.Trade, error) {
|
||||
defer rows.Close()
|
||||
|
||||
trades := make([]tacitus.Trade, 0)
|
||||
|
||||
for rows.Next() {
|
||||
var trade tacitus.Trade
|
||||
if err = rows.Scan(&trade.Id, &trade.TradeId, &trade.Product, &trade.Price,
|
||||
if err := rows.Scan(&trade.Id, &trade.TradeId, &trade.Product, &trade.Price,
|
||||
&trade.Size, &trade.Buy, &trade.Sell, &trade.Timestamp); err != nil {
|
||||
|
||||
return nil, err
|
||||
@ -97,29 +145,3 @@ func (t *TradeService) TradesInDateRange(product string, start,
|
||||
|
||||
return trades, nil
|
||||
}
|
||||
|
||||
func (t *TradeService) FirstTrade(product string) (*tacitus.Trade, error) {
|
||||
var tr tacitus.Trade
|
||||
|
||||
row := t.client.db.QueryRow(trade_first, product)
|
||||
if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy,
|
||||
&tr.Sell, &tr.Timestamp); err != nil {
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &tr, nil
|
||||
}
|
||||
|
||||
func (t *TradeService) LastTrade(product string) (*tacitus.Trade, error) {
|
||||
var tr tacitus.Trade
|
||||
|
||||
row := t.client.db.QueryRow(trade_last, product)
|
||||
if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy,
|
||||
&tr.Sell, &tr.Timestamp); err != nil {
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &tr, nil
|
||||
}
|
||||
|
34
trade.go
34
trade.go
@ -3,18 +3,21 @@ package tacitus
|
||||
import (
|
||||
"github.com/shopspring/decimal"
|
||||
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var PRODUCTS = [...]string{"BTCUSD", "ETHUSD"}
|
||||
|
||||
type Trade struct {
|
||||
Id int
|
||||
TradeId int
|
||||
Product string
|
||||
Price decimal.Decimal
|
||||
Size decimal.Decimal
|
||||
Buy bool
|
||||
Sell bool
|
||||
Timestamp time.Time
|
||||
Id int `json:"-"`
|
||||
TradeId int `json:"trade_id,"`
|
||||
Product string `json:"product,"`
|
||||
Price decimal.Decimal `json:"price,"`
|
||||
Size decimal.Decimal `json:"size,"`
|
||||
Buy bool `json:"buy,"`
|
||||
Sell bool `json:"sell,"`
|
||||
Timestamp time.Time `json:"timestamp,"`
|
||||
}
|
||||
|
||||
type TradeService interface {
|
||||
@ -23,4 +26,19 @@ type TradeService interface {
|
||||
CreateTrade(t *Trade) error
|
||||
DeleteTrade(id int)
|
||||
TradesInDateRange(product string, start, end time.Time) ([]Trade, error)
|
||||
FirstTrades(product string, limit int) ([]Trade, error)
|
||||
LastTrades(product string, limit int) ([]Trade, error)
|
||||
TradesAfter(product string, id, limit int) ([]Trade, error)
|
||||
TradesBefore(product string, id, limit int) ([]Trade, error)
|
||||
}
|
||||
|
||||
func ValidProduct(prod string) bool {
|
||||
prod = strings.ToUpper(prod)
|
||||
for _, p := range PRODUCTS {
|
||||
if p == prod {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
Reference in New Issue
Block a user