From 17b68fb365253ebe76ceb3a7d08237b3a946587b Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 25 Mar 2020 13:20:29 -0400 Subject: [PATCH] Prep PostProcessResponseBare and PostProcessResponse --- types/rest/rest.go | 68 ++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/types/rest/rest.go b/types/rest/rest.go index 2589175862..ca79eb7175 100644 --- a/types/rest/rest.go +++ b/types/rest/rest.go @@ -229,21 +229,31 @@ func ParseQueryHeightOrReturnBadRequest(w http.ResponseWriter, cliCtx context.CL // PostProcessResponseBare post processes a body similar to PostProcessResponse // except it does not wrap the body and inject the height. -func PostProcessResponseBare(w http.ResponseWriter, cliCtx context.CLIContext, body interface{}) { +func PostProcessResponseBare(w http.ResponseWriter, ctx context.CLIContext, body interface{}) { var ( resp []byte err error ) + // 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 + } + switch b := body.(type) { case []byte: resp = b default: - if cliCtx.Indent { - resp, err = cliCtx.Codec.MarshalJSONIndent(body, "", " ") - } else { - resp, err = cliCtx.Codec.MarshalJSON(body) + resp, err = marshaler.MarshalJSON(body) + + if ctx.Indent && err == nil { + resp, err = sdk.MarshalIndentFromJSON(resp) } if err != nil { @@ -259,24 +269,36 @@ func PostProcessResponseBare(w http.ResponseWriter, cliCtx context.CLIContext, b // PostProcessResponse performs post processing for a REST response. The result // returned to clients will contain two fields, the height at which the resource // was queried at and the original result. -func PostProcessResponse(w http.ResponseWriter, cliCtx context.CLIContext, resp interface{}) { - var result []byte +func PostProcessResponse(w http.ResponseWriter, ctx context.CLIContext, resp interface{}) { + var ( + result []byte + err error + ) - if cliCtx.Height < 0 { + if ctx.Height < 0 { WriteErrorResponse(w, http.StatusInternalServerError, fmt.Errorf("negative height in response").Error()) return } + // 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 + } + switch res := resp.(type) { case []byte: result = res default: - var err error - if cliCtx.Indent { - result, err = cliCtx.Codec.MarshalJSONIndent(resp, "", " ") - } else { - result, err = cliCtx.Codec.MarshalJSON(resp) + result, err = marshaler.MarshalJSON(resp) + + if ctx.Indent && err == nil { + result, err = sdk.MarshalIndentFromJSON(result) } if err != nil { @@ -285,17 +307,11 @@ func PostProcessResponse(w http.ResponseWriter, cliCtx context.CLIContext, resp } } - wrappedResp := NewResponseWithHeight(cliCtx.Height, result) + wrappedResp := NewResponseWithHeight(ctx.Height, result) - var ( - output []byte - err error - ) - - if cliCtx.Indent { - output, err = cliCtx.Codec.MarshalJSONIndent(wrappedResp, "", " ") - } else { - output, err = cliCtx.Codec.MarshalJSON(wrappedResp) + output, err := marshaler.MarshalJSON(wrappedResp) + if ctx.Indent && err == nil { + output, err = sdk.MarshalIndentFromJSON(output) } if err != nil { @@ -312,10 +328,12 @@ func PostProcessResponse(w http.ResponseWriter, cliCtx context.CLIContext, resp // 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" { continue } + var value string value, err = url.QueryUnescape(values[0]) if err != nil { @@ -326,13 +344,17 @@ func ParseHTTPArgsWithLimit(r *http.Request, defaultLimit int) (tags []string, p switch key { case types.TxHeightKey: tag = fmt.Sprintf("%s=%s", key, value) + case TxMinHeightKey: tag = fmt.Sprintf("%s>=%s", types.TxHeightKey, value) + case TxMaxHeightKey: tag = fmt.Sprintf("%s<=%s", types.TxHeightKey, value) + default: tag = fmt.Sprintf("%s='%s'", key, value) } + tags = append(tags, tag) }