Skip to content

Commit

Permalink
Merge pull request #68 from celenium-io/feature/enum-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Dec 9, 2023
2 parents 2e7aa64 + a450e6f commit 0827b30
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 6 deletions.
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

0 comments on commit 0827b30

Please sign in to comment.