cosmos-sdk/x/auth/client/rest/query.go

107 lines
2.8 KiB
Go
Raw Normal View History

2018-03-10 09:33:05 -08:00
package rest
import (
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
2018-03-10 09:33:05 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-05-23 19:47:33 -07:00
"github.com/cosmos/cosmos-sdk/x/auth"
2018-08-06 11:11:30 -07:00
"github.com/gorilla/mux"
2018-03-10 09:33:05 -08:00
)
2018-04-18 21:49:24 -07:00
// register REST routes
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec, storeName string) {
2018-04-18 21:49:24 -07:00
r.HandleFunc(
2018-10-04 05:27:43 -07:00
"/auth/accounts/{address}",
QueryAccountRequestHandlerFn(storeName, cdc, context.GetAccountDecoder(cdc), cliCtx),
2018-04-18 21:49:24 -07:00
).Methods("GET")
2018-09-29 20:42:12 -07:00
r.HandleFunc(
"/bank/balances/{address}",
QueryBalancesRequestHandlerFn(storeName, cdc, context.GetAccountDecoder(cdc), cliCtx),
2018-09-29 20:42:12 -07:00
).Methods("GET")
r.HandleFunc(
"/tx/sign",
SignTxRequestHandlerFn(cdc, cliCtx),
).Methods("POST")
2018-03-10 09:33:05 -08:00
}
2018-04-18 21:49:24 -07:00
// query accountREST Handler
2018-08-06 11:11:30 -07:00
func QueryAccountRequestHandlerFn(
storeName string, cdc *codec.Codec,
2018-08-06 11:11:30 -07:00
decoder auth.AccountDecoder, cliCtx context.CLIContext,
) http.HandlerFunc {
2018-03-10 09:33:05 -08:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bech32addr := vars["address"]
2018-03-14 03:43:31 -07:00
2018-07-09 16:06:05 -07:00
addr, err := sdk.AccAddressFromBech32(bech32addr)
2018-03-10 09:33:05 -08:00
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-03-10 09:33:05 -08:00
return
}
2018-08-06 11:11:30 -07:00
res, err := cliCtx.QueryStore(auth.AddressStoreKey(addr), storeName)
2018-03-10 09:33:05 -08:00
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-03-10 09:33:05 -08:00
return
}
// the query will return empty if there is no data for this account
2018-03-14 03:43:31 -07:00
if len(res) == 0 {
2018-03-10 09:33:05 -08:00
w.WriteHeader(http.StatusNoContent)
return
}
2018-03-20 18:27:50 -07:00
// decode the value
2018-04-18 21:49:24 -07:00
account, err := decoder(res)
2018-03-10 09:33:05 -08:00
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-03-10 09:33:05 -08:00
return
}
utils.PostProcessResponse(w, cdc, account, cliCtx.Indent)
2018-03-10 09:33:05 -08:00
}
}
2018-09-29 20:42:12 -07:00
// query accountREST Handler
func QueryBalancesRequestHandlerFn(
storeName string, cdc *codec.Codec,
decoder auth.AccountDecoder, cliCtx context.CLIContext,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2018-09-30 03:50:31 -07:00
w.Header().Set("Content-Type", "application/json")
2018-09-29 20:42:12 -07:00
vars := mux.Vars(r)
bech32addr := vars["address"]
addr, err := sdk.AccAddressFromBech32(bech32addr)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
res, err := cliCtx.QueryStore(auth.AddressStoreKey(addr), storeName)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-09-29 20:42:12 -07:00
return
}
// the query will return empty if there is no data for this account
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
// decode the value
account, err := decoder(res)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-09-29 20:42:12 -07:00
return
}
utils.PostProcessResponse(w, cdc, account.GetCoins(), cliCtx.Indent)
2018-09-29 20:42:12 -07:00
}
}