From d480bbbebf1ebb08ebbc30921faa4e6348f0e751 Mon Sep 17 00:00:00 2001 From: Intizar Date: Tue, 16 Jul 2024 17:49:47 +0900 Subject: [PATCH 1/2] get multiple data feed by symbols --- node/pkg/dal/api/controller.go | 34 ++++++++++++------- node/pkg/dal/api/route.go | 2 +- node/pkg/dal/tests/api_test.go | 4 +-- node/pkg/dal/utils/initializer/initializer.go | 2 +- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/node/pkg/dal/api/controller.go b/node/pkg/dal/api/controller.go index 6e73423a0..f9906985d 100644 --- a/node/pkg/dal/api/controller.go +++ b/node/pkg/dal/api/controller.go @@ -3,6 +3,7 @@ package api import ( "context" "errors" + "fmt" "strings" "bisonai.com/orakl/node/pkg/common/types" @@ -186,29 +187,36 @@ func getLatestFeeds(c *fiber.Ctx) error { return c.JSON(result) } -func getLatestFeed(c *fiber.Ctx) error { +func getLatestMultipleFeeds(c *fiber.Ctx) error { controller, ok := c.Locals("apiController").(*Controller) if !ok { return errors.New("api controller not found") } - symbol := c.Params("symbol") + symbolsStr := c.Params("symbols") - if symbol == "" { + if symbolsStr == "" { return errors.New("invalid symbol: empty symbol") } - if !strings.Contains(symbol, "-") { - return errors.New("symbol should be in {BASE}-{QUOTE} format") - } - if !strings.Contains(symbol, "test") { - symbol = strings.ToUpper(symbol) - } + symbols := strings.Split(symbolsStr, ",") + results := make([]*dalcommon.OutgoingSubmissionData, len(symbols)) + for i, symbol := range symbols { + if !strings.Contains(symbol, "-") { + return fmt.Errorf("wrong symbol format: %s, symbol should be in {BASE}-{QUOTE} format", symbol) + } - result, err := controller.Collector.GetLatestData(symbol) - if err != nil { - return err + if !strings.Contains(symbol, "test") { + symbol = strings.ToUpper(symbol) + } + + result, err := controller.Collector.GetLatestData(symbol) + if err != nil { + return err + } + + results[i] = result } - return c.JSON(*result) + return c.JSON(results) } diff --git a/node/pkg/dal/api/route.go b/node/pkg/dal/api/route.go index 4ec21b703..a86c86a84 100644 --- a/node/pkg/dal/api/route.go +++ b/node/pkg/dal/api/route.go @@ -10,6 +10,6 @@ func Routes(router fiber.Router) { api.Get("/symbols", getSymbols) api.Get("/latest-data-feeds/all", getLatestFeeds) - api.Get("/latest-data-feeds/:symbol", getLatestFeed) + api.Get("/latest-data-feeds/:symbols", getLatestMultipleFeeds) api.Get("/ws", websocket.New(HandleWebsocket)) } diff --git a/node/pkg/dal/tests/api_test.go b/node/pkg/dal/tests/api_test.go index 7a195545c..43b6f2efb 100644 --- a/node/pkg/dal/tests/api_test.go +++ b/node/pkg/dal/tests/api_test.go @@ -136,7 +136,7 @@ func TestApiGetLatest(t *testing.T) { time.Sleep(10 * time.Millisecond) - result, err := request.Request[common.OutgoingSubmissionData](request.WithEndpoint("http://localhost:8090/api/v1/dal/latest-data-feeds/test-aggregate"), request.WithHeaders(map[string]string{"X-API-Key": testItems.ApiKey})) + result, err := request.Request[[]common.OutgoingSubmissionData](request.WithEndpoint("http://localhost:8090/api/v1/dal/latest-data-feeds/test-aggregate"), request.WithHeaders(map[string]string{"X-API-Key": testItems.ApiKey})) if err != nil { t.Fatalf("error getting latest data: %v", err) } @@ -144,7 +144,7 @@ func TestApiGetLatest(t *testing.T) { if err != nil { t.Fatalf("error converting sample submission data to outgoing data: %v", err) } - assert.Equal(t, *expected, result) + assert.Equal(t, *expected, result[0]) } func TestApiWebsocket(t *testing.T) { diff --git a/node/pkg/dal/utils/initializer/initializer.go b/node/pkg/dal/utils/initializer/initializer.go index 5a8747467..d92b6350d 100644 --- a/node/pkg/dal/utils/initializer/initializer.go +++ b/node/pkg/dal/utils/initializer/initializer.go @@ -46,7 +46,7 @@ func Setup(ctx context.Context, apiController *api.Controller, keyCache *keycach app := fiber.New(fiber.Config{ AppName: "Data Availability Layer API 0.1.0", - EnablePrintRoutes: false, + EnablePrintRoutes: true, ErrorHandler: CustomErrorHandler, }) From 5bf266667a54d1a1a3a1064017021f0a448e882a Mon Sep 17 00:00:00 2001 From: Intizar Date: Tue, 16 Jul 2024 19:24:48 +0900 Subject: [PATCH 2/2] rename functions and add extra symbol check condition --- node/pkg/dal/api/controller.go | 8 ++++++-- node/pkg/dal/api/route.go | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/node/pkg/dal/api/controller.go b/node/pkg/dal/api/controller.go index f9906985d..a72c5f912 100644 --- a/node/pkg/dal/api/controller.go +++ b/node/pkg/dal/api/controller.go @@ -177,7 +177,7 @@ func getSymbols(c *fiber.Ctx) error { return c.JSON(result) } -func getLatestFeeds(c *fiber.Ctx) error { +func getAllLatestFeeds(c *fiber.Ctx) error { controller, ok := c.Locals("apiController").(*Controller) if !ok { return errors.New("api controller not found") @@ -187,7 +187,7 @@ func getLatestFeeds(c *fiber.Ctx) error { return c.JSON(result) } -func getLatestMultipleFeeds(c *fiber.Ctx) error { +func getLatestFeeds(c *fiber.Ctx) error { controller, ok := c.Locals("apiController").(*Controller) if !ok { return errors.New("api controller not found") @@ -206,6 +206,10 @@ func getLatestMultipleFeeds(c *fiber.Ctx) error { return fmt.Errorf("wrong symbol format: %s, symbol should be in {BASE}-{QUOTE} format", symbol) } + if symbol == "" { + continue + } + if !strings.Contains(symbol, "test") { symbol = strings.ToUpper(symbol) } diff --git a/node/pkg/dal/api/route.go b/node/pkg/dal/api/route.go index a86c86a84..01f7929a4 100644 --- a/node/pkg/dal/api/route.go +++ b/node/pkg/dal/api/route.go @@ -9,7 +9,7 @@ func Routes(router fiber.Router) { api := router.Group("/dal") api.Get("/symbols", getSymbols) - api.Get("/latest-data-feeds/all", getLatestFeeds) - api.Get("/latest-data-feeds/:symbols", getLatestMultipleFeeds) + api.Get("/latest-data-feeds/all", getAllLatestFeeds) + api.Get("/latest-data-feeds/:symbols", getLatestFeeds) api.Get("/ws", websocket.New(HandleWebsocket)) }