2018-03-10 09:33:05 -08:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-03-10 09:33:05 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2018-08-06 11:11:30 -07:00
|
|
|
|
2019-02-14 08:53:36 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2018-03-10 09:33:05 -08:00
|
|
|
)
|
|
|
|
|
2018-04-18 21:49:24 -07:00
|
|
|
// register REST routes
|
2018-09-13 11:17:32 -07:00
|
|
|
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}",
|
2018-11-19 09:02:34 -08:00
|
|
|
QueryAccountRequestHandlerFn(storeName, cdc, context.GetAccountDecoder(cdc), cliCtx),
|
2018-04-18 21:49:24 -07:00
|
|
|
).Methods("GET")
|
2019-02-26 03:34:27 -08:00
|
|
|
|
2018-09-29 20:42:12 -07:00
|
|
|
r.HandleFunc(
|
|
|
|
"/bank/balances/{address}",
|
2018-11-19 09:02:34 -08:00
|
|
|
QueryBalancesRequestHandlerFn(storeName, cdc, context.GetAccountDecoder(cdc), 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(
|
2018-09-13 11:17:32 -07:00
|
|
|
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)
|
2018-06-05 21:53:04 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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 {
|
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 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 {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-03-10 09:33:05 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.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 {
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
2018-09-29 20:42:12 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := cliCtx.QueryStore(auth.AddressStoreKey(addr), storeName)
|
|
|
|
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 {
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-04 07:48:26 -08:00
|
|
|
rest.PostProcessResponse(w, cdc, account.GetCoins(), cliCtx.Indent)
|
2018-09-29 20:42:12 -07:00
|
|
|
}
|
|
|
|
}
|