Prep QueryBalancesRequestHandlerFn for proto

This commit is contained in:
Aleksandr Bezobchuk 2020-03-25 13:22:29 -04:00
parent 17b68fb365
commit 0e83e328d0
No known key found for this signature in database
GPG Key ID: 7DAC30FBD99879B0
1 changed files with 17 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/bank/types"
@ -14,7 +15,7 @@ import (
// QueryBalancesRequestHandlerFn returns a REST handler that queries for all // QueryBalancesRequestHandlerFn returns a REST handler that queries for all
// account balances or a specific balance by denomination. // account balances or a specific balance by denomination.
func QueryBalancesRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc { func QueryBalancesRequestHandlerFn(ctx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r) vars := mux.Vars(r)
@ -26,7 +27,7 @@ func QueryBalancesRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return return
} }
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r) ctx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, ctx, r)
if !ok { if !ok {
return return
} }
@ -36,6 +37,16 @@ func QueryBalancesRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
route string route string
) )
// TODO: Remove once client-side Protobuf migration has been completed.
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
var marshaler codec.JSONMarshaler
if ctx.Marshaler != nil {
marshaler = ctx.Marshaler
} else {
marshaler = ctx.Codec
}
denom := r.FormValue("denom") denom := r.FormValue("denom")
if denom == "" { if denom == "" {
params = types.NewQueryAllBalancesParams(addr) params = types.NewQueryAllBalancesParams(addr)
@ -45,19 +56,19 @@ func QueryBalancesRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryBalance) route = fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryBalance)
} }
bz, err := cliCtx.Codec.MarshalJSON(params) bz, err := marshaler.MarshalJSON(params)
if err != nil { if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error()) rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return return
} }
res, height, err := cliCtx.QueryWithData(route, bz) res, height, err := ctx.QueryWithData(route, bz)
if err != nil { if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error()) rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return return
} }
cliCtx = cliCtx.WithHeight(height) ctx = ctx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res) rest.PostProcessResponse(w, ctx, res)
} }
} }