Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Signing Infos Endpoint #4292

Merged
merged 9 commits into from
May 9, 2019
Prev Previous commit
Implement and use ParseHTTPArgsWithLimit
  • Loading branch information
alexanderbez committed May 9, 2019
commit 7b0231151738e8ff2472d99a35b8015f6b0ef28f
15 changes: 11 additions & 4 deletions types/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,10 @@ func PostProcessResponse(w http.ResponseWriter, cdc *codec.Codec, response inter
_, _ = w.Write(output)
}

// ParseHTTPArgs parses the request's URL and returns a slice containing all arguments pairs.
// It separates page and limit used for pagination
func ParseHTTPArgs(r *http.Request) (tags []string, page, limit int, err error) {
// ParseHTTPArgsWithLimit parses the request's URL and returns a slice containing
// all arguments pairs. It separates page and limit used for pagination where a
// default limit can be provided.
func ParseHTTPArgsWithLimit(r *http.Request, defaultLimit int) (tags []string, page, limit int, err error) {
tags = make([]string, 0, len(r.Form))
for key, values := range r.Form {
if key == "page" || key == "limit" {
Expand Down Expand Up @@ -258,7 +259,7 @@ func ParseHTTPArgs(r *http.Request) (tags []string, page, limit int, err error)

limitStr := r.FormValue("limit")
if limitStr == "" {
limit = DefaultLimit
limit = defaultLimit
} else {
limit, err = strconv.Atoi(limitStr)
if err != nil {
Expand All @@ -270,3 +271,9 @@ func ParseHTTPArgs(r *http.Request) (tags []string, page, limit int, err error)

return tags, page, limit, nil
}

// ParseHTTPArgs parses the request's URL and returns a slice containing all
// arguments pairs. It separates page and limit used for pagination.
func ParseHTTPArgs(r *http.Request) (tags []string, page, limit int, err error) {
return ParseHTTPArgsWithLimit(r, DefaultLimit)
}
8 changes: 1 addition & 7 deletions x/slashing/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,12 @@ func signingInfoHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.Hand
// 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.ParseHTTPArgs(r)
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

// Override default limit if it wasn't provided as the querier will set
// it to the correct default limit.
if l := r.FormValue("limit"); l == "" {
limit = 0
}

params := slashing.NewQuerySigningInfosParams(page, limit)
bz, err := cdc.MarshalJSON(params)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions x/staking/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,12 @@ func delegatorValidatorHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) ht
// HTTP request handler to query list of validators
func validatorsHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, page, limit, err := rest.ParseHTTPArgs(r)
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

// override default limit if it wasn't provided
if l := r.FormValue("limit"); l == "" {
limit = 0
}

status := r.FormValue("status")
if status == "" {
status = sdk.BondStatusBonded
Expand Down