-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathclient.go
598 lines (505 loc) · 18.5 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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package msgraph
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/hashicorp/go-azure-sdk/sdk/auth"
"github.com/hashicorp/go-azure-sdk/sdk/environments"
"github.com/hashicorp/go-azure-sdk/sdk/odata"
"github.com/hashicorp/go-retryablehttp"
)
type ApiVersion string
const (
Version10 ApiVersion = "v1.0"
VersionBeta ApiVersion = "beta"
)
// ConsistencyFailureFunc is a function that determines whether an HTTP request has failed due to eventual consistency and should be retried
type ConsistencyFailureFunc func(*http.Response, *odata.OData) bool
// RequestMiddleware can manipulate or log a request before it is sent
type RequestMiddleware func(*http.Request) (*http.Request, error)
// ResponseMiddleware can manipulate or log a response before it is parsed and returned
type ResponseMiddleware func(*http.Request, *http.Response) (*http.Response, error)
// RetryOn404ConsistencyFailureFunc can be used to retry a request when a 404 response is received
func RetryOn404ConsistencyFailureFunc(resp *http.Response, _ *odata.OData) bool {
return resp != nil && resp.StatusCode == http.StatusNotFound
}
// ValidStatusFunc is a function that tests whether an HTTP response is considered valid for the particular request.
type ValidStatusFunc func(*http.Response, *odata.OData) bool
// HttpRequestInput is any type that can validate the response to an HTTP request.
type HttpRequestInput interface {
GetConsistencyFailureFunc() ConsistencyFailureFunc
GetContentType() string
GetOData() odata.Query
GetValidStatusCodes() []int
GetValidStatusFunc() ValidStatusFunc
}
// Uri represents a Microsoft Graph endpoint.
type Uri struct {
Entity string
Params url.Values
}
// RetryableErrorHandler ensures that the response is returned after exhausting retries for a request
// We can't return an error here, or net/http will not return the response
func RetryableErrorHandler(resp *http.Response, err error, numTries int) (*http.Response, error) {
if resp == nil {
return nil, err
}
return resp, nil
}
// Client is a base client to be used by clients for specific entities.
// It can send GET, POST, PUT, PATCH and DELETE requests to Microsoft Graph and is API version and tenant aware.
type Client struct {
// Endpoint is the base endpoint for Microsoft Graph, usually "https://graph.microsoft.com".
Endpoint string
// ApiVersion is the Microsoft Graph API version to use.
ApiVersion ApiVersion
// UserAgent is the HTTP user agent string to send in requests.
UserAgent 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
// HttpClient is the underlying http.Client, which by default uses a retryable client
HttpClient *http.Client
RetryableClient *retryablehttp.Client
}
// NewClient returns a new Client configured with the specified API version and tenant ID.
func NewClient(apiVersion ApiVersion) Client {
r := retryablehttp.NewClient()
r.ErrorHandler = RetryableErrorHandler
r.Logger = nil
var endpoint string
if defaultEndpoint, _ := environments.AzurePublic().MicrosoftGraph.Endpoint(); defaultEndpoint != nil {
endpoint = *defaultEndpoint
}
return Client{
Endpoint: endpoint,
ApiVersion: apiVersion,
UserAgent: "Hamilton (Go-http-client/1.1)",
HttpClient: r.StandardClient(),
RetryableClient: r,
}
}
// buildUri is used by the package to build a complete URI string for API requests.
func (c Client) buildUri(uri Uri) (string, error) {
newUrl, err := url.Parse(string(c.Endpoint))
if err != nil {
return "", err
}
newUrl.Path = "/" + string(c.ApiVersion)
newUrl.Path = fmt.Sprintf("%s/%s", newUrl.Path, strings.TrimLeft(uri.Entity, "/"))
if uri.Params != nil {
newUrl.RawQuery = uri.Params.Encode()
}
return newUrl.String(), nil
}
// performRequest is used by the package to send an HTTP request to the API.
func (c Client) performRequest(req *http.Request, input HttpRequestInput) (*http.Response, int, *odata.OData, error) {
var status int
query := input.GetOData()
req.Header = query.AppendHeaders(req.Header)
req.Header.Add("Content-Type", input.GetContentType())
if c.Authorizer != nil {
token, err := c.Authorizer.Token(req.Context(), req)
if err != nil {
return nil, status, nil, err
}
token.SetAuthHeader(req)
}
if c.UserAgent != "" {
req.Header.Add("User-Agent", c.UserAgent)
}
var resp *http.Response
var o *odata.OData
var err error
var reqBody []byte
if req.Body != nil {
reqBody, err = io.ReadAll(req.Body)
if err != nil {
return nil, status, nil, fmt.Errorf("reading request body: %v", err)
}
}
c.RetryableClient.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
if resp != nil && !c.DisableRetries {
if resp.StatusCode == http.StatusFailedDependency {
return true, nil
}
o, err = odata.FromResponse(resp)
if err != nil {
return false, err
}
f := input.GetConsistencyFailureFunc()
if f != nil && f(resp, o) {
return true, nil
}
}
return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
}
req.Body = io.NopCloser(bytes.NewBuffer(reqBody))
if c.RequestMiddlewares != nil {
for _, m := range *c.RequestMiddlewares {
r, err := m(req)
if err != nil {
return nil, status, nil, err
}
req = r
}
}
resp, err = c.HttpClient.Do(req)
if err != nil {
return nil, status, nil, err
}
if c.ResponseMiddlewares != nil {
for _, m := range *c.ResponseMiddlewares {
r, err := m(req, resp)
if err != nil {
return nil, status, nil, err
}
resp = r
}
}
o, err = odata.FromResponse(resp)
if err != nil {
return nil, status, o, err
}
if resp == nil {
return resp, status, o, fmt.Errorf("nil response received")
}
status = resp.StatusCode
if !containsStatusCode(input.GetValidStatusCodes(), status) {
f := input.GetValidStatusFunc()
if f != nil && f(resp, o) {
return resp, status, o, nil
}
var errText string
switch {
case o != nil && o.Error != nil && o.Error.String() != "":
errText = fmt.Sprintf("OData error: %s", o.Error)
default:
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, o, fmt.Errorf("unexpected status %d, could not read response body", status)
}
if len(respBody) == 0 {
return nil, status, o, fmt.Errorf("unexpected status %d received with no body", status)
}
errText = fmt.Sprintf("response: %s", respBody)
}
return nil, status, o, fmt.Errorf("unexpected status %d with %s", status, errText)
}
return resp, status, o, nil
}
// 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
}
// DeleteHttpRequestInput configures a DELETE request.
type DeleteHttpRequestInput struct {
ConsistencyFailureFunc ConsistencyFailureFunc
OData odata.Query
ValidStatusCodes []int
ValidStatusFunc ValidStatusFunc
Uri Uri
}
// GetConsistencyFailureFunc returns a function used to evaluate whether a failed request is due to eventual consistency and should be retried.
func (i DeleteHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc {
return i.ConsistencyFailureFunc
}
// GetContentType returns the content type for the request, currently only application/json is supported
func (i DeleteHttpRequestInput) GetContentType() string {
return "application/json; charset=utf-8"
}
// GetOData returns the OData request metadata
func (i DeleteHttpRequestInput) GetOData() odata.Query {
return i.OData
}
// GetValidStatusCodes returns a []int of status codes considered valid for a DELETE request.
func (i DeleteHttpRequestInput) GetValidStatusCodes() []int {
return i.ValidStatusCodes
}
// GetValidStatusFunc returns a function used to evaluate whether the response to a DELETE request is considered valid.
func (i DeleteHttpRequestInput) GetValidStatusFunc() ValidStatusFunc {
return i.ValidStatusFunc
}
// Delete performs a DELETE request.
func (c Client) Delete(ctx context.Context, input DeleteHttpRequestInput) (*http.Response, int, *odata.OData, error) {
var status int
url, err := c.buildUri(input.Uri)
if err != nil {
return nil, status, nil, fmt.Errorf("unable to make request: %v", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, url, http.NoBody)
if err != nil {
return nil, status, nil, err
}
resp, status, o, err := c.performRequest(req, input)
if err != nil {
return nil, status, o, err
}
return resp, status, o, nil
}
// GetHttpRequestInput configures a GET request.
type GetHttpRequestInput struct {
ConsistencyFailureFunc ConsistencyFailureFunc
DisablePaging bool
OData odata.Query
ValidStatusCodes []int
ValidStatusFunc ValidStatusFunc
Uri Uri
rawUri string
}
// GetConsistencyFailureFunc returns a function used to evaluate whether a failed request is due to eventual consistency and should be retried.
func (i GetHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc {
return i.ConsistencyFailureFunc
}
// GetContentType returns the content type for the request, currently only application/json is supported
func (i GetHttpRequestInput) GetContentType() string {
return "application/json; charset=utf-8"
}
// GetOData returns the OData request metadata
func (i GetHttpRequestInput) GetOData() odata.Query {
return i.OData
}
// GetValidStatusCodes returns a []int of status codes considered valid for a GET request.
func (i GetHttpRequestInput) GetValidStatusCodes() []int {
return i.ValidStatusCodes
}
// GetValidStatusFunc returns a function used to evaluate whether the response to a GET request is considered valid.
func (i GetHttpRequestInput) GetValidStatusFunc() ValidStatusFunc {
return i.ValidStatusFunc
}
// Get performs a GET request.
func (c Client) Get(ctx context.Context, input GetHttpRequestInput) (*http.Response, int, *odata.OData, error) {
var status int
// Check for a raw uri, else build one from the Uri field
url := input.rawUri
if url == "" {
// Append odata query parameters
input.Uri.Params = input.OData.AppendValues(input.Uri.Params)
var err error
url, err = c.buildUri(input.Uri)
if err != nil {
return nil, status, nil, fmt.Errorf("unable to make request: %v", err)
}
}
// Build a new request
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return nil, status, nil, err
}
// Perform the request
resp, status, o, err := c.performRequest(req, input)
if err != nil {
return nil, status, o, err
}
// Check for json content before handling pagination
contentType := strings.ToLower(resp.Header.Get("Content-Type"))
if strings.HasPrefix(contentType, "application/json") {
// Read the response body and close it
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, o, fmt.Errorf("could not parse response body")
}
resp.Body.Close()
// Unmarshall firstOdata
var firstOdata odata.OData
if err := json.Unmarshal(respBody, &firstOdata); err != nil {
return nil, status, o, err
}
firstValue, ok := firstOdata.Value.([]interface{})
if input.DisablePaging || firstOdata.NextLink == nil || firstValue == nil || !ok {
// No more pages, reassign response body and return
resp.Body = io.NopCloser(bytes.NewBuffer(respBody))
return resp, status, o, nil
}
// Get the next page, recursively
nextInput := input
nextInput.rawUri = string(*firstOdata.NextLink)
nextResp, status, o, err := c.Get(ctx, nextInput)
if err != nil {
return resp, status, o, err
}
// Read the next page response body and close it
nextRespBody, err := io.ReadAll(nextResp.Body)
if err != nil {
return nil, status, o, fmt.Errorf("could not parse response body")
}
nextResp.Body.Close()
// Unmarshall firstOdata from the next page
var nextOdata odata.OData
if err := json.Unmarshal(nextRespBody, &nextOdata); err != nil {
return resp, status, o, err
}
if nextValue, ok := nextOdata.Value.([]interface{}); ok {
// Next page has results, append to current page
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 resp, status, o, err
}
// Reassign the response body
resp.Body = io.NopCloser(bytes.NewBuffer(newJson))
}
return resp, status, o, nil
}
// PatchHttpRequestInput configures a PATCH request.
type PatchHttpRequestInput struct {
ConsistencyFailureFunc ConsistencyFailureFunc
Body []byte
OData odata.Query
ValidStatusCodes []int
ValidStatusFunc ValidStatusFunc
Uri Uri
}
// GetConsistencyFailureFunc returns a function used to evaluate whether a failed request is due to eventual consistency and should be retried.
func (i PatchHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc {
return i.ConsistencyFailureFunc
}
// GetContentType returns the content type for the request, currently only application/json is supported
func (i PatchHttpRequestInput) GetContentType() string {
return "application/json; charset=utf-8"
}
// GetOData returns the OData request metadata
func (i PatchHttpRequestInput) GetOData() odata.Query {
return i.OData
}
// GetValidStatusCodes returns a []int of status codes considered valid for a PATCH request.
func (i PatchHttpRequestInput) GetValidStatusCodes() []int {
return i.ValidStatusCodes
}
// GetValidStatusFunc returns a function used to evaluate whether the response to a PATCH request is considered valid.
func (i PatchHttpRequestInput) GetValidStatusFunc() ValidStatusFunc {
return i.ValidStatusFunc
}
// Patch performs a PATCH request.
func (c Client) Patch(ctx context.Context, input PatchHttpRequestInput) (*http.Response, int, *odata.OData, error) {
var status int
url, err := c.buildUri(input.Uri)
if err != nil {
return nil, status, nil, fmt.Errorf("unable to make request: %v", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, url, bytes.NewBuffer(input.Body))
if err != nil {
return nil, status, nil, err
}
resp, status, o, err := c.performRequest(req, input)
if err != nil {
return nil, status, o, err
}
return resp, status, o, nil
}
// PostHttpRequestInput configures a POST request.
type PostHttpRequestInput struct {
Body []byte
ConsistencyFailureFunc ConsistencyFailureFunc
OData odata.Query
ValidStatusCodes []int
ValidStatusFunc ValidStatusFunc
Uri Uri
}
// GetConsistencyFailureFunc returns a function used to evaluate whether a failed request is due to eventual consistency and should be retried.
func (i PostHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc {
return i.ConsistencyFailureFunc
}
// GetContentType returns the content type for the request, currently only application/json is supported
func (i PostHttpRequestInput) GetContentType() string {
return "application/json; charset=utf-8"
}
// GetOData returns the OData request metadata
func (i PostHttpRequestInput) GetOData() odata.Query {
return i.OData
}
// GetValidStatusCodes returns a []int of status codes considered valid for a POST request.
func (i PostHttpRequestInput) GetValidStatusCodes() []int {
return i.ValidStatusCodes
}
// GetValidStatusFunc returns a function used to evaluate whether the response to a POST request is considered valid.
func (i PostHttpRequestInput) GetValidStatusFunc() ValidStatusFunc {
return i.ValidStatusFunc
}
// Post performs a POST request.
func (c Client) Post(ctx context.Context, input PostHttpRequestInput) (*http.Response, int, *odata.OData, error) {
var status int
url, err := c.buildUri(input.Uri)
if err != nil {
return nil, status, nil, fmt.Errorf("unable to make request: %v", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(input.Body))
if err != nil {
return nil, status, nil, err
}
resp, status, o, err := c.performRequest(req, input)
if err != nil {
return nil, status, o, err
}
return resp, status, o, nil
}
// PutHttpRequestInput configures a PUT request.
type PutHttpRequestInput struct {
ConsistencyFailureFunc ConsistencyFailureFunc
ContentType string
Body []byte
OData odata.Query
ValidStatusCodes []int
ValidStatusFunc ValidStatusFunc
Uri Uri
}
// GetConsistencyFailureFunc returns a function used to evaluate whether a failed request is due to eventual consistency and should be retried.
func (i PutHttpRequestInput) GetConsistencyFailureFunc() ConsistencyFailureFunc {
return i.ConsistencyFailureFunc
}
// GetContentType returns the content type for the request, defaults to application/json
func (i PutHttpRequestInput) GetContentType() string {
if i.ContentType != "" {
return i.ContentType
}
return "application/json; charset=utf-8"
}
// GetOData returns the OData request metadata
func (i PutHttpRequestInput) GetOData() odata.Query {
return i.OData
}
// GetValidStatusCodes returns a []int of status codes considered valid for a PUT request.
func (i PutHttpRequestInput) GetValidStatusCodes() []int {
return i.ValidStatusCodes
}
// GetValidStatusFunc returns a function used to evaluate whether the response to a PUT request is considered valid.
func (i PutHttpRequestInput) GetValidStatusFunc() ValidStatusFunc {
return i.ValidStatusFunc
}
// Put performs a PUT request.
func (c Client) Put(ctx context.Context, input PutHttpRequestInput) (*http.Response, int, *odata.OData, error) {
var status int
url, err := c.buildUri(input.Uri)
if err != nil {
return nil, status, nil, fmt.Errorf("unable to make request: %v", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bytes.NewBuffer(input.Body))
if err != nil {
return nil, status, nil, err
}
resp, status, o, err := c.performRequest(req, input)
if err != nil {
return nil, status, o, err
}
return resp, status, o, nil
}