Skip to content

Commit

Permalink
Fix rest and cli client for instance moduel
Browse files Browse the repository at this point in the history
  • Loading branch information
krhubert committed Feb 12, 2020
1 parent e42ed83 commit 92ff3ac
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
3 changes: 2 additions & 1 deletion x/instance/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func GetCmdGetInstance(queryRoute string, cdc *codec.Codec) *cobra.Command {
return &cobra.Command{
Use: "get",
Short: "get",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryGetInstance), nil)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", queryRoute, types.QueryGetInstance, args[0]), nil)
if err != nil {
fmt.Printf("could not get instance\n%s\n", err.Error())
return nil
Expand Down
56 changes: 49 additions & 7 deletions x/instance/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,69 @@ import (

func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(
"/ownership/get",
queryHandlerFn(cliCtx, types.QueryGetInstance),
"/instance/get/{hash}",
queryGetHandlerFn(cliCtx),
).Methods(http.MethodGet)
r.HandleFunc(
"/ownership/list",
queryHandlerFn(cliCtx, types.QueryListInstances),
"/instance/list",
queryListHandlerFn(cliCtx),
).Methods(http.MethodGet)
r.HandleFunc(
"/instance/parameters",
queryHandlerFn(cliCtx, types.QuerierRoute),
queryParametersHandlerFn(cliCtx),
).Methods("GET")
}

func queryHandlerFn(cliCtx context.CLIContext, path string) http.HandlerFunc {
func queryGetHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryGetInstance, vars["hash"])

res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}

func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryListInstances)

res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}

func queryParametersHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, path)
route := fmt.Sprintf("custom/%s/parameters", types.QuerierRoute)

res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions x/instance/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ func (k Keeper) List(ctx sdk.Context, f *api.ListInstanceRequest_Filter) ([]*ins
var items []*instance.Instance

for iter.Valid() {
var item *instance.Instance
var item instance.Instance
if err := k.cdc.UnmarshalBinaryLengthPrefixed(iter.Value(), &item); err != nil {
return nil, err
}
if f == nil || f.ServiceHash.IsZero() || item.ServiceHash.Equal(f.ServiceHash) {
items = append(items, item)
items = append(items, &item)
}
iter.Next()
}
Expand Down

0 comments on commit 92ff3ac

Please sign in to comment.