Prep PostProcessResponseBare and PostProcessResponse

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

View File

@ -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)
}