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

124 lines
3.6 KiB
Go
Raw Normal View History

2018-03-10 09:33:05 -08:00
package rest
import (
"fmt"
"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"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
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-09-13 08:09:17 -07:00
"/accounts/{address}",
2018-08-06 11:11:30 -07:00
QueryAccountRequestHandlerFn(storeName, cdc, authcmd.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, authcmd.GetAccountDecoder(cdc), cliCtx),
).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) {
2018-09-30 03:50:31 -07:00
w.Header().Set("Content-Type", "application/json")
2018-03-10 09:33:05 -08:00
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, fmt.Sprintf("couldn't query account. Error: %s", 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, fmt.Sprintf("couldn't parse query result. Result: %s. Error: %s", res, err.Error()))
2018-03-10 09:33:05 -08:00
return
}
// print out whole account
2018-04-07 10:56:49 -07:00
output, err := cdc.MarshalJSON(account)
2018-03-10 09:33:05 -08:00
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't marshall query result. Error: %s", err.Error()))
2018-03-10 09:33:05 -08:00
return
}
w.Write(output)
}
}
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, fmt.Sprintf("couldn't query account. Error: %s", err.Error()))
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, fmt.Sprintf("couldn't parse query result. Result: %s. Error: %s", res, err.Error()))
return
}
// print out whole account
output, err := cdc.MarshalJSONIndent(account.GetCoins(), "", " ")
2018-09-29 20:42:12 -07:00
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't marshall query result. Error: %s", err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
2018-09-29 20:42:12 -07:00
w.Write(output)
}
}