-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
78 lines (63 loc) · 1.84 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
package volunteer
import (
"github.com/gogo/protobuf/proto"
"github.com/xpladev/xpla.go/core"
"github.com/xpladev/xpla.go/types"
"github.com/xpladev/xpla.go/types/errors"
"github.com/xpladev/xpla.go/util"
volunteertypes "github.com/xpladev/xpla/x/volunteer/types"
)
var out []byte
var res proto.Message
var err error
// Query client for volunteer module.
func QueryVolunteer(i core.QueryClient) (string, error) {
if i.QueryType == types.QueryGrpc {
return queryByGrpcVolunteer(i)
} else {
return queryByLcdVolunteer(i)
}
}
func queryByGrpcVolunteer(i core.QueryClient) (string, error) {
queryClient := volunteertypes.NewQueryClient(i.Ixplac.GetGrpcClient())
switch {
// Volunteer validators
case i.Ixplac.GetMsgType() == VolunteerQueryValidatorsMsgType:
convertMsg := i.Ixplac.GetMsg().(volunteertypes.QueryVolunteerValidatorsRequest)
res, err = queryClient.VolunteerValidators(
i.Ixplac.GetContext(),
&convertMsg,
)
if err != nil {
return "", util.LogErr(errors.ErrGrpcRequest, err)
}
default:
return "", util.LogErr(errors.ErrInvalidMsgType, i.Ixplac.GetMsgType())
}
out, err = core.PrintProto(i, res)
if err != nil {
return "", err
}
return string(out), nil
}
const (
volunteerQueryValidatorsLabel = "validators"
)
func queryByLcdVolunteer(i core.QueryClient) (string, error) {
url := "/xpla/volunteer/v1beta1/"
switch {
// Skating validator
case i.Ixplac.GetMsgType() == VolunteerQueryValidatorsMsgType:
url = url + util.MakeQueryLabels(volunteerQueryValidatorsLabel)
default:
return "", util.LogErr(errors.ErrInvalidMsgType, i.Ixplac.GetMsgType())
}
i.Ixplac.GetHttpMutex().Lock()
out, err := util.CtxHttpClient("GET", i.Ixplac.GetLcdURL()+url, nil, i.Ixplac.GetContext())
if err != nil {
i.Ixplac.GetHttpMutex().Unlock()
return "", err
}
i.Ixplac.GetHttpMutex().Unlock()
return string(out), nil
}