-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
122 lines (100 loc) · 4.16 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package rest
import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/furya-official/fury-blockchain/x/payments/keeper"
"github.com/furya-official/fury-blockchain/x/payments/types"
)
func registerQueryRoutes(clientCtx client.Context, r *mux.Router) {
r.HandleFunc(fmt.Sprintf("/payments/templates/{%s}", RestPaymentTemplateId),
queryPaymentTemplateHandler(clientCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/payments/contracts/{%s}", RestPaymentContractId),
queryPaymentContractHandler(clientCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/payments/contracts_by_id_prefix/{%s}", RestPaymentContractsIdPrefix),
queryPaymentContractsByIdPrefixHandler(clientCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/payments/subscriptions/{%s}", RestSubscriptionId),
querySubscriptionHandler(clientCtx)).Methods("GET")
}
func queryPaymentTemplateHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
templateId := vars[RestPaymentTemplateId]
bz, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s",
types.QuerierRoute, keeper.QueryPaymentTemplate, templateId), nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("Couldn't get query iid %s", err.Error())))
return
}
var template types.PaymentTemplate
if err := clientCtx.LegacyAmino.UnmarshalJSON(bz, &template); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("Couldn't Unmarshal iid %s", err.Error())))
return
}
rest.PostProcessResponse(w, clientCtx, template)
}
}
func queryPaymentContractHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
contractId := vars[RestPaymentContractId]
bz, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s",
types.QuerierRoute, keeper.QueryPaymentContract, contractId), nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("Couldn't get query iid %s", err.Error())))
return
}
var contract types.PaymentContract
if err := clientCtx.LegacyAmino.UnmarshalJSON(bz, &contract); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("Couldn't Unmarshal iid %s", err.Error())))
return
}
rest.PostProcessResponse(w, clientCtx, contract)
}
}
func queryPaymentContractsByIdPrefixHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
contractIdPrefix := vars[RestPaymentContractsIdPrefix]
bz, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s",
types.QuerierRoute, keeper.QueryPaymentContractsByIdPrefix, contractIdPrefix), nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("Couldn't get query iid %s", err.Error())))
return
}
var contracts []types.PaymentContract
if err := clientCtx.LegacyAmino.UnmarshalJSON(bz, &contracts); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("Couldn't Unmarshal iid %s", err.Error())))
return
}
rest.PostProcessResponse(w, clientCtx, contracts)
}
}
func querySubscriptionHandler(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
subscriptionId := vars[RestSubscriptionId]
bz, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s",
types.QuerierRoute, keeper.QuerySubscription, subscriptionId), nil)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("Couldn't get query iid %s", err.Error())))
return
}
var subscription types.Subscription
if err := clientCtx.LegacyAmino.UnmarshalJSON(bz, &subscription); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("Couldn't Unmarshal iid %s", err.Error())))
return
}
rest.PostProcessResponse(w, clientCtx, subscription)
}
}