-
Notifications
You must be signed in to change notification settings - Fork 9
/
jsonrpc2.go
58 lines (49 loc) · 1.65 KB
/
jsonrpc2.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
package jsonrpc2
import (
"encoding/json"
"math/rand"
"strconv"
)
// EncodeClientRequest encodes parameters for a JSON-RPC client request.
func EncodeClientRequest(method string, args interface{}, id string) ([]byte, error) {
return NewRequest(method, args, id).Encode()
}
// Encode returns the byte array json encoded value
func (req *JSONRPCRequest) Encode() ([]byte, error) {
return json.Marshal(req)
}
// EncodeClientRequests encodes an array of rpc requests
func EncodeClientRequests(requests []*JSONRPCRequest) ([]byte, error) {
return json.Marshal(requests)
}
// BuildRequest creates a new RPC request struct with a random ID
func BuildRequest(method string, args interface{}) *JSONRPCRequest {
id := strconv.FormatInt(rand.Int63(), 16)
return NewRequest(method, args, id)
}
// NewRequest creates a new RPC requests struct with all attributes required
func NewRequest(method string, args interface{}, id string) *JSONRPCRequest {
return &JSONRPCRequest{
Version: "2.0",
Method: method,
Params: args,
ID: id,
}
}
// DecodeResponse decodes the top level json rpc response for a single rpc call
func DecodeResponse(response []byte) (*JSONRPCMessage, error) {
var message JSONRPCMessage
message.Raw = response
if err := json.Unmarshal(response, &message); err != nil {
return nil, DecodeError{response, err}
}
return &message, nil
}
// DecodeResponses decodes the top level json rpc response for a batched rpc call
func DecodeResponses(response []byte) ([]json.RawMessage, error) {
var messages []json.RawMessage
if err := json.Unmarshal(response, &messages); err != nil {
return nil, DecodeError{response, err}
}
return messages, nil
}