-
Notifications
You must be signed in to change notification settings - Fork 13
/
rest.go
46 lines (37 loc) · 1.33 KB
/
rest.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
package rest
import (
"fmt"
"net/http"
"github.com/commercionetwork/commercionetwork/x/encapsulated/bank/internal/types"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/bank/blockedAccounts", queryBlockedAccountsRequestHandlerFn(cliCtx)).Methods("GET")
}
// query accountREST Handler
func queryBlockedAccountsRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
query := fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryBlockedAccounts)
res, height, err := cliCtx.QueryWithData(query, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
// the query will return empty if there is no data for this account
if len(res) == 0 {
rest.PostProcessResponse(w, cliCtx, sdk.Coins{})
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}