-
Notifications
You must be signed in to change notification settings - Fork 46
/
client.go
94 lines (75 loc) · 2.46 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package msgraph
import (
"context"
"fmt"
"net/url"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/environments"
)
var _ client.BaseClient = &Client{}
type ApiVersion string
const (
VersionOnePointZero ApiVersion = "v1.0"
VersionBeta ApiVersion = "beta"
)
type Client struct {
*client.Client
// EnableRetries allows reattempting failed requests to work around eventual consistency issues
// Note that 429 responses are always handled by the base client regardless of this setting
EnableRetries bool
// apiVersion specifies the version of the API being used, either "beta" or "v1.0"
apiVersion ApiVersion
// tenantId is the tenant ID to use in requests
tenantId string
}
func NewMsGraphClient(api environments.Api, serviceName string, apiVersion ApiVersion) (*Client, error) {
endpoint, ok := api.Endpoint()
if !ok {
return nil, fmt.Errorf("no `endpoint` was returned for this environment")
}
baseUri := fmt.Sprintf("%s/%s", *endpoint, apiVersion)
baseClient := client.NewClient(baseUri, fmt.Sprintf("MicrosoftGraph-%s", serviceName), string(apiVersion))
return &Client{
Client: baseClient,
EnableRetries: true,
apiVersion: apiVersion,
}, nil
}
func (c *Client) NewRequest(ctx context.Context, input client.RequestOptions) (*client.Request, error) {
if _, ok := ctx.Deadline(); !ok {
return nil, fmt.Errorf("the context used must have a deadline attached for polling purposes, but got no deadline")
}
if err := input.Validate(); err != nil {
return nil, fmt.Errorf("pre-validating request payload: %+v", err)
}
req, err := c.Client.NewRequest(ctx, input)
if err != nil {
return nil, fmt.Errorf("building %s request: %+v", input.HttpMethod, err)
}
req.Client = c
query := url.Values{}
if input.OptionsObject != nil {
if h := input.OptionsObject.ToHeaders(); h != nil {
for k, v := range h.Headers() {
req.Header[k] = v
}
}
if q := input.OptionsObject.ToQuery(); q != nil {
for k, v := range q.Values() {
// we intentionally only add one of each type
query.Del(k)
query.Add(k, v[0])
}
}
if o := input.OptionsObject.ToOData(); o != nil {
req.Header = o.AppendHeaders(req.Header)
query = o.AppendValues(query)
}
}
req.URL.RawQuery = query.Encode()
//req.RetryFunc = client.RequestRetryAny(defaultRetryFunctions...)
req.ValidStatusCodes = input.ExpectedStatusCodes
return req, nil
}