cosmos-sdk/x/slashing/client/rest/query.go

102 lines
2.8 KiB
Go
Raw Normal View History

package rest
import (
2018-12-14 11:09:39 -08:00
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/slashing"
)
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
r.HandleFunc(
"/slashing/validators/{validatorPubKey}/signing_info",
signingInfoHandlerFn(cliCtx, cdc),
).Methods("GET")
2018-12-14 11:09:39 -08:00
r.HandleFunc(
"/slashing/signing_infos",
signingInfoHandlerListFn(cliCtx, cdc),
).Methods("GET")
2018-12-14 11:09:39 -08:00
r.HandleFunc(
"/slashing/parameters",
queryParamsHandlerFn(cdc, cliCtx),
).Methods("GET")
}
// http request handler to query signing info
func signingInfoHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pk, err := sdk.GetConsPubKeyBech32(vars["validatorPubKey"])
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params := slashing.NewQuerySigningInfoParams(sdk.ConsAddress(pk.Address()))
2018-08-06 11:11:30 -07:00
bz, err := cdc.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
route := fmt.Sprintf("custom/%s/%s", slashing.QuerierRoute, slashing.QuerySigningInfo)
res, err := cliCtx.QueryWithData(route, bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
// http request handler to query signing info
func signingInfoHandlerListFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
params := slashing.NewQuerySigningInfosParams(page, limit)
bz, err := cdc.MarshalJSON(params)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
route := fmt.Sprintf("custom/%s/%s", slashing.QuerierRoute, slashing.QuerySigningInfos)
res, err := cliCtx.QueryWithData(route, bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
2018-12-14 11:09:39 -08:00
func queryParamsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/parameters", slashing.QuerierRoute)
res, err := cliCtx.QueryWithData(route, nil)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-12-14 11:09:39 -08:00
return
}
2019-02-04 07:48:26 -08:00
rest.PostProcessResponse(w, cdc, res, cliCtx.Indent)
2018-12-14 11:09:39 -08:00
}
}