-
Notifications
You must be signed in to change notification settings - Fork 17
/
request.go
46 lines (36 loc) · 1.08 KB
/
request.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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0
package jsonrpc
import "encoding/json"
const (
jsonRpcVersion = "2.0"
)
// Method is type of JSON-RPC method
type Method string
// Param is type of JSON-RPC params list
type Param interface{}
// RequestID is type for request ID.
type RequestID uint
// Request is type of JSON-RPC request struct
type Request struct {
Method Method `json:"method"`
Params []Param `json:"params"`
ID RequestID `json:"id"`
JsonRpc string `json:"jsonrpc"`
}
// NewRequest creates `Request` with default `JsonRpc` = "2.0" and ID = 1
func NewRequest(m Method, params ...Param) *Request {
return NewRequestWithID(1, m, params...)
}
// NewRequestWithID creates `Request` with default `JsonRpc` = "2.0"
func NewRequestWithID(id RequestID, m Method, params ...Param) *Request {
if params == nil {
params = []Param{}
}
return &Request{m, params, id, jsonRpcVersion}
}
// ToString returns json format of the request
func (r *Request) ToString() string {
b, _ := json.MarshalIndent(r, "", "\t")
return string(b)
}