tendermint/client/http_client.go

163 lines
4.1 KiB
Go
Raw Normal View History

2016-01-12 16:50:06 -05:00
package rpcclient
import (
"bytes"
2016-01-13 18:37:35 -05:00
"encoding/json"
2016-01-12 16:50:06 -05:00
"errors"
"io/ioutil"
2016-02-18 21:07:49 +00:00
"net"
2016-01-12 16:50:06 -05:00
"net/http"
"net/url"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-rpc/types"
"github.com/tendermint/go-wire"
)
2016-02-18 21:07:49 +00:00
// Set the net.Dial manually so we can do http over tcp or unix.
// Get/Post require a dummyDomain but it's over written by the Transport
2016-02-19 02:05:24 +00:00
var dummyDomain = "http://dummyDomain"
2016-02-18 21:07:49 +00:00
2016-02-19 02:05:24 +00:00
func dialer(remote string) func(string, string) (net.Conn, error) {
2016-02-18 21:07:49 +00:00
return func(proto, addr string) (conn net.Conn, err error) {
2016-02-19 02:05:24 +00:00
return net.Dial(rpctypes.SocketType(remote), remote)
2016-02-18 21:07:49 +00:00
}
2016-01-12 16:50:06 -05:00
}
2016-02-19 00:20:20 +00:00
// remote is IP:PORT or /path/to/socket
2016-02-18 21:07:49 +00:00
func socketTransport(remote string) *http.Transport {
2016-02-19 02:05:24 +00:00
return &http.Transport{
Dial: dialer(remote),
2016-02-18 21:07:49 +00:00
}
2016-01-12 16:50:06 -05:00
}
2016-02-18 21:07:49 +00:00
//------------------------------------------------------------------------------------
// JSON rpc takes params as a slice
type ClientJSONRPC struct {
2016-01-12 16:50:06 -05:00
remote string
2016-02-18 21:07:49 +00:00
client *http.Client
2016-01-12 16:50:06 -05:00
}
2016-02-18 21:07:49 +00:00
func NewClientJSONRPC(remote string) *ClientJSONRPC {
return &ClientJSONRPC{
remote: remote,
client: &http.Client{Transport: socketTransport(remote)},
2016-01-12 16:50:06 -05:00
}
}
2016-02-18 21:07:49 +00:00
func (c *ClientJSONRPC) Call(method string, params []interface{}, result interface{}) (interface{}, error) {
return c.call(method, params, result)
2016-01-12 16:50:06 -05:00
}
2016-02-18 21:07:49 +00:00
func (c *ClientJSONRPC) call(method string, params []interface{}, result interface{}) (interface{}, error) {
2016-01-12 16:50:06 -05:00
// Make request and get responseBytes
request := rpctypes.RPCRequest{
JSONRPC: "2.0",
Method: method,
Params: params,
ID: "",
}
requestBytes := wire.JSONBytes(request)
requestBuf := bytes.NewBuffer(requestBytes)
2016-02-18 21:07:49 +00:00
log.Info(Fmt("RPC request to %v (%v): %v", c.remote, method, string(requestBytes)))
httpResponse, err := c.client.Post(dummyDomain, "text/json", requestBuf)
2016-01-12 16:50:06 -05:00
if err != nil {
return nil, err
}
defer httpResponse.Body.Close()
responseBytes, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
2016-02-18 21:07:49 +00:00
// log.Info(Fmt("RPC response: %v", string(responseBytes)))
2016-01-13 18:37:35 -05:00
return unmarshalResponseBytes(responseBytes, result)
2016-01-12 16:50:06 -05:00
}
2016-02-18 21:07:49 +00:00
//-------------------------------------------------------------
// URI takes params as a map
type ClientURI struct {
remote string
client *http.Client
}
func NewClientURI(remote string) *ClientURI {
return &ClientURI{
remote: remote,
client: &http.Client{Transport: socketTransport(remote)},
}
}
func (c *ClientURI) Call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
return c.call(method, params, result)
}
func (c *ClientURI) call(method string, params map[string]interface{}, result interface{}) (interface{}, error) {
2016-01-12 16:50:06 -05:00
values, err := argsToURLValues(params)
if err != nil {
return nil, err
}
2016-02-18 21:07:49 +00:00
log.Info(Fmt("URI request to %v (%v): %v", c.remote, method, values))
2016-02-19 02:05:24 +00:00
resp, err := c.client.PostForm(dummyDomain+"/"+method, values)
2016-01-12 16:50:06 -05:00
if err != nil {
return nil, err
}
defer resp.Body.Close()
responseBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
2016-01-13 18:37:35 -05:00
return unmarshalResponseBytes(responseBytes, result)
2016-01-12 16:50:06 -05:00
}
//------------------------------------------------
2016-01-13 18:37:35 -05:00
func unmarshalResponseBytes(responseBytes []byte, result interface{}) (interface{}, error) {
2016-01-12 16:50:06 -05:00
// read response
// if rpc/core/types is imported, the result will unmarshal
// into the correct type
var err error
response := &rpctypes.RPCResponse{}
2016-01-13 18:37:35 -05:00
err = json.Unmarshal(responseBytes, response)
2016-01-12 16:50:06 -05:00
if err != nil {
return nil, err
}
errorStr := response.Error
if errorStr != "" {
return nil, errors.New(errorStr)
}
2016-01-13 18:37:35 -05:00
// unmarshal the RawMessage into the result
result = wire.ReadJSONPtr(result, *response.Result, &err)
return result, err
2016-01-12 16:50:06 -05:00
}
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
}