-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
208 lines (166 loc) · 4.53 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
/*
* Go OAuth2 Client
*
* MIT License
*
* Copyright (c) 2015 Globo.com
*/
package galf
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"time"
"github.com/afex/hystrix-go/hystrix"
"github.com/facebookgo/stackerr"
"github.com/globocom/goreq"
)
type (
Client struct {
TokenManager TokenManager
Options ClientOptions
clientHTTP goreq.Client
}
)
func init() {}
func NewClient(options ...ClientOptions) *Client {
clientOptions := defaultClientOptions
if len(options) > 0 {
clientOptions = options[0]
}
return NewClientCustom(defaultTokenManager, clientOptions)
}
func NewClientCustom(tokenManager TokenManager, options ClientOptions) *Client {
return &Client{
TokenManager: tokenManager,
Options: options,
clientHTTP: goreq.NewClient(goreq.Options{
Timeout: options.Timeout,
MaxIdleConnsPerHost: 250,
}),
}
}
func (c *Client) Get(url string, reqOptions ...*requestOptions) (*goreq.Response, error) {
return c.retry(http.MethodGet, url, nil, reqOptions...)
}
func (c *Client) Post(url string, body interface{}, reqOptions ...*requestOptions) (*goreq.Response, error) {
return c.retry(http.MethodPost, url, body, reqOptions...)
}
func (c *Client) Put(url string, body interface{}, reqOptions ...*requestOptions) (*goreq.Response, error) {
return c.retry(http.MethodPut, url, body, reqOptions...)
}
func (c *Client) Delete(url string, reqOptions ...*requestOptions) (*goreq.Response, error) {
return c.retry(http.MethodDelete, url, nil, reqOptions...)
}
func (c *Client) retry(method string, url string, body interface{}, reqOptions ...*requestOptions) (resp *goreq.Response, err error) {
if c.TokenManager == nil {
return nil, errors.New("Configure tokenManager or SetDefaultTokenManager")
}
var reqOption *requestOptions
if len(reqOptions) > 0 {
reqOption = reqOptions[0]
}
var originalBody []byte
if originalBody, err = copyBody(body); err != nil {
return nil, err
}
var bodyReader io.Reader
for i := 1; i <= c.Options.MaxRetries; i++ {
if len(originalBody) > 0 {
bodyReader = bytes.NewBuffer(originalBody)
}
if resp, err = c.do(method, url, bodyReader, reqOption); err != nil {
return nil, err
}
if resp.StatusCode != http.StatusUnauthorized {
return resp, nil
}
if i < c.Options.MaxRetries {
c.TokenManager.ResetToken()
time.Sleep(c.Options.Backoff(i))
}
}
return resp, err
}
func (c *Client) do(method string, url string, body interface{}, reqOption *requestOptions) (*goreq.Response, error) {
token, err := c.TokenManager.GetToken()
if err != nil {
return nil, err
}
if c.Options.HystrixConfig == nil {
return c.request(token.Authorization, method, url, body, reqOption)
}
if err := c.Options.HystrixConfig.valid(); err != nil {
return nil, err
}
return c.requestHystrix(token.Authorization, method, url, body, reqOption)
}
func (c *Client) requestHystrix(authorization string, method string, url string, body interface{}, reqOption *requestOptions) (*goreq.Response, error) {
output := make(chan *goreq.Response, 1)
errors := hystrix.Go(c.Options.HystrixConfig.Name, func() error {
resp, err := c.request(authorization, method, url, body, reqOption)
if err != nil {
return err
}
output <- resp
return nil
}, nil)
select {
case out := <-output:
return out, nil
case err := <-errors:
return nil, err
}
}
func (c *Client) request(authorization string, method string, url string, body interface{}, reqOption *requestOptions) (*goreq.Response, error) {
req := goreq.Request{
Method: method,
ContentType: c.getContentType(),
Uri: url,
Body: body,
ShowDebug: c.Options.ShowDebug,
}
req.AddHeader("Authorization", authorization)
if reqOption != nil && reqOption.headers != nil {
for _, header := range reqOption.headers {
req.AddHeader(header.name, header.value)
}
}
resp, err := c.clientHTTP.Do(req)
if err != nil {
return nil, stackerr.Wrap(err)
}
return resp, nil
}
func (c *Client) getContentType() (contentType string) {
contentType = c.Options.ContentType
if contentType == "" {
contentType = DefaultContentType
}
return contentType
}
func copyBody(b interface{}) ([]byte, error) {
switch v := b.(type) {
case string:
return []byte(v), nil
case io.Reader:
var originalBody bytes.Buffer
_, err := io.Copy(&originalBody, v.(io.Reader))
if err != nil {
return nil, err
}
return originalBody.Bytes(), nil
case []byte:
return v, nil
case nil:
return nil, nil
default:
j, err := json.Marshal(b)
if err != nil {
return nil, err
}
return j, nil
}
}