forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tickerRoutes.go
89 lines (78 loc) · 2.27 KB
/
tickerRoutes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
func jsonTickerResponse(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
currency := vars["currency"]
exchangeName := vars["exchangeName"]
var response TickerPrice
var err error
for i := 0; i < len(bot.exchanges); i++ {
if bot.exchanges[i] != nil {
if bot.exchanges[i].IsEnabled() && bot.exchanges[i].GetName() == exchangeName {
response, err = bot.exchanges[i].GetTickerPrice(currency)
if err != nil {
log.Println(err)
continue
}
}
}
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
encoder := json.NewEncoder(w)
if err = encoder.Encode(response); err != nil {
panic(err)
}
}
type AllEnabledExchangeCurrencies struct {
Data []EnabledExchangeCurrencies `json:"data"`
}
type EnabledExchangeCurrencies struct {
ExchangeName string `json:"exchangeName"`
ExchangeValues []TickerPrice `json:"exchangeValues"`
}
func getAllActiveTickersResponse(w http.ResponseWriter, r *http.Request) {
var response AllEnabledExchangeCurrencies
for _, individualBot := range bot.exchanges {
if individualBot != nil && individualBot.IsEnabled() {
var individualExchange EnabledExchangeCurrencies
individualExchange.ExchangeName = individualBot.GetName()
log.Println("Getting enabled currencies for '" + individualBot.GetName() + "'")
currencies := individualBot.GetEnabledCurrencies()
log.Println(currencies)
for _, currency := range currencies {
tickerPrice, err := individualBot.GetTickerPrice(currency)
if err != nil {
continue
}
log.Println(tickerPrice)
individualExchange.ExchangeValues = append(individualExchange.ExchangeValues, tickerPrice)
}
response.Data = append(response.Data, individualExchange)
}
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
panic(err)
}
}
var ExchangeRoutes = Routes{
Route{
"AllActiveExchangesAndCurrencies",
"GET",
"/exchanges/enabled/latest/all",
getAllActiveTickersResponse,
},
Route{
"IndividualExchangeAndCurrency",
"GET",
"/exchanges/{exchangeName}/latest/{currency}",
jsonTickerResponse,
},
}