/
rpc.go
107 lines (86 loc) · 1.78 KB
/
rpc.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
package rpc
import (
"bytes"
"context"
"errors"
"net/http"
"reflect"
"github.com/vmihailenco/msgpack/v5"
)
type Result string
const (
SUCCESS Result = "success"
FAILURE Result = "failure"
)
type RPC struct {
http *http.Client
url string
token string
Auth *auth
Console *console
Core *core
Health *health
Job *job
Module *module
Plugin *plugin
Session *session
}
func NewRPC(http *http.Client, url string) *RPC {
rpc := &RPC{
http: http,
url: url,
}
rpc.Auth = &auth{rpc: rpc}
rpc.Console = &console{rpc: rpc}
rpc.Core = &core{rpc: rpc}
rpc.Health = &health{rpc: rpc}
rpc.Job = &job{rpc: rpc}
rpc.Module = &module{rpc: rpc}
rpc.Plugin = &plugin{rpc: rpc}
rpc.Session = &session{rpc: rpc}
return rpc
}
func (r *RPC) Call(req, res interface{}) error {
method := rpcMethod(req)
if method != "auth.login" && method != "health.check" {
if r.token == "" {
return errors.New("client not authenticated")
}
}
buf := new(bytes.Buffer)
enc := msgpack.NewEncoder(buf)
enc.UseArrayEncodedStructs(true)
if err := enc.Encode(req); err != nil {
return err
}
request, err := http.NewRequestWithContext(context.Background(), "POST", r.url, buf)
if err != nil {
return err
}
request.Header.Add("Content-Type", "binary/message-pack")
response, err := r.http.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
if res != nil {
if err := msgpack.NewDecoder(response.Body).Decode(res); err != nil {
return err
}
}
return nil
}
func (r *RPC) Token() string {
return r.token
}
func (r *RPC) SetToken(token string) {
r.token = token
}
func rpcMethod(req interface{}) string {
stype := reflect.ValueOf(req).Elem()
field := stype.FieldByName("Method")
if field.IsValid() {
return field.String()
}
return ""
}