/
client.go
59 lines (51 loc) · 1.36 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package rpcclient
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/Jeffail/gabs"
)
var (
httpClient = &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 100,
},
Timeout: time.Second * 500,
}
)
type RPCRequest struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
ID int `json:"id"`
}
func NewRPCRequest(jsonrpc string, method string, param ...interface{}) *RPCRequest {
r := new(RPCRequest)
r.Jsonrpc = jsonrpc
r.Method = method
r.Params = make([]interface{}, 0)
r.Params = append(r.Params, param...)
return r
}
func SendRPCRequst(host string, rpcRequest *RPCRequest) (*gabs.Container, error) {
var buff bytes.Buffer
err := json.NewEncoder(&buff).Encode(rpcRequest)
if err != nil {
return nil, fmt.Errorf("SendRPCRequst EncodeRequest error --- %s", err)
}
req, _ := http.NewRequest("POST", host, &buff)
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
//resp, err := http.Post(host, "application/json", &buff)
if err != nil {
return nil, fmt.Errorf("SendRPCRequst Post error --- %s", err)
}
defer resp.Body.Close()
jsonParsed, err := gabs.ParseJSONBuffer(resp.Body)
if err != nil {
return nil, fmt.Errorf("SendRPCRequst ParseJSONBuffer error --- %s", err)
}
return jsonParsed, nil
}