From 0c82bbdcbe051db94d05af035a3b42d7bdc7fff2 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Tue, 14 Dec 2021 16:25:26 +0100 Subject: [PATCH] Add multi-tickers endpoint --- server/public.go | 35 ++++++++++++++++++++++++++++++++++- server/public_test.go | 20 +++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/server/public.go b/server/public.go index 1d192141..88666553 100644 --- a/server/public.go +++ b/server/public.go @@ -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"` } diff --git a/server/public_test.go b/server/public_test.go index 7e04f748..cb909251 100644 --- a/server/public_test.go +++ b/server/public_test.go @@ -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¤cy=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¤cy=usd"),