Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get multiple data feed by symbols #1820

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions node/pkg/dal/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"errors"
"fmt"
"strings"

"bisonai.com/orakl/node/pkg/common/types"
Expand Down Expand Up @@ -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)
}

Intizar-T marked this conversation as resolved.
Show resolved Hide resolved
result, err := controller.Collector.GetLatestData(symbol)
if err != nil {
return err
}
nick-bisonai marked this conversation as resolved.
Show resolved Hide resolved

results[i] = result
}

return c.JSON(*result)
return c.JSON(results)
Intizar-T marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion node/pkg/dal/api/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 2 additions & 2 deletions node/pkg/dal/tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ 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)
}
expected, err := testItems.Controller.Collector.IncomingDataToOutgoingData(ctx, *sampleSubmissionData)
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])
Intizar-T marked this conversation as resolved.
Show resolved Hide resolved
}

func TestApiWebsocket(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion node/pkg/dal/utils/initializer/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Intizar-T marked this conversation as resolved.
Show resolved Hide resolved
ErrorHandler: CustomErrorHandler,
})

Expand Down