-
Notifications
You must be signed in to change notification settings - Fork 162
/
post.go
146 lines (130 loc) · 3.84 KB
/
post.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
package client
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
)
const (
defaultTimeout = 60 // seconds
defaultRequestID = 1
)
// Request json rpc request
type Request struct {
Method string
Params interface{}
Timeout int
ID int
}
// NewRequest new request
func NewRequest(method string, params ...interface{}) *Request {
return &Request{
Method: method,
Params: params,
Timeout: defaultTimeout,
ID: defaultRequestID,
}
}
// NewRequestWithTimeoutAndID new request with timeout and id
func NewRequestWithTimeoutAndID(timeout, id int, method string, params ...interface{}) *Request {
return &Request{
Method: method,
Params: params,
Timeout: timeout,
ID: id,
}
}
// RPCPost rpc post
func RPCPost(result interface{}, url, method string, params ...interface{}) error {
req := NewRequest(method, params...)
return RPCPostRequest(url, req, result)
}
// RPCPostWithTimeoutAndID rpc post with timeout and id
func RPCPostWithTimeoutAndID(result interface{}, timeout, id int, url, method string, params ...interface{}) error {
req := NewRequestWithTimeoutAndID(timeout, id, method, params...)
return RPCPostRequest(url, req, result)
}
// RequestBody request body
type RequestBody struct {
Version string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params"`
ID int `json:"id"`
}
type jsonError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
func (err *jsonError) Error() string {
return fmt.Sprintf("json-rpc error %d, %s", err.Code, err.Message)
}
type jsonrpcResponse struct {
Version string `json:"jsonrpc,omitempty"`
ID json.RawMessage `json:"id,omitempty"`
Error *jsonError `json:"error,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
}
// RPCPostRequest rpc post request
func RPCPostRequest(url string, req *Request, result interface{}) error {
reqBody := &RequestBody{
Version: "2.0",
Method: req.Method,
Params: req.Params,
ID: req.ID,
}
resp, err := HTTPPost(url, reqBody, nil, nil, req.Timeout)
if err != nil {
return err
}
return getResultFromJSONResponse(result, resp)
}
func getResultFromJSONResponse(result interface{}, resp *http.Response) error {
defer resp.Body.Close()
const maxReadContentLength int64 = 1024 * 1024 * 10 // 10M
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, maxReadContentLength))
if err != nil {
return fmt.Errorf("read body error: %v", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("wrong response status %v. message: %v", resp.StatusCode, string(body))
}
if len(body) == 0 {
return fmt.Errorf("empty response body")
}
var jsonResp jsonrpcResponse
err = json.Unmarshal(body, &jsonResp)
if err != nil {
return fmt.Errorf("unmarshal body error, body is \"%v\" err=\"%v\"", string(body), err)
}
if jsonResp.Error != nil {
return fmt.Errorf("return error: %v", jsonResp.Error.Error())
}
err = json.Unmarshal(jsonResp.Result, &result)
if err != nil {
return fmt.Errorf("unmarshal result error: %v", err)
}
return nil
}
// RPCRawPost rpc raw post
func RPCRawPost(url, body string) (string, error) {
return RPCRawPostWithTimeout(url, body, defaultTimeout)
}
// RPCRawPostWithTimeout rpc raw post with timeout
func RPCRawPostWithTimeout(url, reqBody string, timeout int) (string, error) {
resp, err := HTTPRawPost(url, reqBody, nil, nil, timeout)
if err != nil {
return "", err
}
defer resp.Body.Close()
const maxReadContentLength int64 = 1024 * 1024 * 10 // 10M
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, maxReadContentLength))
if err != nil {
return "", fmt.Errorf("read body error: %v", err)
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("wrong response status %v. message: %v", resp.StatusCode, string(body))
}
return string(body), nil
}