client: rename CliContext to Context (#6290)
* Refactor CliContext as Context * Fix lint issues * Fix goimports * Fix gov tests * Resolved ci-lint issues * Add changelog * Rename cliCtx to clientCtx * Fix mocks and routes * Add changelog * Update changelog * Apply suggestions from code review Co-authored-by: Alessio Treglia <alessio@tendermint.com> * merge client/rpc/ro{ot,utes}.go * Update docs * client/rpc: remove redundant client/rpc.RegisterRPCRoutes * regenerate mocks * Update ADRs Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Alessio Treglia
Federico Kunze
mergify[bot]
parent
654b2fdd10
commit
39f53ac22f
+9
-9
@@ -14,7 +14,7 @@ import (
|
||||
|
||||
"github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
@@ -229,32 +229,32 @@ func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEm
|
||||
|
||||
// ParseQueryHeightOrReturnBadRequest sets the height to execute a query if set by the http request.
|
||||
// It returns false if there was an error parsing the height.
|
||||
func ParseQueryHeightOrReturnBadRequest(w http.ResponseWriter, cliCtx context.CLIContext, r *http.Request) (context.CLIContext, bool) {
|
||||
func ParseQueryHeightOrReturnBadRequest(w http.ResponseWriter, clientCtx client.Context, r *http.Request) (client.Context, bool) {
|
||||
heightStr := r.FormValue("height")
|
||||
if heightStr != "" {
|
||||
height, err := strconv.ParseInt(heightStr, 10, 64)
|
||||
if CheckBadRequestError(w, err) {
|
||||
return cliCtx, false
|
||||
return clientCtx, false
|
||||
}
|
||||
|
||||
if height < 0 {
|
||||
WriteErrorResponse(w, http.StatusBadRequest, "height must be equal or greater than zero")
|
||||
return cliCtx, false
|
||||
return clientCtx, false
|
||||
}
|
||||
|
||||
if height > 0 {
|
||||
cliCtx = cliCtx.WithHeight(height)
|
||||
clientCtx = clientCtx.WithHeight(height)
|
||||
}
|
||||
} else {
|
||||
cliCtx = cliCtx.WithHeight(0)
|
||||
clientCtx = clientCtx.WithHeight(0)
|
||||
}
|
||||
|
||||
return cliCtx, true
|
||||
return clientCtx, true
|
||||
}
|
||||
|
||||
// PostProcessResponseBare post processes a body similar to PostProcessResponse
|
||||
// except it does not wrap the body and inject the height.
|
||||
func PostProcessResponseBare(w http.ResponseWriter, ctx context.CLIContext, body interface{}) {
|
||||
func PostProcessResponseBare(w http.ResponseWriter, ctx client.Context, body interface{}) {
|
||||
var (
|
||||
resp []byte
|
||||
err error
|
||||
@@ -293,7 +293,7 @@ func PostProcessResponseBare(w http.ResponseWriter, ctx context.CLIContext, body
|
||||
// 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, ctx context.CLIContext, resp interface{}) {
|
||||
func PostProcessResponse(w http.ResponseWriter, ctx client.Context, resp interface{}) {
|
||||
var (
|
||||
result []byte
|
||||
err error
|
||||
|
||||
+19
-19
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/tendermint/tendermint/crypto"
|
||||
"github.com/tendermint/tendermint/crypto/secp256k1"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
"github.com/cosmos/cosmos-sdk/types"
|
||||
@@ -147,25 +147,25 @@ func TestParseQueryHeight(t *testing.T) {
|
||||
name string
|
||||
req *http.Request
|
||||
w http.ResponseWriter
|
||||
cliCtx context.CLIContext
|
||||
clientCtx client.Context
|
||||
expectedHeight int64
|
||||
expectedOk bool
|
||||
}{
|
||||
{"no height", req0, httptest.NewRecorder(), context.CLIContext{}, emptyHeight, true},
|
||||
{"height", req1, httptest.NewRecorder(), context.CLIContext{}, height, true},
|
||||
{"invalid height", req2, httptest.NewRecorder(), context.CLIContext{}, emptyHeight, false},
|
||||
{"negative height", req3, httptest.NewRecorder(), context.CLIContext{}, emptyHeight, false},
|
||||
{"no height", req0, httptest.NewRecorder(), client.Context{}, emptyHeight, true},
|
||||
{"height", req1, httptest.NewRecorder(), client.Context{}, height, true},
|
||||
{"invalid height", req2, httptest.NewRecorder(), client.Context{}, emptyHeight, false},
|
||||
{"negative height", req3, httptest.NewRecorder(), client.Context{}, emptyHeight, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(tt.w, tt.cliCtx, tt.req)
|
||||
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(tt.w, tt.clientCtx, tt.req)
|
||||
if tt.expectedOk {
|
||||
require.True(t, ok)
|
||||
require.Equal(t, tt.expectedHeight, cliCtx.Height)
|
||||
require.Equal(t, tt.expectedHeight, clientCtx.Height)
|
||||
} else {
|
||||
require.False(t, ok)
|
||||
require.Empty(t, tt.expectedHeight, cliCtx.Height)
|
||||
require.Empty(t, tt.expectedHeight, clientCtx.Height)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -187,7 +187,7 @@ func TestProcessPostResponse(t *testing.T) {
|
||||
|
||||
// setup
|
||||
viper.Set(flags.FlagOffline, true)
|
||||
ctx := context.NewCLIContext()
|
||||
ctx := client.NewContext()
|
||||
height := int64(194423)
|
||||
|
||||
privKey := secp256k1.GenPrivKey()
|
||||
@@ -312,11 +312,11 @@ func TestPostProcessResponseBare(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// write bytes
|
||||
ctx := context.CLIContext{}
|
||||
clientCtx := client.Context{}
|
||||
w := httptest.NewRecorder()
|
||||
bs := []byte("text string")
|
||||
|
||||
rest.PostProcessResponseBare(w, ctx, bs)
|
||||
rest.PostProcessResponseBare(w, clientCtx, bs)
|
||||
|
||||
res := w.Result() //nolint:bodyclose
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
@@ -328,14 +328,14 @@ func TestPostProcessResponseBare(t *testing.T) {
|
||||
require.Equal(t, "text string", string(got))
|
||||
|
||||
// write struct and indent response
|
||||
ctx = context.CLIContext{Indent: true}.WithCodec(codec.New())
|
||||
clientCtx = client.Context{Indent: true}.WithCodec(codec.New())
|
||||
w = httptest.NewRecorder()
|
||||
data := struct {
|
||||
X int `json:"x"`
|
||||
S string `json:"s"`
|
||||
}{X: 10, S: "test"}
|
||||
|
||||
rest.PostProcessResponseBare(w, ctx, data)
|
||||
rest.PostProcessResponseBare(w, clientCtx, data)
|
||||
|
||||
res = w.Result() //nolint:bodyclose
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
@@ -350,14 +350,14 @@ func TestPostProcessResponseBare(t *testing.T) {
|
||||
}`, string(got))
|
||||
|
||||
// write struct, don't indent response
|
||||
ctx = context.CLIContext{Indent: false}.WithCodec(codec.New())
|
||||
clientCtx = client.Context{Indent: false}.WithCodec(codec.New())
|
||||
w = httptest.NewRecorder()
|
||||
data = struct {
|
||||
X int `json:"x"`
|
||||
S string `json:"s"`
|
||||
}{X: 10, S: "test"}
|
||||
|
||||
rest.PostProcessResponseBare(w, ctx, data)
|
||||
rest.PostProcessResponseBare(w, clientCtx, data)
|
||||
|
||||
res = w.Result() //nolint:bodyclose
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
@@ -369,11 +369,11 @@ func TestPostProcessResponseBare(t *testing.T) {
|
||||
require.Equal(t, `{"x":"10","s":"test"}`, string(got))
|
||||
|
||||
// test marshalling failure
|
||||
ctx = context.CLIContext{Indent: false}.WithCodec(codec.New())
|
||||
clientCtx = client.Context{Indent: false}.WithCodec(codec.New())
|
||||
w = httptest.NewRecorder()
|
||||
data2 := badJSONMarshaller{}
|
||||
|
||||
rest.PostProcessResponseBare(w, ctx, data2)
|
||||
rest.PostProcessResponseBare(w, clientCtx, data2)
|
||||
|
||||
res = w.Result() //nolint:bodyclose
|
||||
require.Equal(t, http.StatusInternalServerError, res.StatusCode)
|
||||
@@ -395,7 +395,7 @@ func (badJSONMarshaller) MarshalJSON() ([]byte, error) {
|
||||
// asserts that ResponseRecorder returns the expected code and body
|
||||
// runs PostProcessResponse on the objects regular interface and on
|
||||
// the marshalled struct.
|
||||
func runPostProcessResponse(t *testing.T, ctx context.CLIContext, obj interface{}, expectedBody []byte, indent bool) {
|
||||
func runPostProcessResponse(t *testing.T, ctx client.Context, obj interface{}, expectedBody []byte, indent bool) {
|
||||
if indent {
|
||||
ctx.Indent = indent
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user