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

Feature: add endpoint with celenium enumerators #68

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 44 additions & 0 deletions cmd/api/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions cmd/api/docs/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions cmd/api/docs/swagger.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions cmd/api/handler/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ func (handler *ConstantHandler) Get(c echo.Context) error {
}
return c.JSON(http.StatusOK, responses.NewConstants(consts, dm))
}

// Enums godoc
//
// @Summary Get celenium enumerators
// @Description Get celenium enumerators
// @Tags general
// @ID get-enums
// @Produce json
// @Success 200 {object} responses.Enums
// @Router /v1/enums [get]
func (handler *ConstantHandler) Enums(c echo.Context) error {
return c.JSON(http.StatusOK, responses.NewEnums())
}
66 changes: 66 additions & 0 deletions cmd/api/handler/constant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-FileCopyrightText: 2023 PK Lab AG <[email protected]>
// SPDX-License-Identifier: MIT

package handler

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/celenium-io/celestia-indexer/cmd/api/handler/responses"
"github.com/celenium-io/celestia-indexer/internal/storage/mock"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"
)

// ConstantTestSuite -
type ConstantTestSuite struct {
suite.Suite
constants *mock.MockIConstant
denomMetadata *mock.MockIDenomMetadata
address *mock.MockIAddress
echo *echo.Echo
handler *ConstantHandler
ctrl *gomock.Controller
}

// SetupSuite -
func (s *ConstantTestSuite) SetupSuite() {
s.echo = echo.New()
s.echo.Validator = NewCelestiaApiValidator()
s.ctrl = gomock.NewController(s.T())
s.constants = mock.NewMockIConstant(s.ctrl)
s.denomMetadata = mock.NewMockIDenomMetadata(s.ctrl)
s.address = mock.NewMockIAddress(s.ctrl)
}

// TearDownSuite -
func (s *ConstantTestSuite) TearDownSuite() {
s.ctrl.Finish()
s.Require().NoError(s.echo.Shutdown(context.Background()))
}

func TestSuiteConstant_Run(t *testing.T) {
suite.Run(t, new(ConstantTestSuite))
}

func (s *ConstantTestSuite) TestEnums() {
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := s.echo.NewContext(req, rec)
c.SetPath("/enums")

s.Require().NoError(s.handler.Enums(c))
s.Require().Equal(http.StatusOK, rec.Code)

var enums responses.Enums
err := json.NewDecoder(rec.Body).Decode(&enums)
s.Require().NoError(err)
s.Require().Len(enums.EventType, 55)
s.Require().Len(enums.MessageType, 74)
s.Require().Len(enums.Status, 2)
}
15 changes: 15 additions & 0 deletions cmd/api/handler/responses/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package responses

import (
"github.com/celenium-io/celestia-indexer/internal/storage"
"github.com/celenium-io/celestia-indexer/internal/storage/types"
"github.com/goccy/go-json"
"github.com/shopspring/decimal"
)
Expand Down Expand Up @@ -63,3 +64,17 @@ func NewConstants(consts []storage.Constant, denomMetadata []storage.DenomMetada

return response
}

type Enums struct {
Status []string `json:"status"`
MessageType []string `json:"message_type"`
EventType []string `json:"event_type"`
}

func NewEnums() Enums {
return Enums{
Status: types.StatusNames(),
MessageType: types.MsgTypeNames(),
EventType: types.EventTypeNames(),
}
}
1 change: 1 addition & 0 deletions cmd/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ func initHandlers(ctx context.Context, e *echo.Echo, cfg Config, db postgres.Sto
v1.GET("/head", stateHandlers.Head)
constantsHandler := handler.NewConstantHandler(db.Constants, db.DenomMetadata, db.Address)
v1.GET("/constants", constantsHandler.Get)
v1.GET("/enums", constantsHandler.Enums)

searchHandler := handler.NewSearchHandler(db.Address, db.Blocks, db.Namespace, db.Tx)
v1.GET("/search", searchHandler.Search)
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/types/event_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ package types
cancel_unbonding_delegation
)
*/
//go:generate go-enum --marshal --sql --values
//go:generate go-enum --marshal --sql --values --names
type EventType string
68 changes: 67 additions & 1 deletion internal/storage/types/event_type_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/storage/types/msg_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ package types
MsgAcknowledgement,
)
*/
//go:generate go-enum --marshal --sql --values --noprefix
//go:generate go-enum --marshal --sql --values --noprefix --names
type MsgType string
Loading
Loading