add Call method to WSClient, which does proper encoding of params

This commit is contained in:
Anton Kaliaev 2017-03-10 15:23:43 +04:00
parent 3233c9c003
commit 5d19a008ce
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
4 changed files with 27 additions and 15 deletions

View File

@ -125,3 +125,4 @@ IMPROVEMENTS:
- added `HTTPClient` interface, which can be used for both `ClientURI`
and `ClientJSONRPC`
- 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)

View File

@ -72,7 +72,6 @@ func (c *JSONRPCClient) Call(method string, params map[string]interface{}, resul
// (handlers.go:176) on the server side
encodedParams := make(map[string]interface{})
for k, v := range params {
// log.Printf("%s: %v (%s)\n", k, v, string(wire.JSONBytes(v)))
bytes := json.RawMessage(wire.JSONBytes(v))
encodedParams[k] = &bytes
}

View File

@ -10,6 +10,7 @@ import (
"github.com/pkg/errors"
cmn "github.com/tendermint/go-common"
types "github.com/tendermint/go-rpc/types"
wire "github.com/tendermint/go-wire"
)
const (
@ -116,7 +117,8 @@ func (wsc *WSClient) receiveEventsRoutine() {
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 {
err := wsc.WriteJSON(types.RPCRequest{
JSONRPC: "2.0",
@ -127,7 +129,8 @@ func (wsc *WSClient) Subscribe(eventid string) error {
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 {
err := wsc.WriteJSON(types.RPCRequest{
JSONRPC: "2.0",
@ -137,3 +140,22 @@ func (wsc *WSClient) Unsubscribe(eventid string) error {
})
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
}

View File

@ -137,12 +137,7 @@ func echoViaWS(cl *client.WSClient, val string) (string, error) {
params := map[string]interface{}{
"arg": val,
}
err := cl.WriteJSON(types.RPCRequest{
JSONRPC: "2.0",
ID: "",
Method: "echo",
Params: params,
})
err := cl.Call("echo", params)
if err != nil {
return "", err
}
@ -164,12 +159,7 @@ func echoBytesViaWS(cl *client.WSClient, bytes []byte) ([]byte, error) {
params := map[string]interface{}{
"arg": bytes,
}
err := cl.WriteJSON(types.RPCRequest{
JSONRPC: "2.0",
ID: "",
Method: "echo_bytes",
Params: params,
})
err := cl.Call("echo_bytes", params)
if err != nil {
return []byte{}, err
}