REST folder restructure & cleanup

This commit is contained in:
Christopher Goes
2018-04-25 16:18:06 +02:00
parent 85a49646eb
commit 56ac948dc6
20 changed files with 69 additions and 59 deletions
@@ -1,4 +1,4 @@
package commands
package cli
import (
"encoding/hex"
@@ -10,20 +10,21 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
auth "github.com/cosmos/cosmos-sdk/x/auth/commands"
auth "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
)
// register REST routes
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, storeName string) {
ctx := context.NewCoreContextFromViper()
r.HandleFunc(
"/accounts/{address}",
QueryAccountRequestHandler(storeName, cdc, auth.GetAccountDecoder(cdc)),
QueryAccountRequestHandler(storeName, cdc, auth.GetAccountDecoder(cdc), ctx),
).Methods("GET")
}
// query accountREST Handler
func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder) func(http.ResponseWriter, *http.Request) {
ctx := context.NewCoreContextFromViper()
func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
addr := vars["address"]
@@ -1,4 +1,4 @@
package commands
package cli
import (
"encoding/hex"
@@ -10,8 +10,8 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
"github.com/cosmos/cosmos-sdk/x/bank"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/bank/client"
)
const (
@@ -48,7 +48,7 @@ func SendTxCmd(cdc *wire.Codec) *cobra.Command {
}
// build and sign the transaction, then broadcast to Tendermint
msg := BuildMsg(from, to, coins)
msg := client.BuildMsg(from, to, coins)
res, err := ctx.EnsureSignBuildBroadcast(ctx.FromAddressName, msg, cdc)
if err != nil {
return err
@@ -62,11 +62,3 @@ func SendTxCmd(cdc *wire.Codec) *cobra.Command {
cmd.Flags().String(flagAmount, "", "Amount of coins to send")
return cmd
}
// build the sendTx msg
func BuildMsg(from sdk.Address, to sdk.Address, coins sdk.Coins) sdk.Msg {
input := bank.NewInput(from, coins)
output := bank.NewOutput(to, coins)
msg := bank.NewMsgSend([]bank.Input{input}, []bank.Output{output})
return msg
}
@@ -12,12 +12,14 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/bank/commands"
"github.com/cosmos/cosmos-sdk/x/bank/client"
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
r.HandleFunc("/accounts/{address}/send", SendRequestHandler(cdc, kb)).Methods("POST")
ctx := context.NewCoreContextFromViper()
r.HandleFunc("/accounts/{address}/send", SendRequestHandler(cdc, kb, ctx)).Methods("POST")
}
type sendBody struct {
@@ -31,8 +33,7 @@ type sendBody struct {
}
// SendRequestHandler - http request handler to send coins to a address
func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWriter, *http.Request) {
ctx := context.NewCoreContextFromViper()
func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// collect data
vars := mux.Vars(r)
@@ -68,7 +69,7 @@ func SendRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWrit
to := sdk.Address(bz)
// build message
msg := commands.BuildMsg(info.PubKey.Address(), to, m.Amount)
msg := client.BuildMsg(info.PubKey.Address(), to, m.Amount)
if err != nil { // XXX rechecking same error ?
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
+14
View File
@@ -0,0 +1,14 @@
package client
import (
sdk "github.com/cosmos/cosmos-sdk/types"
bank "github.com/cosmos/cosmos-sdk/x/bank"
)
// build the sendTx msg
func BuildMsg(from sdk.Address, to sdk.Address, coins sdk.Coins) sdk.Msg {
input := bank.NewInput(from, coins)
output := bank.NewOutput(to, coins)
msg := bank.NewMsgSend([]bank.Input{input}, []bank.Output{output})
return msg
}
@@ -1,4 +1,4 @@
package commands
package cli
import (
"encoding/hex"
@@ -13,7 +13,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/ibc"
)
@@ -1,4 +1,4 @@
package commands
package cli
import (
"os"
@@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/ibc"
)
@@ -16,7 +16,9 @@ import (
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
r.HandleFunc("/ibc/{destchain}/{address}/send", TransferRequestHandler(cdc, kb)).Methods("POST")
ctx := context.NewCoreContextFromViper()
r.HandleFunc("/ibc/{destchain}/{address}/send", TransferRequestHandler(cdc, kb, ctx)).Methods("POST")
}
type transferBody struct {
@@ -30,8 +32,7 @@ type transferBody struct {
// TransferRequestHandler - http request handler to transfer coins to a address
// on a different chain via IBC
func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWriter, *http.Request) {
ctx := context.NewCoreContextFromViper()
func TransferRequestHandler(cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// collect data
vars := mux.Vars(r)
@@ -1,4 +1,4 @@
package commands
package cli
import (
"encoding/hex"
@@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/simplestake"
)
@@ -1,4 +1,4 @@
package commands
package cli
import (
flag "github.com/spf13/pflag"
@@ -1,4 +1,4 @@
package commands
package cli
import (
"encoding/hex"
@@ -1,4 +1,4 @@
package commands
package cli
import (
"encoding/hex"
@@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
"github.com/cosmos/cosmos-sdk/x/stake"
)
@@ -16,12 +16,13 @@ import (
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(r *mux.Router, cdc *wire.Codec, kb keys.Keybase) {
r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", BondingStatusHandler("stake", cdc, kb)).Methods("GET")
ctx := context.NewCoreContextFromViper()
r.HandleFunc("/stake/{delegator}/bonding_status/{candidate}", BondingStatusHandler("stake", cdc, kb, ctx)).Methods("GET")
}
// BondingStatusHandler - http request handler to query delegator bonding status
func BondingStatusHandler(storeName string, cdc *wire.Codec, kb keys.Keybase) func(http.ResponseWriter, *http.Request) {
ctx := context.NewCoreContextFromViper()
func BondingStatusHandler(storeName string, cdc *wire.Codec, kb keys.Keybase, ctx context.CoreContext) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// read parameters
vars := mux.Vars(r)