-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
74 lines (59 loc) · 1.63 KB
/
response.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
package jsonrpc
import (
"encoding/json"
"net/http"
)
// ResponseWriter represents JSON-RPC response writer interface.
type ResponseWriter interface {
// SetResponse set response result.
SetResponse(v interface{})
// SetErrorData set response error data.
// Error will have -32603 status code and message equal to "Internal error".
SetErrorData(v interface{})
// SetInvalidRequestParamsError set response error to invalid req params.
// Error will have -32602 status code and message equal to "Invalid params".
SetInvalidRequestParamsError(data interface{})
}
func newErrorResponse(err *respError) *response {
return &response{
Version: Version,
Error: err,
}
}
func newResponse(r *Request) *response {
return &response{
ID: r.ID,
Version: r.Version,
}
}
// A response represents a JSON-RPC response returned by the server.
type response struct {
Version string `json:"jsonrpc"`
Result interface{} `json:"result,omitempty"`
Error *respError `json:"error,omitempty"`
ID interface{} `json:"id"`
}
func (r *response) SetResponse(v interface{}) {
r.Result = v
}
func (r *response) SetInvalidRequestParamsError(data interface{}) {
r.Error = errInvalidParams()
r.Error.Data = data
}
func (r *response) SetErrorData(v interface{}) {
if r.Error == nil {
r.Error = errInternal()
}
r.Error.Data = v
}
// sendResponse writes JSON-RPC response.
func sendResponse(w http.ResponseWriter, resp []*response) error {
w.Header().Set(contentTypeKey, contentTypeValue)
if len(resp) > 1 {
return json.NewEncoder(w).Encode(resp)
}
if len(resp) == 1 {
return json.NewEncoder(w).Encode(resp[0])
}
return nil
}