-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
272 lines (227 loc) · 7.27 KB
/
client.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package httpx
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
const (
HeaderBodyHash = "X-Auth-BHash"
HeaderSignature = "X-Auth-Signature"
HeaderSigner = "X-Auth-Signer"
HeaderService = "X-Auth-Service"
HeaderJWTParsed = "jwt"
HeaderHeadersList = "X-Custom-Headers"
)
// XClient is an implementation of the Client.
type XClient struct {
http.Client
defaultHeaders Headers
cookies []*http.Cookie
logger *logrus.Entry
}
// NewXClient returns new Client.
func NewXClient() *XClient {
return &XClient{
Client: http.Client{Timeout: defaultTimeout},
defaultHeaders: map[string]string{},
cookies: []*http.Cookie{},
}
}
// SetHTTP - Set customized instance of http.Client
func (client *XClient) SetHTTP(hc http.Client) Client {
newClient := client.clone()
newClient.Client = hc
return newClient
}
// SetLogger - Set logger to enable log requests
func (client *XClient) SetLogger(logger *logrus.Entry) Client {
newClient := client.clone()
newClient.logger = logger
return newClient
}
// DefaultCookies returns a client's default cookies.
func (client *XClient) DefaultCookies() []*http.Cookie {
return client.cookies
}
// SetDefaultCookies sets a default cookies to the client.
func (client *XClient) SetDefaultCookies(cookies []*http.Cookie) Client {
newClient := client.clone()
newClient.cookies = append(newClient.cookies, cookies...)
return newClient
}
// RemoveDefaultCookies removes a default client's cookies.
func (client *XClient) RemoveDefaultCookies() Client {
newClient := client.clone()
newClient.cookies = nil
return newClient
}
// WithCookies append cookies to the client and return new instance.
func (client *XClient) WithCookies(cookies []*http.Cookie) Client {
newClient := client.clone()
newClient.cookies = append(newClient.cookies, cookies...)
return newClient
}
// DefaultHeaders returns a client's default headers.
func (client *XClient) DefaultHeaders() Headers {
return client.defaultHeaders
}
// SetDefaultHeaders sets a default headers to the client.
func (client *XClient) SetDefaultHeaders(headers Headers) Client {
newClient := client.clone()
if newClient.defaultHeaders == nil {
newClient.defaultHeaders = map[string]string{}
}
for key := range headers {
newClient.defaultHeaders[key] = headers[key]
}
return newClient
}
// SetHeader sets new default header to the client.
func (client *XClient) SetHeader(key, val string) Client {
newClient := client.clone()
if newClient.defaultHeaders == nil {
newClient.defaultHeaders = map[string]string{}
}
newClient.defaultHeaders[key] = val
return newClient
}
// RemoveDefaultHeaders removes a default client's headers.
func (client *XClient) RemoveDefaultHeaders() Client {
newClient := client.clone()
newClient.defaultHeaders = map[string]string{}
return newClient
}
// WithHeaders append headers to the client and return new instance.
func (client *XClient) WithHeaders(headers Headers) Client {
newClient := client.clone()
for key := range headers {
newClient.defaultHeaders[key] = headers[key]
}
return newClient
}
// PostJSON sets passed `headers` and `body` and executes RequestJSON with POST method.
func (client *XClient) PostJSON(url string, body interface{}, headers Headers) (*http.Response, error) {
return client.RequestJSON(http.MethodPost, url, body, headers)
}
// PutJSON sets passed `headers` and `body` and executes RequestJSON with PUT method.
func (client *XClient) PutJSON(url string, body interface{}, headers Headers) (*http.Response, error) {
return client.RequestJSON(http.MethodPut, url, body, headers)
}
// PatchJSON sets passed `headers` and `body` and executes RequestJSON with PATCH method.
func (client *XClient) PatchJSON(url string, body interface{}, headers Headers) (*http.Response, error) {
return client.RequestJSON(http.MethodPatch, url, body, headers)
}
// GetJSON sets passed `headers` and executes RequestJSON with GET method.
func (client *XClient) GetJSON(url string, headers Headers) (*http.Response, error) {
return client.RequestJSON(http.MethodGet, url, nil, headers)
}
// DeleteJSON sets passed `headers` and executes RequestJSON with DELETE method.
func (client *XClient) DeleteJSON(url string, headers Headers) (*http.Response, error) {
return client.RequestJSON(http.MethodDelete, url, nil, headers)
}
// RequestJSON creates and executes new request with JSON content type.
func (client *XClient) RequestJSON(method string, url string, body interface{},
headers Headers) (*http.Response, error) {
var rawData []byte
switch v := body.(type) {
case []byte:
rawData = v
default:
var err error
rawData, err = json.Marshal(body)
if err != nil {
return nil, errors.Wrap(err, "unable to marshal body")
}
}
if client.logger != nil {
client.logger.WithFields(logrus.Fields{
"method": method,
"url": url,
"headers": headers,
"body": string(rawData),
}).Trace("do json request")
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(rawData))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
for _, k := range client.cookies {
req.AddCookie(k)
}
for key, value := range client.defaultHeaders {
req.Header.Set(key, value)
}
for key, value := range headers {
req.Header.Set(key, value)
}
return client.Do(req)
}
// ParseJSONBody decodes `json` body from the `http.Request`.
// !> `dest` must be a pointer value.
func (client *XClient) ParseJSONBody(r *http.Request, dest interface{}) error {
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return errors.Wrap(err, "failed to unmarshal request body")
}
if client.logger != nil {
client.logger.WithFields(logrus.Fields{
"method": r.Method,
"remote_url": r.RemoteAddr,
"request_url": r.RequestURI,
"body": string(b)}).Trace("parse json request")
}
err = json.Unmarshal(b, dest)
if err != nil {
return errors.Wrap(err, "failed to unmarshal request body")
}
return nil
}
// ParseJSONResult decodes `json` body from the `http.Response` body into `dest`
// > `dest` must be a pointer value.
func (client *XClient) ParseJSONResult(httpResp *http.Response, dest interface{}) error {
defer httpResp.Body.Close()
b, err := ioutil.ReadAll(httpResp.Body)
if err != nil {
return errors.Wrap(err, "failed to unmarshal request body")
}
if client != nil && client.logger != nil {
client.logger.WithFields(logrus.Fields{
"method": httpResp.Request.Method,
"remote_url": httpResp.Request.RemoteAddr,
"request_url": httpResp.Request.RequestURI,
"status": httpResp.StatusCode,
"body": string(b)}).Trace("parse json response")
}
err = json.Unmarshal(b, dest)
if err != nil {
return errors.Wrap(err, "failed to unmarshal response body")
}
return nil
}
// Clone returns safe clone of Client.
func (client *XClient) Clone() Client {
return client.clone()
}
func (client *XClient) clone() *XClient {
var clone = NewXClient()
if client == nil {
return clone
}
if client.logger != nil {
clone.logger = client.logger.WithField("", "")
}
if len(client.cookies) > 0 {
copy(clone.cookies, client.cookies)
}
if len(client.defaultHeaders) > 0 {
for name := range client.defaultHeaders {
clone.defaultHeaders[name] = client.defaultHeaders[name]
}
}
return clone
}