This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
jsonrpc.go
113 lines (99 loc) · 3.26 KB
/
jsonrpc.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
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rpc
import (
"encoding/json"
"fmt"
)
// JSON-RPC 2.0 error codes.
const (
INVALID_REQUEST = -32600
METHOD_NOT_FOUND = -32601
INVALID_PARAMS = -32602
INTERNAL_ERROR = -32603
PARSE_ERROR = -32700
)
// Request and Response objects. Id is a string. Error data not used.
// Refer to JSON-RPC specification http://www.jsonrpc.org/specification
type (
RPCRequest struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
Id string `json:"id"`
}
// RPCResponse MUST follow the JSON-RPC specification for Response object
// reference: http://www.jsonrpc.org/specification#response_object
RPCResponse interface {
AssertIsRPCResponse() bool
}
// RPCResultResponse MUST NOT contain the error member if no error occurred
RPCResultResponse struct {
Result interface{} `json:"result"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
// RPCErrorResponse MUST NOT contain the result member if an error occured
RPCErrorResponse struct {
Error *RPCError `json:"error"`
Id string `json:"id"`
JSONRPC string `json:"jsonrpc"`
}
// RPCError MUST be included in the Response object if an error occured
RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
// Note: Data is currently unused, and the data member may be omitted
// Data interface{} `json:"data"`
}
)
func (err RPCError) Error() string {
return fmt.Sprintf("Error %v: %s", err.Code, err.Message)
}
// Create a new RPC request. This is the generic struct that is passed to RPC
// methods
func NewRPCRequest(id string, method string, params json.RawMessage) *RPCRequest {
return &RPCRequest{
JSONRPC: "2.0",
Id: id,
Method: method,
Params: params,
}
}
// NewRPCResponse creates a new response object from a result
func NewRPCResponse(id string, res interface{}) RPCResponse {
return RPCResponse(&RPCResultResponse{
Result: res,
Id: id,
JSONRPC: "2.0",
})
}
// NewRPCErrorResponse creates a new error-response object from the error code and message
func NewRPCErrorResponse(id string, code int, message string) RPCResponse {
return RPCResponse(&RPCErrorResponse{
Error: &RPCError{code, message},
Id: id,
JSONRPC: "2.0",
})
}
// AssertIsRPCResponse implements a marker method for RPCResultResponse
// to implement the interface RPCResponse
func (rpcResultResponse *RPCResultResponse) AssertIsRPCResponse() bool {
return true
}
// AssertIsRPCResponse implements a marker method for RPCErrorResponse
// to implement the interface RPCResponse
func (rpcErrorResponse *RPCErrorResponse) AssertIsRPCResponse() bool {
return true
}