0g-chain/x/auction/client/rest/query.go

120 lines
3.7 KiB
Go
Raw Normal View History

2019-11-25 19:46:02 +00:00
package rest
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/kava-labs/kava/x/auction/types"
)
2019-11-25 19:46:02 +00:00
const restAuctionID = "auction-id"
2019-11-25 19:46:02 +00:00
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(fmt.Sprintf("/%s/auctions", types.ModuleName), queryAuctionsHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/auctions/{%s}", types.ModuleName, restAuctionID), queryAuctionHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/parameters", types.ModuleName), getParamsHandlerFn(cliCtx)).Methods("GET")
2019-11-25 19:46:02 +00:00
}
func queryAuctionHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
2019-11-25 19:46:02 +00:00
return func(w http.ResponseWriter, r *http.Request) {
// Parse the query height
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
// Prepare params for querier
vars := mux.Vars(r)
if len(vars[restAuctionID]) == 0 {
err := fmt.Errorf("%s required but not specified", restAuctionID)
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
auctionID, ok := rest.ParseUint64OrReturnBadRequest(w, vars[restAuctionID])
if !ok {
return
}
bz, err := cliCtx.Codec.MarshalJSON(types.QueryAuctionParams{AuctionID: auctionID})
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
// Query
2020-02-01 15:49:36 +00:00
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("/custom/%s/%s", types.ModuleName, types.QueryGetAuction), bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
// Decode and return results
cliCtx = cliCtx.WithHeight(height)
var auction types.Auction
2020-02-01 15:49:36 +00:00
err = cliCtx.Codec.UnmarshalJSON(res, &auction)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
auctionWithPhase := types.NewAuctionWithPhase(auction)
rest.PostProcessResponse(w, cliCtx, cliCtx.Codec.MustMarshalJSON(auctionWithPhase))
}
}
func queryAuctionsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Parse the query height
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
// Get all auctions
2020-02-01 15:49:36 +00:00
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("/custom/%s/%s", types.ModuleName, types.QueryGetAuctions), nil)
2019-11-25 19:46:02 +00:00
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
// Decode and return results
2019-11-25 19:46:02 +00:00
cliCtx = cliCtx.WithHeight(height)
var auctions types.Auctions
2020-02-01 15:49:36 +00:00
err = cliCtx.Codec.UnmarshalJSON(res, &auctions)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
auctionsWithPhase := []types.AuctionWithPhase{} // using empty slice so json returns [] instead of null when there's no auctions
for _, a := range auctions {
auctionsWithPhase = append(auctionsWithPhase, types.NewAuctionWithPhase(a))
}
rest.PostProcessResponse(w, cliCtx, cliCtx.Codec.MustMarshalJSON(auctionsWithPhase))
2019-11-25 19:46:02 +00:00
}
}
func getParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Parse the query height
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
// Get the params
2020-02-01 15:49:36 +00:00
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryGetParams), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
// Decode and return results
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}