mirror of
https://github.com/fluencelabs/tendermint
synced 2025-07-02 14:11:37 +00:00
ws fixes; rpc tests
This commit is contained in:
@ -2,17 +2,47 @@ package rpcclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
. "github.com/tendermint/go-common"
|
||||
"github.com/tendermint/go-wire"
|
||||
"github.com/tendermint/tendermint/rpc/types"
|
||||
)
|
||||
|
||||
func CallHTTP(remote string, method string, params []interface{}, dest interface{}) (interface{}, error) {
|
||||
// JSON rpc takes params as a slice
|
||||
type ClientJSONRPC struct {
|
||||
remote string
|
||||
}
|
||||
|
||||
func NewClientJSONRPC(remote string) *ClientJSONRPC {
|
||||
return &ClientJSONRPC{remote}
|
||||
}
|
||||
|
||||
func (c *ClientJSONRPC) Call(method string, params []interface{}) (interface{}, error) {
|
||||
return CallHTTP_JSONRPC(c.remote, method, params)
|
||||
}
|
||||
|
||||
// URI takes params as a map
|
||||
type ClientURI struct {
|
||||
remote string
|
||||
}
|
||||
|
||||
func NewClientURI(remote string) *ClientURI {
|
||||
if !strings.HasSuffix(remote, "/") {
|
||||
remote = remote + "/"
|
||||
}
|
||||
return &ClientURI{remote}
|
||||
}
|
||||
|
||||
func (c *ClientURI) Call(method string, params map[string]interface{}) (interface{}, error) {
|
||||
return CallHTTP_URI(c.remote, method, params)
|
||||
}
|
||||
|
||||
func CallHTTP_JSONRPC(remote string, method string, params []interface{}) (interface{}, error) {
|
||||
// Make request and get responseBytes
|
||||
request := rpctypes.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
@ -25,27 +55,79 @@ func CallHTTP(remote string, method string, params []interface{}, dest interface
|
||||
log.Info(Fmt("RPC request to %v: %v", remote, string(requestBytes)))
|
||||
httpResponse, err := http.Post(remote, "text/json", requestBuf)
|
||||
if err != nil {
|
||||
return dest, err
|
||||
return nil, err
|
||||
}
|
||||
defer httpResponse.Body.Close()
|
||||
responseBytes, err := ioutil.ReadAll(httpResponse.Body)
|
||||
if err != nil {
|
||||
return dest, err
|
||||
return nil, err
|
||||
}
|
||||
log.Info(Fmt("RPC response: %v", string(responseBytes)))
|
||||
return unmarshalResponseBytes(responseBytes)
|
||||
}
|
||||
|
||||
// Parse response into JSONResponse
|
||||
response := rpctypes.RPCResponse{}
|
||||
err = json.Unmarshal(responseBytes, &response)
|
||||
func CallHTTP_URI(remote string, method string, params map[string]interface{}) (interface{}, error) {
|
||||
values, err := argsToURLValues(params)
|
||||
if err != nil {
|
||||
return dest, err
|
||||
return nil, err
|
||||
}
|
||||
log.Info(Fmt("URI request to %v: %v", remote, values))
|
||||
resp, err := http.PostForm(remote+method, values)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
responseBytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return unmarshalResponseBytes(responseBytes)
|
||||
}
|
||||
|
||||
//------------------------------------------------
|
||||
|
||||
func unmarshalResponseBytes(responseBytes []byte) (interface{}, error) {
|
||||
// read response
|
||||
// if rpc/core/types is imported, the result will unmarshal
|
||||
// into the correct type
|
||||
var err error
|
||||
response := &rpctypes.RPCResponse{}
|
||||
wire.ReadJSON(response, responseBytes, &err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Parse response into dest
|
||||
resultJSONObject := response.Result
|
||||
errorStr := response.Error
|
||||
if errorStr != "" {
|
||||
return dest, errors.New(errorStr)
|
||||
return nil, errors.New(errorStr)
|
||||
}
|
||||
dest = wire.ReadJSONObject(dest, resultJSONObject, &err)
|
||||
return dest, err
|
||||
return response.Result, err
|
||||
}
|
||||
|
||||
func argsToURLValues(args map[string]interface{}) (url.Values, error) {
|
||||
values := make(url.Values)
|
||||
if len(args) == 0 {
|
||||
return values, nil
|
||||
}
|
||||
err := argsToJson(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for key, val := range args {
|
||||
values.Set(key, val.(string))
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func argsToJson(args map[string]interface{}) error {
|
||||
var n int
|
||||
var err error
|
||||
for k, v := range args {
|
||||
buf := new(bytes.Buffer)
|
||||
wire.WriteJSON(v, buf, &n, &err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args[k] = buf.String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user