Add multi-tickers endpoint

This commit is contained in:
Martin Boehm 2021-12-14 16:25:26 +01:00
parent 6a9852c463
commit 0c82bbdcbe
2 changed files with 53 additions and 2 deletions

View File

@ -190,6 +190,7 @@ func (s *PublicServer) ConnectFullPublicInterface() {
serveMux.HandleFunc(path+"api/v2/feestats/", s.jsonHandler(s.apiFeeStats, apiV2))
serveMux.HandleFunc(path+"api/v2/balancehistory/", s.jsonHandler(s.apiBalanceHistory, apiDefault))
serveMux.HandleFunc(path+"api/v2/tickers/", s.jsonHandler(s.apiTickers, apiV2))
serveMux.HandleFunc(path+"api/v2/multi-tickers/", s.jsonHandler(s.apiMultiTickers, apiV2))
serveMux.HandleFunc(path+"api/v2/tickers-list/", s.jsonHandler(s.apiTickersList, apiV2))
// socket.io interface
serveMux.Handle(path+"socket.io/", s.socketio.GetHandler())
@ -1210,7 +1211,7 @@ func (s *PublicServer) apiTickers(r *http.Request, apiVersion int) (interface{},
timestamp, err := strconv.ParseInt(timestampString, 10, 64)
if err != nil {
return nil, api.NewAPIError("Parameter \"timestamp\" is not a valid Unix timestamp.", true)
return nil, api.NewAPIError("Parameter 'timestamp' is not a valid Unix timestamp.", true)
}
resultTickers, err := s.api.GetFiatRatesForTimestamps([]int64{timestamp}, currencies)
@ -1229,6 +1230,38 @@ func (s *PublicServer) apiTickers(r *http.Request, apiVersion int) (interface{},
return result, nil
}
// apiMultiTickers returns FiatRates ticker prices for the specified comma separated list of timestamps.
func (s *PublicServer) apiMultiTickers(r *http.Request, apiVersion int) (interface{}, error) {
var result []db.ResultTickerAsString
var err error
currency := strings.ToLower(r.URL.Query().Get("currency"))
var currencies []string
if currency != "" {
currencies = []string{currency}
}
if timestampString := r.URL.Query().Get("timestamp"); timestampString != "" {
// Get tickers for specified timestamp
s.metrics.ExplorerViews.With(common.Labels{"action": "api-multi-tickers-date"}).Inc()
timestamps := strings.Split(timestampString, ",")
t := make([]int64, len(timestamps))
for i := range timestamps {
t[i], err = strconv.ParseInt(timestamps[i], 10, 64)
if err != nil {
return nil, api.NewAPIError("Parameter 'timestamp' does not contain a valid Unix timestamp.", true)
}
}
resultTickers, err := s.api.GetFiatRatesForTimestamps(t, currencies)
if err != nil {
return nil, err
}
result = resultTickers.Tickers
} else {
return nil, api.NewAPIError("Parameter 'timestamp' is missing.", true)
}
return result, nil
}
type resultEstimateFeeAsString struct {
Result string `json:"result"`
}

View File

@ -565,7 +565,7 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
status: http.StatusBadRequest,
contentType: "application/json; charset=utf-8",
body: []string{
`{"error":"Parameter \"timestamp\" is not a valid Unix timestamp."}`,
`{"error":"Parameter 'timestamp' is not a valid Unix timestamp."}`,
},
},
{
@ -595,6 +595,24 @@ func httpTestsBitcoinType(t *testing.T, ts *httptest.Server) {
`{"ts":1574344800,"rates":{"eur":7100}}`,
},
},
{
name: "apiMultiFiatRates all currencies",
r: newGetRequest(ts.URL + "/api/v2/multi-tickers?timestamp=1574344800,1574346615"),
status: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: []string{
`[{"ts":1574344800,"rates":{"eur":7100,"usd":7814.5}},{"ts":1574346615,"rates":{"eur":7134.1,"usd":7914.5}}]`,
},
},
{
name: "apiMultiFiatRates get EUR rate",
r: newGetRequest(ts.URL + "/api/v2/multi-tickers?timestamp=1574344800,1574346615&currency=eur"),
status: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: []string{
`[{"ts":1574344800,"rates":{"eur":7100}},{"ts":1574346615,"rates":{"eur":7134.1}}]`,
},
},
{
name: "apiFiatRates get closest rate",
r: newGetRequest(ts.URL + "/api/v2/tickers?timestamp=1357045200&currency=usd"),