Add option to indent response

This commit is contained in:
Federico Kunze 2018-10-15 10:41:37 +02:00
parent 63f8669c4a
commit 8e1d0fbac0
2 changed files with 20 additions and 33 deletions

View File

@ -756,8 +756,8 @@ func TestProposalsQuery(t *testing.T) {
require.Len(t, deposits, 2)
deposit = getDeposit(t, port, proposalID2, addr)
require.Equal(t, deposit, deposits[0])
deposit = getDeposit(t, port, proposalID2, addr2)
require.Equal(t, deposit, deposits[1])
deposit2 := getDeposit(t, port, proposalID2, addr2)
require.Equal(t, deposit2, deposits[1])
deposits = getDeposits(t, port, proposalID3)
require.Len(t, deposits, 1)

View File

@ -31,12 +31,12 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec)
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits", RestProposalID), depositHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), voteHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc("/gov/proposals", queryProposalsWithParameterFn(cdc)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}", RestProposalID), queryProposalHandlerFn(cdc)).Methods("GET")
r.HandleFunc("/gov/proposals", queryProposalsWithParameterFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}", RestProposalID), queryProposalHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits", RestProposalID), queryDepositsHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits/{%s}", RestProposalID, RestDepositer), queryDepositHandlerFn(cdc)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), queryVotesOnProposalHandlerFn(cdc)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes/{%s}", RestProposalID, RestVoter), queryVoteHandlerFn(cdc)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits/{%s}", RestProposalID, RestDepositer), queryDepositHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), queryVotesOnProposalHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes/{%s}", RestProposalID, RestVoter), queryVoteHandlerFn(cdc, cliCtx)).Methods("GET")
}
type postProposalReq struct {
@ -163,7 +163,7 @@ func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc
}
}
func queryProposalHandlerFn(cdc *codec.Codec) http.HandlerFunc {
func queryProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
@ -179,8 +179,6 @@ func queryProposalHandlerFn(cdc *codec.Codec) http.HandlerFunc {
return
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
params := gov.QueryProposalParams{
ProposalID: proposalID,
}
@ -197,7 +195,7 @@ func queryProposalHandlerFn(cdc *codec.Codec) http.HandlerFunc {
return
}
w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
@ -227,12 +225,11 @@ func queryDepositsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
func queryDepositHandlerFn(cdc *codec.Codec) http.HandlerFunc {
func queryDepositHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
@ -262,8 +259,6 @@ func queryDepositHandlerFn(cdc *codec.Codec) http.HandlerFunc {
return
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
params := gov.QueryDepositParams{
ProposalID: proposalID,
Depositer: depositerAddr,
@ -295,11 +290,11 @@ func queryDepositHandlerFn(cdc *codec.Codec) http.HandlerFunc {
return
}
w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
func queryVoteHandlerFn(cdc *codec.Codec) http.HandlerFunc {
func queryVoteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
@ -329,8 +324,6 @@ func queryVoteHandlerFn(cdc *codec.Codec) http.HandlerFunc {
return
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
params := gov.QueryVoteParams{
Voter: voterAddr,
ProposalID: proposalID,
@ -365,12 +358,12 @@ func queryVoteHandlerFn(cdc *codec.Codec) http.HandlerFunc {
utils.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
// todo: Split this functionality into helper functions to remove the above
func queryVotesOnProposalHandlerFn(cdc *codec.Codec) http.HandlerFunc {
func queryVotesOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
@ -386,8 +379,6 @@ func queryVotesOnProposalHandlerFn(cdc *codec.Codec) http.HandlerFunc {
return
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
params := gov.QueryVotesParams{
ProposalID: proposalID,
}
@ -403,12 +394,12 @@ func queryVotesOnProposalHandlerFn(cdc *codec.Codec) http.HandlerFunc {
return
}
w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
// todo: Split this functionality into helper functions to remove the above
func queryProposalsWithParameterFn(cdc *codec.Codec) http.HandlerFunc {
func queryProposalsWithParameterFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bechVoterAddr := r.URL.Query().Get(RestVoter)
bechDepositerAddr := r.URL.Query().Get(RestDepositer)
@ -460,20 +451,18 @@ func queryProposalsWithParameterFn(cdc *codec.Codec) http.HandlerFunc {
return
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, err := cliCtx.QueryWithData("custom/gov/proposals", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
// todo: Split this functionality into helper functions to remove the above
func queryTallyOnProposalHandlerFn(cdc *codec.Codec) http.HandlerFunc {
func queryTallyOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]
@ -491,8 +480,6 @@ func queryTallyOnProposalHandlerFn(cdc *codec.Codec) http.HandlerFunc {
return
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
params := gov.QueryTallyParams{
ProposalID: proposalID,
}
@ -510,6 +497,6 @@ func queryTallyOnProposalHandlerFn(cdc *codec.Codec) http.HandlerFunc {
return
}
w.Write(res)
utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}