-
Notifications
You must be signed in to change notification settings - Fork 27
/
client.go
195 lines (166 loc) · 5.35 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
package api
import (
"context"
"io"
"net/http"
"github.com/alcionai/clues"
"github.com/alcionai/corso/src/pkg/account"
"github.com/alcionai/corso/src/pkg/control"
"github.com/alcionai/corso/src/pkg/count"
"github.com/alcionai/corso/src/pkg/path"
"github.com/alcionai/corso/src/pkg/services/m365/api/graph"
)
// ---------------------------------------------------------------------------
// interfaces
// ---------------------------------------------------------------------------
// Client is used to fulfill queries that are traditionally
// backed by GraphAPI. A struct is used in this case, instead
// of deferring to pure function wrappers, so that the boundary
// separates the granular implementation of the graphAPI and
// kiota away from the other packages.
type Client struct {
Credentials account.M365Config
// The Stable service is re-usable for any request.
// This allows us to maintain performance across async requests.
Stable graph.Servicer
// The LargeItem graph servicer is configured specifically for
// downloading large items such as drive item content or outlook
// mail and event attachments.
LargeItem graph.Servicer
// The Requester provides a client specifically for calling
// arbitrary urls instead of constructing queries using the
// graph api client.
Requester graph.Requester
counter *count.Bus
options control.Options
}
// NewClient produces a new exchange api client. Must be used in
// place of creating an ad-hoc client struct.
func NewClient(
creds account.M365Config,
co control.Options,
counter *count.Bus,
opts ...graph.Option,
) (Client, error) {
s, err := NewService(creds, counter, opts...)
if err != nil {
return Client{}, err
}
li, err := newLargeItemService(creds, counter)
if err != nil {
return Client{}, err
}
azureAuth, err := graph.NewAzureAuth(creds)
if err != nil {
return Client{}, clues.Wrap(err, "generating azure authorizer")
}
rqr := graph.NewNoTimeoutHTTPWrapper(
counter,
graph.AuthorizeRequester(azureAuth))
if co.DeltaPageSize < 1 || co.DeltaPageSize > maxDeltaPageSize {
co.DeltaPageSize = maxDeltaPageSize
}
cli := Client{
Credentials: creds,
Stable: s,
LargeItem: li,
Requester: rqr,
counter: counter,
options: co,
}
return cli, nil
}
// initConcurrencyLimit ensures that the graph concurrency limiter is
// initialized, so that calls do not step over graph api's service limits.
// Limits are derived from the provided servie type.
// Callers will need to call this func before making api calls an api client.
func InitConcurrencyLimit(ctx context.Context, pst path.ServiceType) {
graph.InitializeConcurrencyLimiter(ctx, pst == path.ExchangeService, 4)
}
// Service generates a new graph servicer. New servicers are used for paged
// and other long-running requests instead of the client's stable service,
// so that in-flight state within the adapter doesn't get clobbered.
// Most calls should use the Client.Stable property instead of calling this
// func, unless it is explicitly necessary.
func (c Client) Service(
counter *count.Bus,
opts ...graph.Option,
) (graph.Servicer, error) {
return NewService(c.Credentials, counter, opts...)
}
func NewService(
creds account.M365Config,
counter *count.Bus,
opts ...graph.Option,
) (*graph.Service, error) {
a, err := graph.CreateAdapter(
creds.AzureTenantID,
creds.AzureClientID,
creds.AzureClientSecret,
counter,
opts...)
if err != nil {
return nil, clues.Wrap(err, "generating graph api adapter")
}
return graph.NewService(a), nil
}
func newLargeItemService(
creds account.M365Config,
counter *count.Bus,
) (*graph.Service, error) {
a, err := NewService(creds, counter, graph.NoTimeout())
return a, clues.Wrap(err, "generating no-timeout graph adapter").OrNil()
}
type Getter interface {
Get(
ctx context.Context,
url string,
headers map[string]string,
requireAuth bool,
) (*http.Response, error)
}
// Get performs an ad-hoc get request using its graph.Requester
func (c Client) Get(
ctx context.Context,
url string,
headers map[string]string,
requireAuth bool,
) (*http.Response, error) {
return c.Requester.Request(ctx, http.MethodGet, url, nil, headers, requireAuth)
}
// Get performs an ad-hoc get request using its graph.Requester
func (c Client) Post(
ctx context.Context,
url string,
headers map[string]string,
body io.Reader,
requireAuth bool,
) (*http.Response, error) {
return c.Requester.Request(ctx, http.MethodGet, url, body, headers, requireAuth)
}
// ---------------------------------------------------------------------------
// per-call config
// ---------------------------------------------------------------------------
// CallConfig defines additional parameters to add to the query like field
// selection and limiting results.
//
// Not all calls may support all options. Check individual implementations to
// see what's supported and what's not.
type CallConfig struct {
Expand []string
Select []string
CanMakeDeltaQueries bool
// LimitResults limits the returned results to the given number. If 0, returns
// all results.
LimitResults int
}
// ---------------------------------------------------------------------------
// common interfaces
// ---------------------------------------------------------------------------
type GetByIDer[T any] interface {
GetByID(
ctx context.Context,
identifier string,
cc CallConfig,
) (T, error)
}