-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathrequester.go
More file actions
244 lines (201 loc) · 7.77 KB
/
requester.go
File metadata and controls
244 lines (201 loc) · 7.77 KB
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
/****************************************************************************
* Copyright 2019,2022-2023 Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
// Package utils //
package utils
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/optimizely/go-sdk/v2/pkg/logging"
jsoniter "github.com/json-iterator/go"
)
const (
// HeaderContentType is the HTTP Content Type header.
HeaderContentType = "Content-Type"
// HeaderAuthorization is the HTTP Authorization Type header.
HeaderAuthorization = "Authorization"
// HeaderAccept is the HTTP Accept Type header.
HeaderAccept = "Accept"
// ContentTypeJSON is the Content-Type value for a JSON response.
ContentTypeJSON = "application/json"
defaultTTL = 5 * time.Second
initialRetryInterval = 200 * time.Millisecond
maxRetryInterval = 1 * time.Second
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// Requester is used to make outbound requests with
type Requester interface {
Get(url string, headers ...Header) (response []byte, responseHeaders http.Header, code int, err error)
GetObj(url string, result interface{}, headers ...Header) error
Post(url string, body interface{}, headers ...Header) (response []byte, responseHeaders http.Header, code int, err error)
PostObj(url string, body interface{}, result interface{}, headers ...Header) error
String() string
}
// Header element to be sent
type Header struct {
Name, Value string
}
// Client sets http client
func Client(client http.Client) func(r *HTTPRequester) {
return func(r *HTTPRequester) {
r.client = client
}
}
// Timeout sets http client timeout
func Timeout(timeout time.Duration) func(r *HTTPRequester) {
return func(r *HTTPRequester) {
r.client.Timeout = timeout
}
}
// Retries sets max number of retries for failed calls
func Retries(retries int) func(r *HTTPRequester) {
return func(r *HTTPRequester) {
r.retries = retries
}
}
// Headers sets request headers
func Headers(headers ...Header) func(r *HTTPRequester) {
return func(r *HTTPRequester) {
r.headers = []Header{}
r.headers = append(r.headers, headers...)
}
}
// HTTPRequester contains main info
type HTTPRequester struct {
client http.Client
retries int
headers []Header
logger logging.OptimizelyLogProducer
}
// NewHTTPRequester makes Requester with api and parameters. Sets defaults
// api has the base part of request's url, like http://localhost/api/v1
func NewHTTPRequester(logger logging.OptimizelyLogProducer, params ...func(*HTTPRequester)) *HTTPRequester {
res := HTTPRequester{
retries: 1,
headers: []Header{{HeaderContentType, ContentTypeJSON}, {HeaderAccept, ContentTypeJSON}},
client: http.Client{Timeout: defaultTTL},
logger: logger,
}
for _, param := range params {
param(&res)
}
return &res
}
// Get executes HTTP GET with url and optional extra headers, returns body in []bytes
func (r HTTPRequester) Get(url string, headers ...Header) (response []byte, responseHeaders http.Header, code int, err error) {
return r.Do(url, "GET", nil, headers)
}
// GetObj executes HTTP GET with url and optional extra headers, returns filled object
func (r HTTPRequester) GetObj(url string, result interface{}, headers ...Header) error {
b, _, _, err := r.Do(url, "GET", nil, headers)
if err != nil {
return err
}
return json.Unmarshal(b, result)
}
// Post executes HTTP POST with url, body and optional extra headers
func (r HTTPRequester) Post(url string, body interface{}, headers ...Header) (response []byte, responseHeaders http.Header, code int, err error) {
b, err := json.Marshal(body)
if err != nil {
return nil, nil, http.StatusBadRequest, err
}
return r.Do(url, "POST", bytes.NewBuffer(b), headers)
}
// PostObj executes HTTP POST with url, body and optional extra headers. Returns filled object
func (r HTTPRequester) PostObj(url string, body, result interface{}, headers ...Header) error {
b, _, _, err := r.Post(url, body, headers...)
if err != nil {
return err
}
return json.Unmarshal(b, result)
}
// Do executes request and returns response body for requested url
func (r HTTPRequester) Do(url, method string, body io.Reader, headers []Header) (response []byte, responseHeaders http.Header, code int, err error) {
single := func(request *http.Request) (response []byte, responseHeaders http.Header, code int, e error) {
resp, doErr := r.client.Do(request)
if doErr != nil {
r.logger.Error(fmt.Sprintf("failed to send request %v", request), doErr)
return nil, http.Header{}, 0, doErr
}
defer func() {
if e := resp.Body.Close(); e != nil {
r.logger.Warning(fmt.Sprintf("can't close body for %s request, %s", request.URL, e))
}
}()
if response, err = io.ReadAll(resp.Body); err != nil {
r.logger.Error("failed to read body", err)
return nil, resp.Header, resp.StatusCode, err
}
if resp.StatusCode >= http.StatusBadRequest {
r.logger.Warning(fmt.Sprintf("error status code=%d", resp.StatusCode))
return response, resp.Header, resp.StatusCode, errors.New(resp.Status)
}
return response, resp.Header, resp.StatusCode, nil
}
r.logger.Debug(fmt.Sprintf("request %s", url))
req, err := http.NewRequest(method, url, body)
if err != nil {
r.logger.Error(fmt.Sprintf("failed to make request %s", url), err)
return nil, nil, 0, err
}
r.addHeaders(req, headers)
for i := 0; i < r.retries; i++ {
if response, responseHeaders, code, err = single(req); err == nil {
triedMsg := ""
if i > 0 {
triedMsg = fmt.Sprintf(", tried %d time(s)", i+1)
}
r.logger.Debug(fmt.Sprintf("completed %s%s", url, triedMsg))
return response, responseHeaders, code, err
}
r.logger.Debug(fmt.Sprintf("failed %s with %v", url, err))
if i < r.retries-1 {
// Exponential backoff: 200ms, 400ms, 800ms, ... capped at 1s
delay := initialRetryInterval * time.Duration(1<<i)
if delay > maxRetryInterval {
delay = maxRetryInterval
}
r.logger.Debug(fmt.Sprintf("retrying request (attempt %d of %d) after %v", i+2, r.retries, delay))
time.Sleep(delay)
}
}
return response, responseHeaders, code, err
}
func (r HTTPRequester) addHeaders(req *http.Request, headers []Header) *http.Request {
// Create a map to track which headers have been set
// This ensures that headers from the 'headers' parameter (passed at call time)
// override headers from r.headers (set during requester initialization)
headerMap := make(map[string]string)
// First, add all internal headers to the map
for _, h := range r.headers {
headerMap[h.Name] = h.Value
}
// Then, override with any headers passed at call time
for _, h := range headers {
headerMap[h.Name] = h.Value
}
// Finally, set all headers on the request
for name, value := range headerMap {
req.Header.Set(name, value)
}
return req
}
func (r HTTPRequester) String() string {
return fmt.Sprintf("{timeout: %v, retries: %d}", r.client.Timeout, r.retries)
}