-
Notifications
You must be signed in to change notification settings - Fork 7
/
query.go
executable file
·59 lines (50 loc) · 2.17 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package rest
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/coinexchain/cet-sdk/modules/alias/internal/keepers"
"github.com/coinexchain/cet-sdk/modules/alias/internal/types"
"github.com/coinexchain/cosmos-utils/client/restutil"
)
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec) {
r.HandleFunc("/alias/address-of-alias/{alias}", queryAddressHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc("/alias/aliases-of-address/{address}", queryAliasesHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc("/alias/parameters", queryParamsHandlerFn(cliCtx)).Methods("GET")
}
func queryAddressHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
query := fmt.Sprintf("custom/%s/%s", types.StoreKey, keepers.QueryAliasInfo)
if !types.IsValidAlias(vars["alias"]) {
rest.WriteErrorResponse(w, http.StatusBadRequest, "Invalid Alias")
return
}
param := &keepers.QueryAliasInfoParam{Alias: vars["alias"], QueryOp: keepers.GetAddressFromAlias}
restutil.RestQuery(cdc, cliCtx, w, r, query, param, nil)
}
}
func queryAliasesHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
query := fmt.Sprintf("custom/%s/%s", types.StoreKey, keepers.QueryAliasInfo)
acc, err := sdk.AccAddressFromBech32(vars["address"])
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
param := &keepers.QueryAliasInfoParam{Owner: acc, QueryOp: keepers.ListAliasOfAccount}
restutil.RestQuery(cdc, cliCtx, w, r, query, param, nil)
}
}
// HTTP request handler to query the alias params values
func queryParamsHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", types.StoreKey, keepers.QueryParameters)
restutil.RestQuery(nil, cliCtx, w, r, route, nil, nil)
}
}