forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restful_server.go
293 lines (258 loc) · 8.42 KB
/
restful_server.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/thrasher-/gocryptotrader/config"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)
// AllEnabledExchangeOrderbooks holds the enabled exchange orderbooks
type AllEnabledExchangeOrderbooks struct {
Data []EnabledExchangeOrderbooks `json:"data"`
}
// EnabledExchangeOrderbooks is a sub type for singular exchanges and respective
// orderbooks
type EnabledExchangeOrderbooks struct {
ExchangeName string `json:"exchangeName"`
ExchangeValues []orderbook.Base `json:"exchangeValues"`
}
// AllEnabledExchangeCurrencies holds the enabled exchange currencies
type AllEnabledExchangeCurrencies struct {
Data []EnabledExchangeCurrencies `json:"data"`
}
// EnabledExchangeCurrencies is a sub type for singular exchanges and respective
// currencies
type EnabledExchangeCurrencies struct {
ExchangeName string `json:"exchangeName"`
ExchangeValues []ticker.Price `json:"exchangeValues"`
}
// AllEnabledExchangeAccounts holds all enabled accounts info
type AllEnabledExchangeAccounts struct {
Data []exchange.AccountInfo `json:"data"`
}
// RESTfulJSONResponse outputs a JSON response of the req interface
func RESTfulJSONResponse(w http.ResponseWriter, r *http.Request, req interface{}) error {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(req)
}
// RESTfulError prints the REST method and error
func RESTfulError(method string, err error) {
log.Printf("RESTful %s: server failed to send JSON response. Error %s",
method, err)
}
// RESTGetAllSettings replies to a request with an encoded JSON response about the
// trading bots configuration.
func RESTGetAllSettings(w http.ResponseWriter, r *http.Request) {
err := RESTfulJSONResponse(w, r, bot.config)
if err != nil {
RESTfulError(r.Method, err)
}
}
// RESTSaveAllSettings saves all current settings from request body as a JSON
// document then reloads state and returns the settings
func RESTSaveAllSettings(w http.ResponseWriter, r *http.Request) {
//Get the data from the request
decoder := json.NewDecoder(r.Body)
var responseData config.Post
err := decoder.Decode(&responseData)
if err != nil {
RESTfulError(r.Method, err)
}
//Save change the settings
err = bot.config.UpdateConfig(bot.configFile, responseData.Data)
if err != nil {
RESTfulError(r.Method, err)
}
err = RESTfulJSONResponse(w, r, bot.config)
if err != nil {
RESTfulError(r.Method, err)
}
SetupExchanges()
}
// RESTGetOrderbook returns orderbook info for a given currency, exchange and
// asset type
func RESTGetOrderbook(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
currency := vars["currency"]
exchange := vars["exchangeName"]
assetType := vars["assetType"]
if assetType == "" {
assetType = orderbook.Spot
}
response, err := GetSpecificOrderbook(currency, exchange, assetType)
if err != nil {
log.Printf("Failed to fetch orderbook for %s currency: %s\n", exchange,
currency)
return
}
err = RESTfulJSONResponse(w, r, response)
if err != nil {
RESTfulError(r.Method, err)
}
}
// GetAllActiveOrderbooks returns all enabled exchanges orderbooks
func GetAllActiveOrderbooks() []EnabledExchangeOrderbooks {
var orderbookData []EnabledExchangeOrderbooks
for _, individualBot := range bot.exchanges {
if individualBot != nil && individualBot.IsEnabled() {
var individualExchange EnabledExchangeOrderbooks
exchangeName := individualBot.GetName()
individualExchange.ExchangeName = exchangeName
currencies := individualBot.GetEnabledCurrencies()
assetTypes, err := exchange.GetExchangeAssetTypes(exchangeName)
if err != nil {
log.Printf("failed to get %s exchange asset types. Error: %s",
exchangeName, err)
continue
}
for _, x := range currencies {
currency := x
var ob orderbook.Base
if len(assetTypes) > 1 {
for y := range assetTypes {
ob, err = individualBot.GetOrderbookEx(currency,
assetTypes[y])
}
} else {
ob, err = individualBot.GetOrderbookEx(currency,
assetTypes[0])
}
if err != nil {
log.Printf("failed to get %s %s orderbook. Error: %s",
currency.Pair().String(),
exchangeName,
err)
continue
}
individualExchange.ExchangeValues = append(
individualExchange.ExchangeValues, ob,
)
}
orderbookData = append(orderbookData, individualExchange)
}
}
return orderbookData
}
// RESTGetAllActiveOrderbooks returns all enabled exchange orderbooks
func RESTGetAllActiveOrderbooks(w http.ResponseWriter, r *http.Request) {
var response AllEnabledExchangeOrderbooks
response.Data = GetAllActiveOrderbooks()
err := RESTfulJSONResponse(w, r, response)
if err != nil {
RESTfulError(r.Method, err)
}
}
// RESTGetPortfolio returns the bot portfolio
func RESTGetPortfolio(w http.ResponseWriter, r *http.Request) {
result := bot.portfolio.GetPortfolioSummary()
err := RESTfulJSONResponse(w, r, result)
if err != nil {
RESTfulError(r.Method, err)
}
}
// RESTGetTicker returns ticker info for a given currency, exchange and
// asset type
func RESTGetTicker(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
currency := vars["currency"]
exchange := vars["exchangeName"]
assetType := vars["assetType"]
if assetType == "" {
assetType = ticker.Spot
}
response, err := GetSpecificTicker(currency, exchange, assetType)
if err != nil {
log.Printf("Failed to fetch ticker for %s currency: %s\n", exchange,
currency)
return
}
err = RESTfulJSONResponse(w, r, response)
if err != nil {
RESTfulError(r.Method, err)
}
}
// GetAllActiveTickers returns all enabled exchange tickers
func GetAllActiveTickers() []EnabledExchangeCurrencies {
var tickerData []EnabledExchangeCurrencies
for _, individualBot := range bot.exchanges {
if individualBot != nil && individualBot.IsEnabled() {
var individualExchange EnabledExchangeCurrencies
exchangeName := individualBot.GetName()
individualExchange.ExchangeName = exchangeName
currencies := individualBot.GetEnabledCurrencies()
for _, x := range currencies {
currency := x
assetTypes, err := exchange.GetExchangeAssetTypes(exchangeName)
if err != nil {
log.Printf("failed to get %s exchange asset types. Error: %s",
exchangeName, err)
continue
}
var tickerPrice ticker.Price
if len(assetTypes) > 1 {
for y := range assetTypes {
tickerPrice, err = individualBot.GetTickerPrice(currency,
assetTypes[y])
}
} else {
tickerPrice, err = individualBot.GetTickerPrice(currency,
assetTypes[0])
}
if err != nil {
log.Printf("failed to get %s %s ticker. Error: %s",
currency.Pair().String(),
exchangeName,
err)
continue
}
individualExchange.ExchangeValues = append(
individualExchange.ExchangeValues, tickerPrice,
)
}
tickerData = append(tickerData, individualExchange)
}
}
return tickerData
}
// RESTGetAllActiveTickers returns all active tickers
func RESTGetAllActiveTickers(w http.ResponseWriter, r *http.Request) {
var response AllEnabledExchangeCurrencies
response.Data = GetAllActiveTickers()
err := RESTfulJSONResponse(w, r, response)
if err != nil {
RESTfulError(r.Method, err)
}
}
// GetAllEnabledExchangeAccountInfo returns all the current enabled exchanges
func GetAllEnabledExchangeAccountInfo() AllEnabledExchangeAccounts {
var response AllEnabledExchangeAccounts
for _, individualBot := range bot.exchanges {
if individualBot != nil && individualBot.IsEnabled() {
if !individualBot.GetAuthenticatedAPISupport() {
log.Printf("GetAllEnabledExchangeAccountInfo: Skippping %s due to disabled authenticated API support.", individualBot.GetName())
continue
}
individualExchange, err := individualBot.GetExchangeAccountInfo()
if err != nil {
log.Printf("Error encountered retrieving exchange account info for %s. Error %s",
individualBot.GetName(), err)
continue
}
response.Data = append(response.Data, individualExchange)
}
}
return response
}
// RESTGetAllEnabledAccountInfo via get request returns JSON response of account
// info
func RESTGetAllEnabledAccountInfo(w http.ResponseWriter, r *http.Request) {
response := GetAllEnabledExchangeAccountInfo()
err := RESTfulJSONResponse(w, r, response)
if err != nil {
RESTfulError(r.Method, err)
}
}