-
Notifications
You must be signed in to change notification settings - Fork 13
/
query.go
153 lines (124 loc) · 4.35 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package rest
import (
"fmt"
"net/http"
"github.com/commercionetwork/commercionetwork/x/docs/internal/types"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
)
const (
addressRestParameterName = "user"
)
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(
fmt.Sprintf("/docs/{%s}/sent", addressRestParameterName),
getSentDocumentsHandler(cliCtx)).
Methods("GET")
r.HandleFunc(
fmt.Sprintf("/docs/{%s}/received", addressRestParameterName),
getReceivedDocumentsHandler(cliCtx)).
Methods("GET")
r.HandleFunc(
fmt.Sprintf("/receipts/{%s}/sent", addressRestParameterName),
getSentDocumentsReceiptsHandler(cliCtx)).
Methods("GET")
r.HandleFunc(
fmt.Sprintf("/receipts/{%s}/received", addressRestParameterName),
getReceivedDocumentsReceiptsHandler(cliCtx)).
Methods("GET")
r.HandleFunc(
fmt.Sprintf("/docs/metadataSchemes"),
getSupportedMetadataSchemesHandler(cliCtx)).
Methods("GET")
r.HandleFunc(
fmt.Sprintf("/docs/metadataSchemes/proposers"),
getTrustedMetadataSchemesProposersHandler(cliCtx)).
Methods("GET")
}
// ----------------------------------
// --- Documents
// ----------------------------------
func getSentDocumentsHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
address := vars[addressRestParameterName]
route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QuerySentDocuments, address)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}
func getReceivedDocumentsHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
address := vars[addressRestParameterName]
route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryReceivedDocuments, address)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}
// ---------------------------------
// --- Document receipts
// ---------------------------------
func getSentDocumentsReceiptsHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
address := vars[addressRestParameterName]
route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QuerySentReceipts, address)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}
func getReceivedDocumentsReceiptsHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
address := vars[addressRestParameterName]
route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryReceivedReceipts, address)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}
// ----------------------------------
// --- Document metadata schemes
// ----------------------------------
func getSupportedMetadataSchemesHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySupportedMetadataSchemes)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}
// -----------------------------------------
// --- Document metadata schemes proposers
// -----------------------------------------
func getTrustedMetadataSchemesProposersHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTrustedMetadataProposers)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}