mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-28 21:51:22 +00:00
add Call method to WSClient, which does proper encoding of params
This commit is contained in:
parent
3233c9c003
commit
5d19a008ce
@ -125,3 +125,4 @@ IMPROVEMENTS:
|
|||||||
- added `HTTPClient` interface, which can be used for both `ClientURI`
|
- added `HTTPClient` interface, which can be used for both `ClientURI`
|
||||||
and `ClientJSONRPC`
|
and `ClientJSONRPC`
|
||||||
- all params are now optional (Golang's default will be used if some param is missing)
|
- all params are now optional (Golang's default will be used if some param is missing)
|
||||||
|
- added `Call` method to `WSClient` (see method's doc for details)
|
||||||
|
@ -72,7 +72,6 @@ func (c *JSONRPCClient) Call(method string, params map[string]interface{}, resul
|
|||||||
// (handlers.go:176) on the server side
|
// (handlers.go:176) on the server side
|
||||||
encodedParams := make(map[string]interface{})
|
encodedParams := make(map[string]interface{})
|
||||||
for k, v := range params {
|
for k, v := range params {
|
||||||
// log.Printf("%s: %v (%s)\n", k, v, string(wire.JSONBytes(v)))
|
|
||||||
bytes := json.RawMessage(wire.JSONBytes(v))
|
bytes := json.RawMessage(wire.JSONBytes(v))
|
||||||
encodedParams[k] = &bytes
|
encodedParams[k] = &bytes
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
cmn "github.com/tendermint/go-common"
|
cmn "github.com/tendermint/go-common"
|
||||||
types "github.com/tendermint/go-rpc/types"
|
types "github.com/tendermint/go-rpc/types"
|
||||||
|
wire "github.com/tendermint/go-wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -116,7 +117,8 @@ func (wsc *WSClient) receiveEventsRoutine() {
|
|||||||
close(wsc.ErrorsCh)
|
close(wsc.ErrorsCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
// subscribe to an event
|
// Subscribe to an event. Note the server must have a "subscribe" route
|
||||||
|
// defined.
|
||||||
func (wsc *WSClient) Subscribe(eventid string) error {
|
func (wsc *WSClient) Subscribe(eventid string) error {
|
||||||
err := wsc.WriteJSON(types.RPCRequest{
|
err := wsc.WriteJSON(types.RPCRequest{
|
||||||
JSONRPC: "2.0",
|
JSONRPC: "2.0",
|
||||||
@ -127,7 +129,8 @@ func (wsc *WSClient) Subscribe(eventid string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// unsubscribe from an event
|
// Unsubscribe from an event. Note the server must have a "unsubscribe" route
|
||||||
|
// defined.
|
||||||
func (wsc *WSClient) Unsubscribe(eventid string) error {
|
func (wsc *WSClient) Unsubscribe(eventid string) error {
|
||||||
err := wsc.WriteJSON(types.RPCRequest{
|
err := wsc.WriteJSON(types.RPCRequest{
|
||||||
JSONRPC: "2.0",
|
JSONRPC: "2.0",
|
||||||
@ -137,3 +140,22 @@ func (wsc *WSClient) Unsubscribe(eventid string) error {
|
|||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call asynchronously calls a given method by sending an RPCRequest to the
|
||||||
|
// server. Results will be available on ResultsCh, errors, if any, on ErrorsCh.
|
||||||
|
func (wsc *WSClient) Call(method string, params map[string]interface{}) error {
|
||||||
|
// we need this step because we attempt to decode values using `go-wire`
|
||||||
|
// (handlers.go:470) on the server side
|
||||||
|
encodedParams := make(map[string]interface{})
|
||||||
|
for k, v := range params {
|
||||||
|
bytes := json.RawMessage(wire.JSONBytes(v))
|
||||||
|
encodedParams[k] = &bytes
|
||||||
|
}
|
||||||
|
err := wsc.WriteJSON(types.RPCRequest{
|
||||||
|
JSONRPC: "2.0",
|
||||||
|
Method: method,
|
||||||
|
Params: encodedParams,
|
||||||
|
ID: "",
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
14
rpc_test.go
14
rpc_test.go
@ -137,12 +137,7 @@ func echoViaWS(cl *client.WSClient, val string) (string, error) {
|
|||||||
params := map[string]interface{}{
|
params := map[string]interface{}{
|
||||||
"arg": val,
|
"arg": val,
|
||||||
}
|
}
|
||||||
err := cl.WriteJSON(types.RPCRequest{
|
err := cl.Call("echo", params)
|
||||||
JSONRPC: "2.0",
|
|
||||||
ID: "",
|
|
||||||
Method: "echo",
|
|
||||||
Params: params,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -164,12 +159,7 @@ func echoBytesViaWS(cl *client.WSClient, bytes []byte) ([]byte, error) {
|
|||||||
params := map[string]interface{}{
|
params := map[string]interface{}{
|
||||||
"arg": bytes,
|
"arg": bytes,
|
||||||
}
|
}
|
||||||
err := cl.WriteJSON(types.RPCRequest{
|
err := cl.Call("echo_bytes", params)
|
||||||
JSONRPC: "2.0",
|
|
||||||
ID: "",
|
|
||||||
Method: "echo_bytes",
|
|
||||||
Params: params,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte{}, err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user