-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
66 lines (58 loc) · 1.91 KB
/
helpers.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
package jrpc2client
import (
"math/rand"
"reflect"
"strings"
"github.com/pquerna/ffjson/ffjson"
"github.com/riftbit/jrpc2errors"
"github.com/sirupsen/logrus"
)
// func printObject(v interface{}) string {
// res2B, _ := ffjson.Marshal(v)
// return string(res2B)
// }
// debugLogging method to show debug message with pre-processd values
func debugLogging(clientCfg *Client, fields logrus.Fields, message string) {
if clientCfg.logger.Level == logrus.DebugLevel {
for i, v := range fields {
if reflect.TypeOf(v).String() == "[]uint8" {
fields[i] = strings.Split(string(v.([]uint8)), "\r\n")
}
if i == "headers" && reflect.TypeOf(v).String() == "string" {
fields[i] = strings.Split(fields[i].(string), "\r\n")
}
}
clientCfg.logger.WithFields(fields).Debugln(message)
}
}
// encodeClientRequest encodes parameters for a JSON-RPC client request.
func encodeClientRequest(method string, args interface{}) ([]byte, error) {
c := &clientRequest{
ID: rand.Uint64(),
Version: "2.0",
Method: method,
Params: args,
}
return ffjson.Marshal(c)
}
// decodeClientResponse decodes the response body of a client request into the interface reply.
func decodeClientResponse(r []byte, dst interface{}) error {
var c clientResponse
if err := ffjson.NewDecoder().Decode(r, &c); err != nil {
return &jrpc2errors.Error{Code: jrpc2errors.ParseError, Message: err.Error()}
}
if c.Error != nil {
jsonErr := &jrpc2errors.Error{}
if err := ffjson.Unmarshal(*c.Error, jsonErr); err != nil {
return &jrpc2errors.Error{Code: jrpc2errors.InternalError, Message: string(*c.Error)}
}
return jsonErr
}
if c.Result == nil {
return &jrpc2errors.Error{Code: jrpc2errors.ServerError, Message: jrpc2errors.ErrNullResult.Error()}
}
if err := ffjson.Unmarshal(*c.Result, &dst); err != nil {
return &jrpc2errors.Error{Code: jrpc2errors.InternalError, Message: jrpc2errors.ErrNullResult.Error()}
}
return nil
}