-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.go
178 lines (150 loc) · 4.23 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"sync"
"encoding/json"
"time"
"bytes"
"io/ioutil"
"net/http"
"log"
)
type Client struct {
sync.Mutex
url string
client *http.Client
VMconfig *RPCVMConfig
}
type RPCRequest struct {
Id int64 `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
type RPCResponse struct {
Id int64 `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Result json.RawMessage `json:"result"`
Error string `json:"error"`
}
type RPCVMConfig struct {
DisableStorage bool `json:"disableStorage"`
DisableMemory bool `json:"disableMemory"`
DisableStack bool `json:"disableStack"`
FullStorage bool `json:"fullStorage"`
}
func NewClient(url string) *Client {
client := &Client{url: url}
client.client = &http.Client{}
client.VMconfig = &RPCVMConfig{
DisableStorage: false,
DisableMemory: true,
DisableStack: true,
FullStorage: false,
}
return client
}
var baseId int64 = 0
var requestIndex int64 = 0
func (self *Client) GetUniqueId() int64 {
//if the current time is greater than the previously used time seed plus the number of requests made at that time
//then any new request made at time seed current cannot collide with any open requests so reset the index and seed
if baseId + requestIndex < time.Now().UnixNano() {
baseId = time.Now().UnixNano()
requestIndex = 0
} else {
requestIndex += 1
}
return baseId + requestIndex
}
func (self *Client) MakeRequest(id int64, method string, params []interface{}) *RPCRequest {
jsonReq := &RPCRequest{
Id: id,
Jsonrpc: "2.0",
Method: method,
Params: params,
}
return jsonReq
}
func (self *Client) MakeRequestUnique(method string, params []interface{}) *RPCRequest {
jsonReq := &RPCRequest{
Id: self.GetUniqueId(),
Jsonrpc: "2.0",
Method: method,
Params: params,
}
return jsonReq
}
func (self *Client) BatchRequest(requests []*RPCRequest) ([]RPCResponse, error) {
reqJSON, _ := json.Marshal(requests)
req, err := http.NewRequest("POST", self.url, bytes.NewBuffer(reqJSON))
req.Header.Set("Content-Length", (string)(len(reqJSON)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := self.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var rpcresp []RPCResponse
err = json.Unmarshal(body, &rpcresp)
if err != nil {
log.Println(string(reqJSON))
log.Println(string(body))
return nil, err
}
return rpcresp, nil
}
func (self *Client) PostRequest(requests *RPCRequest) (*RPCResponse, error) {
reqJSON, _ := json.Marshal(requests)
req, err := http.NewRequest("POST", self.url, bytes.NewBuffer(reqJSON))
req.Header.Set("Content-Length", (string)(len(reqJSON)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := self.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var rpcresp RPCResponse
err = json.Unmarshal(body, &rpcresp)
if err != nil {
log.Println(string(reqJSON))
log.Println(string(body))
return nil, err
}
return &rpcresp, nil
}
//NOTE: PostRpcRequest is here to return a non nil rpcresp, and err object vs. PostRequest above
//For now, the response is converted to a string and returned as the first value.
//TODO: think about how to structure the return values here.
func (self *Client) PostRpcRequest(requests *RPCRequest) (string, *RPCResponse, error) {
reqJSON, _ := json.Marshal(requests)
req, err := http.NewRequest("POST", self.url, bytes.NewBuffer(reqJSON))
req.Header.Set("Content-Length", (string)(len(reqJSON)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := self.client.Do(req)
if err != nil {
return "", nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if err != nil {
return "", nil, err
}
var rpcresp RPCResponse
err = json.Unmarshal(body, &rpcresp)
if err != nil {
log.Println(string(reqJSON))
log.Println(string(body))
}
return string(body), &rpcresp, err
}