steem-block-db/jsonrpc.go

111 lines
2.1 KiB
Go
Raw Permalink Normal View History

2018-10-04 18:41:39 +00:00
package main
// thanks to https://github.com/onrik/ethrpc/blob/master/ethrpc.go for
// example
import (
"bytes"
"encoding/json"
"fmt"
2018-10-18 06:31:02 +00:00
log "github.com/sirupsen/logrus"
2018-10-04 18:41:39 +00:00
"io"
"io/ioutil"
"net/http"
2018-11-12 05:34:55 +00:00
"time"
2018-10-04 18:41:39 +00:00
)
2018-10-18 06:31:02 +00:00
type httpRPCClient interface {
2018-10-04 18:41:39 +00:00
Post(url string, contentType string, body io.Reader) (*http.Response, error)
}
2018-10-18 06:31:02 +00:00
type JSONRPCError struct {
2018-10-04 18:41:39 +00:00
Code int `json:"code"`
Message string `json:"message"`
}
2018-10-18 06:31:02 +00:00
func (err JSONRPCError) Error() string {
2018-10-04 18:41:39 +00:00
return fmt.Sprintf("Error %d (%s)", err.Code, err.Message)
}
2018-10-18 06:31:02 +00:00
type JSONRPCResponse struct {
2018-10-18 06:31:43 +00:00
ID string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result json.RawMessage `json:"result"`
Error *JSONRPCError `json:"error"`
2018-10-18 06:31:02 +00:00
}
type JSONRPCRequest struct {
2018-10-18 07:09:30 +00:00
ID string `json:"id"`
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
2018-10-04 18:41:39 +00:00
}
2018-10-18 06:31:02 +00:00
type JSONRPC struct {
2018-10-04 18:41:39 +00:00
url string
2018-10-18 06:31:02 +00:00
client httpRPCClient
2018-10-04 18:41:39 +00:00
}
// New create new rpc client with given url
2018-10-18 06:31:02 +00:00
func NewJSONRPC(url string, options ...func(rpc *JSONRPC)) *JSONRPC {
2018-11-12 05:34:55 +00:00
netClient := &http.Client{
Timeout: time.Second * 20,
}
2018-10-18 06:31:02 +00:00
rpc := &JSONRPC{
2018-10-04 18:41:39 +00:00
url: url,
2018-11-12 05:34:55 +00:00
client: netClient,
2018-10-04 18:41:39 +00:00
}
for _, option := range options {
option(rpc)
}
return rpc
}
// Call returns raw response of method call
2018-10-18 07:09:30 +00:00
func (rpc *JSONRPC) Call(method string, params json.RawMessage) (json.RawMessage, error) {
2018-10-18 06:31:02 +00:00
request := JSONRPCRequest{
2018-10-04 18:41:39 +00:00
ID: "1",
JSONRPC: "2.0",
Method: method,
Params: params,
}
body, err := json.Marshal(request)
if err != nil {
return nil, err
}
response, err := rpc.client.Post(rpc.url, "application/json", bytes.NewBuffer(body))
if response != nil {
defer response.Body.Close()
}
if err != nil {
2018-11-12 05:34:55 +00:00
log.Infof("jsonrpc error: %v", err)
2018-10-04 18:41:39 +00:00
return nil, err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
2018-10-31 09:44:19 +00:00
log.Debugf("%s", method)
log.Debugf("Request: %s", body)
log.Debugf("Response: %s", data)
2018-10-04 18:41:39 +00:00
2018-10-18 06:31:02 +00:00
resp := new(JSONRPCResponse)
2018-10-04 18:41:39 +00:00
if err := json.Unmarshal(data, resp); err != nil {
return nil, err
}
if resp.Error != nil {
return nil, *resp.Error
}
return resp.Result, nil
}