-
Notifications
You must be signed in to change notification settings - Fork 40
/
client.go
569 lines (477 loc) · 15.6 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package client
import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"math"
"net"
"net/http"
"net/url"
"runtime"
"strconv"
"strings"
"time"
"github.com/hashicorp/go-azure-sdk/sdk/auth"
"github.com/hashicorp/go-azure-sdk/sdk/internal/accept"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/hashicorp/go-retryablehttp"
)
// RetryOn404ConsistencyFailureFunc can be used to retry a request when a 404 response is received
func RetryOn404ConsistencyFailureFunc(resp *http.Response, _ *odata.OData) (bool, error) {
return resp != nil && resp.StatusCode == http.StatusNotFound, nil
}
// RequestRetryAny wraps multiple RequestRetryFuncs and calls them in turn, returning true if any func returns true
func RequestRetryAny(retryFuncs ...RequestRetryFunc) func(resp *http.Response, o *odata.OData) (bool, error) {
return func(resp *http.Response, o *odata.OData) (retry bool, err error) {
for _, retryFunc := range retryFuncs {
if retryFunc != nil {
retry, err = retryFunc(resp, o)
if err != nil {
return
}
if retry {
return
}
}
}
return false, nil
}
}
// RequestRetryAll wraps multiple RequestRetryFuncs and calls them in turn, only returning true if all funcs return true
func RequestRetryAll(retryFuncs ...RequestRetryFunc) func(resp *http.Response, o *odata.OData) (bool, error) {
return func(resp *http.Response, o *odata.OData) (retry bool, err error) {
for _, retryFunc := range retryFuncs {
if retryFunc != nil {
retry, err = retryFunc(resp, o)
if err != nil {
return
}
if !retry {
return
}
}
}
return true, nil
}
}
// RetryableErrorHandler ensures that the response is returned after exhausting retries for a request
// We mustn't return an error here, or net/http will not return the response
func RetryableErrorHandler(resp *http.Response, _ error, _ int) (*http.Response, error) {
return resp, nil
}
// Request embeds *http.Request and adds useful metadata
type Request struct {
RetryFunc RequestRetryFunc
ValidStatusCodes []int
ValidStatusFunc ValidStatusFunc
Client BaseClient
// Embed *http.Request so that we can send this to an *http.Client
*http.Request
}
func (r *Request) Marshal(payload interface{}) error {
contentType := strings.ToLower(r.Header.Get("Content-Type"))
if strings.Contains(contentType, "application/json") {
body, err := json.Marshal(payload)
if err == nil {
r.ContentLength = int64(len(body))
r.Body = io.NopCloser(bytes.NewReader(body))
}
return nil
}
if strings.Contains(contentType, "application/xml") || strings.Contains(contentType, "text/xml") {
body, err := xml.Marshal(payload)
if err == nil {
r.ContentLength = int64(len(body))
r.Body = io.NopCloser(bytes.NewReader(body))
}
return nil
}
if strings.Contains(contentType, "application/octet-stream") {
v, ok := payload.([]byte)
if !ok {
return fmt.Errorf("internal-error: `payload` must be []byte but got %+v", payload)
}
r.ContentLength = int64(len(v))
r.Body = io.NopCloser(bytes.NewReader(v))
}
return fmt.Errorf("internal-error: unimplemented marshal function for content type %q", contentType)
}
func (r *Request) Execute(ctx context.Context) (*Response, error) {
return r.Client.Execute(ctx, r)
}
func (r *Request) ExecutePaged(ctx context.Context) (*Response, error) {
return r.Client.ExecutePaged(ctx, r)
}
func (r *Request) IsIdempotent() bool {
switch strings.ToUpper(r.Method) {
case http.MethodGet, http.MethodHead, http.MethodOptions:
return true
}
return false
}
// Response embeds *http.Response and adds useful methods
type Response struct {
OData *odata.OData
// Embed *http.Response
*http.Response
}
func (r *Response) Unmarshal(model interface{}) error {
if model == nil {
return fmt.Errorf("model was nil")
}
contentType := strings.ToLower(r.Header.Get("Content-Type"))
if contentType == "" {
// some APIs (e.g. Storage Data Plane) don't return a content type... so we'll assume from the Accept header
acc, err := accept.FromString(r.Request.Header.Get("Accept"))
if err != nil {
if preferred := acc.FirstChoice(); preferred != nil {
contentType = preferred.ContentType
}
}
if contentType == "" {
// fall back on request media type
contentType = strings.ToLower(r.Request.Header.Get("Content-Type"))
}
}
if strings.Contains(contentType, "application/json") {
// Read the response body and close it
respBody, err := io.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("parsing response body: %+v", err)
}
r.Body.Close()
// Trim away a BOM if present
respBody = bytes.TrimPrefix(respBody, []byte("\xef\xbb\xbf"))
// Unmarshal into provided model
if err := json.Unmarshal(respBody, model); err != nil {
return fmt.Errorf("unmarshaling response body: %+v", err)
}
// Reassign the response body as downstream code may expect it
r.Body = io.NopCloser(bytes.NewBuffer(respBody))
}
if strings.Contains(contentType, "application/xml") || strings.Contains(contentType, "text/xml") {
// Read the response body and close it
respBody, err := io.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("could not parse response body")
}
r.Body.Close()
// Trim away a BOM if present
respBody = bytes.TrimPrefix(respBody, []byte("\xef\xbb\xbf"))
// Unmarshal into provided model
if err := xml.Unmarshal(respBody, model); err != nil {
return err
}
// Reassign the response body as downstream code may expect it
r.Body = io.NopCloser(bytes.NewBuffer(respBody))
}
if strings.Contains(contentType, "application/octet-stream") {
if _, ok := model.([]byte); !ok {
return fmt.Errorf("internal-error: `model` must be []byte but got %+v", model)
}
// Read the response body and close it
respBody, err := io.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("could not parse response body")
}
r.Body.Close()
// Trim away a BOM if present
respBody = bytes.TrimPrefix(respBody, []byte("\xef\xbb\xbf"))
// copy the byte stream across
model = respBody
// Reassign the response body as downstream code may expect it
r.Body = io.NopCloser(bytes.NewBuffer(respBody))
}
return nil
}
// Client is a base client to be used by API-specific clients. It satisfies the BaseClient interface.
type Client struct {
// BaseUri is the base endpoint for this API.
BaseUri string
// UserAgent is the HTTP user agent string to send in requests.
UserAgent string
// CorrelationId is a custom correlation ID which can be added to requests for tracing purposes
CorrelationId string
// Authorizer is anything that can provide an access token with which to authorize requests.
Authorizer auth.Authorizer
// DisableRetries prevents the client from reattempting failed requests (which it does to work around eventual consistency issues).
// This does not impact handling of retries related to rate limiting, which are always performed.
DisableRetries bool
// RequestMiddlewares is a slice of functions that are called in order before a request is sent
RequestMiddlewares *[]RequestMiddleware
// ResponseMiddlewares is a slice of functions that are called in order before a response is parsed and returned
ResponseMiddlewares *[]ResponseMiddleware
}
// NewClient returns a new Client configured with sensible defaults
func NewClient(baseUri string, serviceName, apiVersion string) *Client {
segments := []string{
"Go-http-Client/1.1",
fmt.Sprintf("%s/%s", serviceName, apiVersion),
}
return &Client{
BaseUri: baseUri,
UserAgent: fmt.Sprintf("HashiCorp/go-azure-sdk (%s)", strings.Join(segments, " ")),
}
}
// NewRequest configures a new *Request
func (c *Client) NewRequest(ctx context.Context, input RequestOptions) (*Request, error) {
req := (&http.Request{}).WithContext(ctx)
req.Method = input.HttpMethod
req.Header = make(http.Header)
req.Header.Add("Content-Type", input.ContentType)
if c.UserAgent != "" {
req.Header.Add("User-Agent", c.UserAgent)
}
if c.CorrelationId != "" {
req.Header.Add("X-Ms-Correlation-Request-Id", c.CorrelationId)
}
path := strings.TrimPrefix(input.Path, "/")
u, err := url.ParseRequestURI(fmt.Sprintf("%s/%s", c.BaseUri, path))
if err != nil {
return nil, err
}
req.Host = u.Host
req.URL = u
ret := Request{
Client: c,
Request: req,
ValidStatusCodes: input.ExpectedStatusCodes,
}
return &ret, nil
}
// Execute is used by the package to send an HTTP request to the API
func (c *Client) Execute(ctx context.Context, req *Request) (*Response, error) {
if req.Request == nil {
return nil, fmt.Errorf("req.Request was nil")
}
// at this point we're ready to send the HTTP Request, as such let's get the Authorization token
// and add that to the request
if c.Authorizer != nil {
token, err := c.Authorizer.Token(ctx, req.Request)
if err != nil {
return nil, err
}
token.SetAuthHeader(req.Request)
}
var err error
// Check we can read the request body and set a default empty body
var reqBody []byte
if req.Body != nil {
reqBody, err = io.ReadAll(req.Body)
if err != nil {
return nil, fmt.Errorf("reading request body: %v", err)
}
req.Body = io.NopCloser(bytes.NewBuffer(reqBody))
req.Header.Set("Content-Length", fmt.Sprintf("%d", req.ContentLength))
}
// Instantiate a RetryableHttp client and configure its CheckRetry func
r := c.retryableClient(func(ctx context.Context, r *http.Response, err error) (bool, error) {
// First check for badly malformed responses
if r == nil {
if req.IsIdempotent() {
return true, nil
}
return false, fmt.Errorf("HTTP response was nil; connection may have been reset")
}
// Eventual consistency checks
if !c.DisableRetries {
if r.StatusCode == http.StatusFailedDependency {
return true, nil
}
o, err := odata.FromResponse(r)
if err != nil {
return false, err
}
if f := req.RetryFunc; f != nil {
shouldRetry, err := f(r, o)
if err != nil || shouldRetry {
return shouldRetry, err
}
}
}
// Fall back to default retry policy to handle rate limiting, server errors etc.
return retryablehttp.DefaultRetryPolicy(ctx, r, err)
})
// Derive an *http.Client for sending the request
client := r.StandardClient()
// Configure any RequestMiddlewares
if c.RequestMiddlewares != nil {
for _, m := range *c.RequestMiddlewares {
r, err := m(req.Request)
if err != nil {
return nil, err
}
req.Request = r
}
}
// Send the request
resp := &Response{}
resp.Response, err = client.Do(req.Request)
if err != nil {
return resp, err
}
if resp.Response == nil {
return resp, fmt.Errorf("HTTP response was nil; connection may have been reset")
}
// Configure any ResponseMiddlewares
if c.ResponseMiddlewares != nil {
for _, m := range *c.ResponseMiddlewares {
r, err := m(req.Request, resp.Response)
if err != nil {
return resp, err
}
resp.Response = r
}
}
// Extract OData from response
var o *odata.OData
resp.OData, err = odata.FromResponse(resp.Response)
if err != nil {
return resp, err
}
if resp == nil {
return resp, fmt.Errorf("nil response received")
}
// Determine whether response status is valid
if !containsStatusCode(req.ValidStatusCodes, resp.StatusCode) {
if f := req.ValidStatusFunc; f != nil && f(resp.Response, o) {
return resp, nil
}
// Determine suitable error text
var errText string
switch {
case o != nil && o.Error != nil && o.Error.String() != "":
errText = fmt.Sprintf("error: %s", o.Error)
default:
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return resp, fmt.Errorf("unexpected status %d, could not read response body", resp.StatusCode)
}
if len(respBody) == 0 {
return resp, fmt.Errorf("unexpected status %d received with no body", resp.StatusCode)
}
errText = fmt.Sprintf("response: %s", respBody)
}
return resp, fmt.Errorf("unexpected status %d with %s", resp.StatusCode, errText)
}
return resp, nil
}
// ExecutePaged automatically pages through the results of Execute
func (c *Client) ExecutePaged(ctx context.Context, req *Request) (*Response, error) {
// Perform the request
resp, err := c.Execute(ctx, req)
if err != nil {
return resp, err
}
// Check for json content before handling pagination
contentType := strings.ToLower(resp.Header.Get("Content-Type"))
if !strings.HasPrefix(contentType, "application/json") {
return resp, fmt.Errorf("unsupported content-type %q received, only application/json is supported for paged results", contentType)
}
// Read the response body and close it
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return resp, fmt.Errorf("could not parse response body")
}
resp.Body.Close()
// Unmarshal firstOdata
var firstOdata odata.OData
if err := json.Unmarshal(respBody, &firstOdata); err != nil {
return resp, err
}
firstValue, ok := firstOdata.Value.([]interface{})
if firstOdata.NextLink == nil || firstValue == nil || !ok {
// No more pages, reassign response body and return
resp.Body = io.NopCloser(bytes.NewBuffer(respBody))
return resp, nil
}
// Get the next page, recursively
// TODO: may have to accommodate APIs with nonstandard paging
nextReq := req
u, err := url.Parse(string(*firstOdata.NextLink))
if err != nil {
return resp, err
}
nextReq.URL = u
nextResp, err := c.ExecutePaged(ctx, req)
if err != nil {
return resp, err
}
// Read the next page response body and close it
nextRespBody, err := io.ReadAll(nextResp.Body)
if err != nil {
return resp, fmt.Errorf("could not parse response body")
}
nextResp.Body.Close()
// Unmarshal nextOdata from the next page
var nextOdata odata.OData
if err := json.Unmarshal(nextRespBody, &nextOdata); err != nil {
return nextResp, err
}
// When next page has results, append to current page
if nextValue, ok := nextOdata.Value.([]interface{}); ok {
value := append(firstValue, nextValue...)
nextOdata.Value = &value
}
// Marshal the entire result, along with fields from the final page
newJson, err := json.Marshal(nextOdata)
if err != nil {
return nextResp, err
}
// Reassign the response body
resp.Body = io.NopCloser(bytes.NewBuffer(newJson))
return resp, nil
}
// retryableClient instantiates a new *retryablehttp.Client having the provided checkRetry func
func (c *Client) retryableClient(checkRetry retryablehttp.CheckRetry) (r *retryablehttp.Client) {
r = retryablehttp.NewClient()
r.Backoff = func(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
if resp != nil {
// Always look for Retry-After header
if s, ok := resp.Header["Retry-After"]; ok {
if sleep, err := strconv.ParseInt(s[0], 10, 64); err == nil {
return time.Second * time.Duration(sleep)
}
}
}
// Default exponential backoff
mult := math.Pow(2, float64(attemptNum)) * float64(min)
sleep := time.Duration(mult)
if float64(sleep) != mult || sleep > max {
sleep = max
}
return sleep
}
r.CheckRetry = checkRetry
r.ErrorHandler = RetryableErrorHandler
r.Logger = nil
r.HTTPClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
d := &net.Dialer{Resolver: &net.Resolver{}}
return d.DialContext(ctx, network, addr)
},
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ForceAttemptHTTP2: true,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
},
}
return
}
// containsStatusCode determines whether the returned status code is in the []int of expected status codes.
func containsStatusCode(expected []int, actual int) bool {
for _, v := range expected {
if actual == v {
return true
}
}
return false
}