mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-29 16:52:15 +00:00
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
|
package rpc
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
|
||
|
"github.com/tendermint/tendermint/binary"
|
||
|
)
|
||
|
|
||
|
func Call(remote string, method string, params []interface{}, dest interface{}) (interface{}, error) {
|
||
|
// Make request and get responseBytes
|
||
|
request := RPCRequest{
|
||
|
JSONRPC: "2.0",
|
||
|
Method: method,
|
||
|
Params: params,
|
||
|
Id: 0,
|
||
|
}
|
||
|
requestBytes := binary.JSONBytes(request)
|
||
|
requestBuf := bytes.NewBuffer(requestBytes)
|
||
|
httpResponse, err := http.Post(remote, "text/json", requestBuf)
|
||
|
if err != nil {
|
||
|
return dest, err
|
||
|
}
|
||
|
defer httpResponse.Body.Close()
|
||
|
responseBytes, err := ioutil.ReadAll(httpResponse.Body)
|
||
|
if err != nil {
|
||
|
return dest, err
|
||
|
}
|
||
|
|
||
|
// Parse response into JSONResponse
|
||
|
response := RPCResponse{}
|
||
|
err = json.Unmarshal(responseBytes, &response)
|
||
|
if err != nil {
|
||
|
return dest, err
|
||
|
}
|
||
|
// Parse response into dest
|
||
|
resultJSONObject := response.Result
|
||
|
errorStr := response.Error
|
||
|
if errorStr != "" {
|
||
|
return dest, errors.New(errorStr)
|
||
|
}
|
||
|
dest = binary.ReadJSONObject(dest, resultJSONObject, &err)
|
||
|
return dest, err
|
||
|
}
|