From 51d610ea4faedbb2a454b5172037de5634bd3c80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Aasted=20S=C3=B8rensen?= Date: Tue, 20 Oct 2020 11:09:41 +0200 Subject: [PATCH] Add query REST endpoint for buyback module balance --- x/buyback/client/rest/query.go | 37 ++++++++++++++++++++++++++++++++++ x/buyback/module.go | 5 ++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 x/buyback/client/rest/query.go diff --git a/x/buyback/client/rest/query.go b/x/buyback/client/rest/query.go new file mode 100644 index 00000000..c22e30b2 --- /dev/null +++ b/x/buyback/client/rest/query.go @@ -0,0 +1,37 @@ +package rest + +import ( + "fmt" + "net/http" + + "github.com/gorilla/mux" + + "github.com/cosmos/cosmos-sdk/client/context" + "github.com/cosmos/cosmos-sdk/types/rest" + + "github.com/e-money/em-ledger/x/buyback/internal/types" +) + +func RegisterQueryRoutes(cliCtx context.CLIContext, r *mux.Router) { + r.HandleFunc("/buyback/balance", queryBalanceHandlerFn(cliCtx)).Methods("GET") +} + +func queryBalanceHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryBalance) + + cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) + if !ok { + return + } + + res, height, err := cliCtx.QueryWithData(route, nil) + if err != nil { + rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) + return + } + + cliCtx = cliCtx.WithHeight(height) + rest.PostProcessResponse(w, cliCtx, res) + } +} diff --git a/x/buyback/module.go b/x/buyback/module.go index 9ef71cd6..8cca52cc 100644 --- a/x/buyback/module.go +++ b/x/buyback/module.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" "github.com/e-money/em-ledger/x/buyback/client/cli" + "github.com/e-money/em-ledger/x/buyback/client/rest" "github.com/e-money/em-ledger/x/buyback/internal/keeper" "github.com/gorilla/mux" "github.com/spf13/cobra" @@ -49,7 +50,9 @@ func (amb AppModuleBasic) ValidateGenesis(json.RawMessage) error { return nil } -func (amb AppModuleBasic) RegisterRESTRoutes(context.CLIContext, *mux.Router) {} +func (amb AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, r *mux.Router) { + rest.RegisterQueryRoutes(ctx, r) +} func (amb AppModuleBasic) GetTxCmd(*codec.Codec) *cobra.Command { return nil