forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
95 lines (78 loc) · 2.51 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package rest
import (
"encoding/hex"
"fmt"
"net/http"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
"github.com/gorilla/mux"
)
func registerQueryRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc(
fmt.Sprintf("/evidence/{%s}", RestParamEvidenceHash),
queryEvidenceHandler(clientCtx),
).Methods(MethodGet)
r.HandleFunc(
"/evidence",
queryAllEvidenceHandler(clientCtx),
).Methods(MethodGet)
}
func queryEvidenceHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
evidenceHash := vars[RestParamEvidenceHash]
if strings.TrimSpace(evidenceHash) == "" {
rest.WriteErrorResponse(w, http.StatusBadRequest, "evidence hash required but not specified")
return
}
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
if !ok {
return
}
decodedHash, err := hex.DecodeString(evidenceHash)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, "invalid evidence hash")
return
}
params := types.NewQueryEvidenceRequest(decodedHash)
bz, err := clientCtx.Codec.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
return
}
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryEvidence)
res, height, err := clientCtx.QueryWithData(route, bz)
if rest.CheckInternalServerError(w, err) {
return
}
clientCtx = clientCtx.WithHeight(height)
rest.PostProcessResponse(w, clientCtx, res)
}
}
func queryAllEvidenceHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
if rest.CheckBadRequestError(w, err) {
return
}
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
if !ok {
return
}
params := types.NewQueryAllEvidenceParams(page, limit)
bz, err := clientCtx.LegacyAmino.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to marshal query params: %s", err))
return
}
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAllEvidence)
res, height, err := clientCtx.QueryWithData(route, bz)
if rest.CheckInternalServerError(w, err) {
return
}
clientCtx = clientCtx.WithHeight(height)
rest.PostProcessResponse(w, clientCtx, res)
}
}