-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrest.go
158 lines (129 loc) · 2.59 KB
/
rest.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package rc
import (
"encoding/json"
"net/url"
"strings"
"go.uber.org/zap"
"gopkg.in/resty.v1"
)
const (
RESTAPIPath = "/api"
RESTV1Path = "/v1"
RESTInfoPath = "/info"
TimeFormat = "2006-01-02T15:04:05.999Z"
)
type restClient struct {
*resty.Client
server string
rest string
info string
debug bool
log *zap.SugaredLogger
}
func newRESTClient(server string, debug bool, logger *zap.SugaredLogger) *restClient {
server = stripTrailingSlash(server)
return &restClient{
Client: resty.New(),
server: server,
rest: server + RESTAPIPath + RESTV1Path,
info: server + RESTAPIPath + RESTInfoPath,
debug: debug,
log: logger,
}
}
func stripTrailingSlash(u string) string {
return strings.TrimRight(u, "/")
}
type Result interface {
Body() []byte
Error() error
JSON(v interface{}) error
String() string
StatusCode() int
}
type restReturn struct {
code int
body []byte
err error
}
func (rr *restReturn) Body() []byte {
return rr.body
}
func (rr *restReturn) Error() error {
return rr.err
}
func (rr *restReturn) JSON(v interface{}) error {
if rr.err != nil {
return rr.err
}
return json.Unmarshal(rr.body, v)
}
func (rr *restReturn) String() string {
return string(rr.Body())
}
func (rr *restReturn) StatusCode() int {
return rr.code
}
func (r *restClient) setAuthHeader(id, token string) {
r.SetHeaders(map[string]string{
"X-Auth-Token": token,
"X-User-Id": id,
})
}
func (r *restClient) postForm(path string, vals url.Values) Result {
call, err := r.R().
SetMultiValueFormData(vals).
Post(r.rest + path)
return &restReturn{
code: call.StatusCode(),
body: call.Body(),
err: err,
}
}
func (r *restClient) postJSON(path string, v interface{}) Result {
call, err := r.R().
SetBody(v).
Post(r.rest + path)
return &restReturn{
code: call.StatusCode(),
body: call.Body(),
err: err,
}
}
func (r *restClient) get(path string, vals url.Values) Result {
call, err := r.R().
SetMultiValueQueryParams(vals).
Get(r.rest + path)
if r.debug {
r.log.Debugw("rest_get", "path", path, "status", call.StatusCode(), "body", string(call.Body()))
}
return &restReturn{
code: call.StatusCode(),
body: call.Body(),
err: err,
}
}
type urlQ struct {
vals url.Values
}
func query(k, v string) *urlQ {
return &urlQ{
vals: url.Values{k: []string{v}},
}
}
func (q *urlQ) V(k, v string) *urlQ {
q.vals[k] = []string{v}
return q
}
func (q *urlQ) Q() url.Values {
return q.vals
}
func (r *restClient) getInfo() Result {
call, err := r.R().
Get(r.info)
return &restReturn{
code: call.StatusCode(),
body: call.Body(),
err: err,
}
}