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

121 lines
3.0 KiB
Go
Raw Normal View History

2018-03-10 09:33:05 -08:00
package rest
import (
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
2018-03-10 09:33:05 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
2018-08-06 11:11:30 -07:00
"github.com/cosmos/cosmos-sdk/x/auth/types"
2018-03-10 09:33:05 -08:00
)
2019-06-14 02:52:28 -07:00
// AccountWithHeight wraps the embedded Account with the height it was queried
// at.
type AccountWithHeight struct {
types.Account
Height int64 `json:"height"`
}
2018-04-18 21:49:24 -07:00
// register REST routes
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, 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, context.GetAccountDecoder(cliCtx.Codec), 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, context.GetAccountDecoder(cliCtx.Codec), cliCtx),
2018-09-29 20:42:12 -07:00
).Methods("GET")
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, decoder types.AccountDecoder, cliCtx context.CLIContext,
2018-08-06 11:11:30 -07:00
) 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 {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-03-10 09:33:05 -08:00
return
}
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
res, height, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
2018-03-10 09:33:05 -08:00
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-03-10 09:33:05 -08:00
return
}
// the query will return empty account if there is no data
2018-03-14 03:43:31 -07:00
if len(res) == 0 {
rest.PostProcessResponse(w, cliCtx, types.BaseAccount{})
2018-03-10 09:33:05 -08:00
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 {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-03-10 09:33:05 -08:00
return
}
rest.PostProcessResponse(w, cliCtx, AccountWithHeight{account, height})
2018-03-10 09:33:05 -08:00
}
}
2018-09-29 20:42:12 -07:00
// query accountREST Handler
func QueryBalancesRequestHandlerFn(
storeName string, decoder types.AccountDecoder, cliCtx context.CLIContext,
2018-09-29 20:42:12 -07:00
) http.HandlerFunc {
2018-09-29 20:42:12 -07:00
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 {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-09-29 20:42:12 -07:00
return
}
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
res, _, err := cliCtx.QueryStore(types.AddressStoreKey(addr), storeName)
2018-09-29 20:42:12 -07:00
if err != nil {
2019-02-04 07:48:26 -08:00
rest.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 {
rest.PostProcessResponse(w, cliCtx, sdk.Coins{})
2018-09-29 20:42:12 -07:00
return
}
// decode the value
account, err := decoder(res)
if err != nil {
2019-02-04 07:48:26 -08:00
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
2018-09-29 20:42:12 -07:00
return
}
rest.PostProcessResponse(w, cliCtx, account.GetCoins())
2018-09-29 20:42:12 -07:00
}
}