|
|
@ -2,6 +2,7 @@ package rpcclient |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"io/ioutil" |
|
|
|
"net/http" |
|
|
@ -22,8 +23,8 @@ 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) |
|
|
|
func (c *ClientJSONRPC) Call(method string, params []interface{}, result interface{}) (interface{}, error) { |
|
|
|
return CallHTTP_JSONRPC(c.remote, method, params, result) |
|
|
|
} |
|
|
|
|
|
|
|
// URI takes params as a map
|
|
|
@ -38,11 +39,11 @@ func NewClientURI(remote string) *ClientURI { |
|
|
|
return &ClientURI{remote} |
|
|
|
} |
|
|
|
|
|
|
|
func (c *ClientURI) Call(method string, params map[string]interface{}) (interface{}, error) { |
|
|
|
return CallHTTP_URI(c.remote, method, params) |
|
|
|
func (c *ClientURI) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) { |
|
|
|
return CallHTTP_URI(c.remote, method, params, result) |
|
|
|
} |
|
|
|
|
|
|
|
func CallHTTP_JSONRPC(remote string, method string, params []interface{}) (interface{}, error) { |
|
|
|
func CallHTTP_JSONRPC(remote string, method string, params []interface{}, result interface{}) (interface{}, error) { |
|
|
|
// Make request and get responseBytes
|
|
|
|
request := rpctypes.RPCRequest{ |
|
|
|
JSONRPC: "2.0", |
|
|
@ -63,10 +64,10 @@ func CallHTTP_JSONRPC(remote string, method string, params []interface{}) (inter |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
log.Info(Fmt("RPC response: %v", string(responseBytes))) |
|
|
|
return unmarshalResponseBytes(responseBytes) |
|
|
|
return unmarshalResponseBytes(responseBytes, result) |
|
|
|
} |
|
|
|
|
|
|
|
func CallHTTP_URI(remote string, method string, params map[string]interface{}) (interface{}, error) { |
|
|
|
func CallHTTP_URI(remote string, method string, params map[string]interface{}, result interface{}) (interface{}, error) { |
|
|
|
values, err := argsToURLValues(params) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
@ -81,18 +82,18 @@ func CallHTTP_URI(remote string, method string, params map[string]interface{}) ( |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return unmarshalResponseBytes(responseBytes) |
|
|
|
return unmarshalResponseBytes(responseBytes, result) |
|
|
|
} |
|
|
|
|
|
|
|
//------------------------------------------------
|
|
|
|
|
|
|
|
func unmarshalResponseBytes(responseBytes []byte) (interface{}, error) { |
|
|
|
func unmarshalResponseBytes(responseBytes []byte, result interface{}) (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) |
|
|
|
err = json.Unmarshal(responseBytes, response) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
@ -100,7 +101,9 @@ func unmarshalResponseBytes(responseBytes []byte) (interface{}, error) { |
|
|
|
if errorStr != "" { |
|
|
|
return nil, errors.New(errorStr) |
|
|
|
} |
|
|
|
return response.Result, err |
|
|
|
// unmarshal the RawMessage into the result
|
|
|
|
result = wire.ReadJSONPtr(result, *response.Result, &err) |
|
|
|
return result, err |
|
|
|
} |
|
|
|
|
|
|
|
func argsToURLValues(args map[string]interface{}) (url.Values, error) { |
|
|
|