Skip to content

Commit

Permalink
Merge pull request #144
Browse files Browse the repository at this point in the history
Refactored CLI and REST
  • Loading branch information
bsrinivas8687 authored Aug 12, 2021
2 parents 20a7d37 + e3face4 commit 47b9270
Show file tree
Hide file tree
Showing 36 changed files with 824 additions and 448 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/spf13/cast v1.3.1
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/tendermint/tendermint v0.34.10
github.com/tendermint/tm-db v0.6.4
Expand Down
19 changes: 14 additions & 5 deletions x/deposit/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func queryDeposit() *cobra.Command {
cmd := &cobra.Command{
Use: "deposit",
Use: "deposit [address]",
Short: "Query a deposit",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -31,8 +31,12 @@ func queryDeposit() *cobra.Command {
qc = types.NewQueryServiceClient(ctx)
)

res, err := qc.QueryDeposit(context.Background(),
types.NewQueryDepositRequest(address))
res, err := qc.QueryDeposit(
context.Background(),
types.NewQueryDepositRequest(
address,
),
)
if err != nil {
return err
}
Expand All @@ -42,6 +46,7 @@ func queryDeposit() *cobra.Command {
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

Expand All @@ -64,8 +69,12 @@ func queryDeposits() *cobra.Command {
qc = types.NewQueryServiceClient(ctx)
)

res, err := qc.QueryDeposits(context.Background(),
types.NewQueryDepositsRequest(pagination))
res, err := qc.QueryDeposits(
context.Background(),
types.NewQueryDepositsRequest(
pagination,
),
)
if err != nil {
return err
}
Expand Down
41 changes: 27 additions & 14 deletions x/deposit/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkquery "github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"

Expand All @@ -14,22 +15,23 @@ import (

func queryDeposit(ctx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var (
qc = types.NewQueryServiceClient(ctx)
vars = mux.Vars(r)
)

address, err := sdk.AccAddressFromBech32(vars["address"])
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
if rest.CheckBadRequestError(w, err) {
return
}

var (
qc = types.NewQueryServiceClient(ctx)
res, err := qc.QueryDeposit(
context.Background(),
types.NewQueryDepositRequest(
address,
),
)

res, err := qc.QueryDeposit(context.Background(),
types.NewQueryDepositRequest(address))
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
if rest.CheckInternalServerError(w, err) {
return
}

Expand All @@ -43,10 +45,21 @@ func queryDeposits(ctx client.Context) http.HandlerFunc {
qc = types.NewQueryServiceClient(ctx)
)

res, err := qc.QueryDeposits(context.Background(),
types.NewQueryDepositsRequest(nil))
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
_, page, limit, err := rest.ParseHTTPArgs(r)
if rest.CheckBadRequestError(w, err) {
return
}

res, err := qc.QueryDeposits(
context.Background(),
types.NewQueryDepositsRequest(
&sdkquery.PageRequest{
Offset: uint64(page * limit),
Limit: uint64(limit),
},
),
)
if rest.CheckInternalServerError(w, err) {
return
}

Expand Down
6 changes: 4 additions & 2 deletions x/deposit/client/rest/rest.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package rest

import (
"net/http"

"github.com/cosmos/cosmos-sdk/client"
"github.com/gorilla/mux"
)

func registerQueryRoutes(ctx client.Context, router *mux.Router) {
router.HandleFunc("/deposits", queryDeposits(ctx)).
Methods("GET")
Methods(http.MethodGet)
router.HandleFunc("/deposits/{address}", queryDeposit(ctx)).
Methods("GET")
Methods(http.MethodGet)
}

func RegisterRoutes(ctx client.Context, router *mux.Router) {
Expand Down
2 changes: 1 addition & 1 deletion x/deposit/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/sentinel-official/hub/x/deposit/types"
)

func NewDecodeStore(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string {
func NewStoreDecoder(cdc codec.Marshaler) func(kvA, kvB kv.Pair) string {
return func(kvA, kvB kv.Pair) string {
if bytes.Equal(kvA.Key[:1], types.DepositKeyPrefix) {
var depositA, depositB types.Deposit
Expand Down
43 changes: 43 additions & 0 deletions x/node/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
package cli

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/pflag"

hubtypes "github.com/sentinel-official/hub/types"
)

const (
flagProvider = "provider"
flagPlan = "plan"
flagPrice = "price"
flagRemoteURL = "remote-url"
flagStatus = "status"
)

func GetProvider(flags *pflag.FlagSet) (hubtypes.ProvAddress, error) {
s, err := flags.GetString(flagProvider)
if err != nil {
return nil, err
}
if s == "" {
return nil, nil
}

return hubtypes.ProvAddressFromBech32(s)
}

func GetPrice(flags *pflag.FlagSet) (sdk.Coins, error) {
s, err := flags.GetString(flagPrice)
if err != nil {
return nil, err
}
if s == "" {
return nil, nil
}

return sdk.ParseCoinsNormalized(s)
}

func GetStatus(flags *pflag.FlagSet) (hubtypes.Status, error) {
s, err := flags.GetString(flagStatus)
if err != nil {
return hubtypes.Unspecified, err
}
if s == "" {
return hubtypes.Unspecified, nil
}

return hubtypes.StatusFromString(s), nil
}
65 changes: 38 additions & 27 deletions x/node/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func queryNode() *cobra.Command {
cmd := &cobra.Command{
Use: "node",
Use: "node [address]",
Short: "Query a node",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -31,8 +31,12 @@ func queryNode() *cobra.Command {
qc = types.NewQueryServiceClient(ctx)
)

res, err := qc.QueryNode(context.Background(),
types.NewQueryNodeRequest(address))
res, err := qc.QueryNode(
context.Background(),
types.NewQueryNodeRequest(
address,
),
)
if err != nil {
return err
}
Expand All @@ -56,7 +60,7 @@ func queryNodes() *cobra.Command {
return err
}

provider, err := cmd.Flags().GetString(flagProvider)
provider, err := GetProvider(cmd.Flags())
if err != nil {
return err
}
Expand All @@ -66,7 +70,7 @@ func queryNodes() *cobra.Command {
return err
}

s, err := cmd.Flags().GetString(flagStatus)
status, err := GetStatus(cmd.Flags())
if err != nil {
return err
}
Expand All @@ -77,42 +81,47 @@ func queryNodes() *cobra.Command {
}

var (
status = hubtypes.StatusFromString(s)
qc = types.NewQueryServiceClient(ctx)
qc = types.NewQueryServiceClient(ctx)
)

if len(provider) > 0 {
address, err := hubtypes.ProvAddressFromBech32(provider)
if err != nil {
return err
}

res, err := qc.QueryNodesForProvider(context.Background(),
types.NewQueryNodesForProviderRequest(address, status, pagination))
if provider != nil {
res, err := qc.QueryNodesForProvider(
context.Background(),
types.NewQueryNodesForProviderRequest(
provider,
status,
pagination,
),
)
if err != nil {
return err
}

return ctx.PrintProto(res)
} else if plan > 0 {
return nil
} else {
res, err := qc.QueryNodes(context.Background(),
types.NewQueryNodesRequest(status, pagination))
if err != nil {
return err
}
}

return ctx.PrintProto(res)
res, err := qc.QueryNodes(
context.Background(),
types.NewQueryNodesRequest(
status,
pagination,
),
)
if err != nil {
return err
}

return ctx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "nodes")
cmd.Flags().String(flagProvider, "", "provider address")
cmd.Flags().Uint64(flagPlan, 0, "subscription plan ID")
cmd.Flags().String(flagStatus, "", "status")
cmd.Flags().String(flagProvider, "", "filter by provider address")
cmd.Flags().Uint64(flagPlan, 0, "filter by plan identity")
cmd.Flags().String(flagStatus, "", "filter by status")

return cmd
}
Expand All @@ -131,8 +140,10 @@ func queryParams() *cobra.Command {
qc = types.NewQueryServiceClient(ctx)
)

res, err := qc.QueryParams(context.Background(),
types.NewQueryParamsRequest())
res, err := qc.QueryParams(
context.Background(),
types.NewQueryParamsRequest(),
)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 47b9270

Please sign in to comment.