-
Notifications
You must be signed in to change notification settings - Fork 2
/
rpc.go
195 lines (168 loc) · 3.96 KB
/
rpc.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package uuoskit
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
type GetTableRowsArgs struct {
Json bool `json:"json"`
Code string `json:"code"`
Scope string `json:"scope"`
Table string `json:"table"`
LowerBound string `json:"lower_bound"`
UpperBound string `json:"upper_bound"`
Limit int `json:"limit"`
KeyType string `json:"key_type"`
IndexPosition int `json:"index_position"`
Reverse bool `json:"reverse"`
ShowPayer bool `json:"show_payer"`
}
type GetRequiredKeysArgs struct {
Transaction *Transaction `json:"transaction"`
AvailableKeys []string `json:"available_keys"`
}
type GetAccountArgs struct {
AccountName string `json:"account_name"`
}
//required_keys
type GetRequiredKeysResult struct {
RequiredKeys []string `json:"required_keys"`
}
type RpcError struct {
err string
}
func (r *RpcError) Error() string {
return r.err
}
func NewRpcError(err string) *RpcError {
return &RpcError{err}
}
type Rpc struct {
client *http.Client
url string
}
func NewRpc(url string) *Rpc {
tr := &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
DisableCompression: true,
}
rpc := &Rpc{}
rpc.url = url
rpc.client = &http.Client{Transport: tr}
return rpc
}
func (r *Rpc) GetInfo() (*ChainInfo, error) {
var info ChainInfo
result, err := r.Call("chain", "get_info", "")
if err != nil {
return nil, newError(err)
}
err = json.Unmarshal(result, &info)
if err != nil {
return nil, newError(err)
}
return &info, nil
}
func (r *Rpc) GetAccount(args *GetAccountArgs) (JsonValue, error) {
_args, err := json.Marshal(args)
if err != nil {
return JsonValue{}, newError(err)
}
result, err := r.Call("chain", "get_account", _args)
if err != nil {
return JsonValue{}, newError(err)
}
result2 := JsonValue{}
err = json.Unmarshal(result, &result2)
if err != nil {
return JsonValue{}, newError(err)
}
return result2, nil
}
func (r *Rpc) GetRequiredKeys(args *GetRequiredKeysArgs) (*GetRequiredKeysResult, error) {
_args, err := json.Marshal(args)
if err != nil {
return nil, newError(err)
}
result, err := r.Call("chain", "get_required_keys", _args)
if err != nil {
return nil, newError(err)
}
result2 := &GetRequiredKeysResult{}
err = json.Unmarshal(result, result2)
if err != nil {
return nil, newError(err)
}
return result2, nil
}
func (t *Rpc) GetTableRows(args *GetTableRowsArgs) (JsonValue, error) {
result := JsonValue{}
r, err := t.Call("chain", "get_table_rows", args)
if err != nil {
return NewJsonValue(nil), err
}
err = json.Unmarshal(r, &result)
if err != nil {
return NewJsonValue(nil), err
}
return result, nil
}
func (t *Rpc) PushTransaction(packedTx *PackedTransaction) (JsonValue, error) {
result := JsonValue{}
_packedTx, err := json.Marshal(packedTx)
if err != nil {
return JsonValue{}, err
}
r, err := t.Call("chain", "push_transaction", _packedTx)
if err != nil {
return JsonValue{}, err
}
err = json.Unmarshal(r, &result)
if err != nil {
return JsonValue{}, err
}
return result, nil
}
func (r *Rpc) Call(api string, endpoint string, params interface{}) ([]byte, error) {
var _params []byte
reqUrl := fmt.Sprintf("%s/v1/%s/%s", r.url, api, endpoint)
switch v := params.(type) {
case string:
_params = []byte(v)
case []byte:
_params = v
default:
var err error
_params, err = json.Marshal(v)
if err != nil {
return nil, newError(err)
}
}
if len(_params) == 0 {
resp, err := r.client.Get(reqUrl)
if err != nil {
return nil, newError(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, newError(err)
}
return body, nil
}
buf := bytes.NewBuffer(_params)
resp, err := r.client.Post(reqUrl, "application/json", buf)
if err != nil {
return nil, newError(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, newError(err)
}
return body, nil
}