/
query.go
114 lines (93 loc) · 3.62 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
// Copyright [2021] - [2022], AssetMantle Pte. Ltd. and the code contributors
// SPDX-License-Identifier: Apache-2.0
package base
import (
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
abciTypes "github.com/tendermint/tendermint/abci/types"
"github.com/AssetMantle/modules/schema/helpers"
)
type query struct {
name string
cliCommand helpers.CLICommand
moduleName string
queryKeeper helpers.QueryKeeper
requestPrototype func() helpers.QueryRequest
responsePrototype func() helpers.QueryResponse
keeperPrototype func() helpers.QueryKeeper
}
var _ helpers.Query = (*query)(nil)
func (query query) GetName() string { return query.name }
func (query query) Command(codec *codec.Codec) *cobra.Command {
runE := func(command *cobra.Command, args []string) error {
cliContext := context.NewCLIContext().WithCodec(codec)
queryRequest, err := query.requestPrototype().FromCLI(query.cliCommand, cliContext)
if err != nil {
return err
}
responseBytes, _, err := query.query(queryRequest, cliContext)
if err != nil {
return err
}
response, err := query.responsePrototype().Decode(responseBytes)
if err != nil {
return err
}
return cliContext.PrintOutput(response)
}
return query.cliCommand.CreateCommand(runE)
}
func (query query) HandleMessage(context sdkTypes.Context, requestQuery abciTypes.RequestQuery) ([]byte, error) {
request, err := query.requestPrototype().Decode(requestQuery.Data)
if err != nil {
return nil, err
}
return query.queryKeeper.Enquire(context, request).Encode()
}
func (query query) RESTQueryHandler(outerCliContext context.CLIContext) http.HandlerFunc {
return func(responseWriter http.ResponseWriter, httpRequest *http.Request) {
responseWriter.Header().Set("Content-Type", "application/json")
cliContext, ok := rest.ParseQueryHeightOrReturnBadRequest(responseWriter, outerCliContext, httpRequest)
if !ok {
return
}
queryRequest, err := query.requestPrototype().FromMap(mux.Vars(httpRequest))
if err != nil {
rest.WriteErrorResponse(responseWriter, http.StatusInternalServerError, err.Error())
return
}
response, height, err := query.query(queryRequest, cliContext)
if err != nil {
rest.WriteErrorResponse(responseWriter, http.StatusInternalServerError, err.Error())
return
}
cliContext = cliContext.WithHeight(height)
rest.PostProcessResponse(responseWriter, cliContext, response)
}
}
func (query query) Initialize(mapper helpers.Mapper, parameters helpers.Parameters, auxiliaryKeepers ...interface{}) helpers.Query {
query.queryKeeper = query.keeperPrototype().Initialize(mapper, parameters, auxiliaryKeepers).(helpers.QueryKeeper)
return query
}
func (query query) query(queryRequest helpers.QueryRequest, cliContext context.CLIContext) ([]byte, int64, error) {
bytes, err := queryRequest.Encode()
if err != nil {
return nil, 0, err
}
return cliContext.QueryWithData("custom"+"/"+query.moduleName+"/"+query.name, bytes)
}
func NewQuery(name string, short string, long string, moduleName string, requestPrototype func() helpers.QueryRequest, responsePrototype func() helpers.QueryResponse, keeperPrototype func() helpers.QueryKeeper, flagList ...helpers.CLIFlag) helpers.Query {
return query{
name: name,
cliCommand: NewCLICommand(name, short, long, flagList),
moduleName: moduleName,
requestPrototype: requestPrototype,
responsePrototype: responsePrototype,
keeperPrototype: keeperPrototype,
}
}