/
client.go
339 lines (293 loc) · 7.22 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
package client
import (
"context"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"golang.org/x/net/publicsuffix"
)
type Query url.Values
func (q Query) Clone() Query {
m := url.Values{}
for k, v := range q {
m[k] = v
}
return Query(m)
}
func (q Query) TakeOrSelf(ctx context.Context, v types.Map) Query {
if len(v.Elements()) == 0 {
return q
}
nq := Query{}
for k, v := range v.Elements() {
vs := []string{}
diags := v.(types.List).ElementsAs(ctx, &vs, false)
if diags.HasError() {
panic(diags)
}
nq[k] = vs
}
return nq
}
func (q Query) ToTFValue() types.Map {
var result types.Map
tfsdk.ValueFrom(context.Background(), q, types.MapType{ElemType: types.ListType{ElemType: types.StringType}}, &result)
return result
}
type Header map[string]string
func (h Header) Clone() Header {
nh := Header{}
for k, v := range h {
nh[k] = v
}
return nh
}
func (h Header) TakeOrSelf(ctx context.Context, v types.Map) Header {
if len(v.Elements()) == 0 {
return h
}
nh := Header{}
for k, v := range v.Elements() {
nh[k] = v.(types.String).ValueString()
}
return nh
}
func (h Header) ToTFValue() types.Map {
var result types.Map
tfsdk.ValueFrom(context.Background(), h, types.MapType{ElemType: types.StringType}, &result)
return result
}
type Client struct {
*resty.Client
}
func New(ctx context.Context, baseURL string, opt *BuildOption) (*Client, error) {
if opt == nil {
opt = &BuildOption{}
}
transport := http.DefaultTransport.(*http.Transport)
transport.TLSClientConfig = &opt.TLSConfig
httpClient := &http.Client{
Transport: transport,
}
if opt.CookieEnabled {
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
httpClient.Jar = cookieJar
}
client := resty.New()
if opt.Security != nil {
var err error
client, err = opt.Security.newClient(ctx, httpClient)
if err != nil {
return nil, err
}
}
if _, err := url.Parse(baseURL); err != nil {
return nil, err
}
client.SetBaseURL(baseURL)
return &Client{client}, nil
}
type RetryOption struct {
StatusLocator ValueLocator
Status PollingStatus
Count int
WaitTime time.Duration
MaxWaitTime time.Duration
}
func (c *Client) resetRetry() {
c.RetryCount = 0
c.RetryWaitTime = 0
c.RetryMaxWaitTime = 0
c.RetryAfter = nil
c.RetryConditions = nil
}
func (c *Client) setRetry(opt RetryOption) {
c.RetryCount = opt.Count
c.RetryWaitTime = opt.WaitTime
c.RetryMaxWaitTime = opt.MaxWaitTime
c.RetryAfter = func(c *resty.Client, r *resty.Response) (time.Duration, error) {
dur := r.Header().Get("Retry-After")
if dur == "" {
// returning 0 will make the resty retry to using the backoff based on wait time, count and max wait time
return 0, nil
}
d, err := time.ParseDuration(dur + "s")
if err != nil {
return 0, fmt.Errorf("invalid Retry-After value in the initiated response: %s", dur)
}
return d, nil
}
c.RetryConditions = []resty.RetryConditionFunc{
func(r *resty.Response, err error) bool {
if err != nil {
return true
}
status := opt.StatusLocator.LocateValueInResp(*r)
if status == "" {
return false
}
// We tolerate case difference here to be pragmatic.
if strings.EqualFold(status, opt.Status.Success) {
return false
}
for _, ps := range opt.Status.Pending {
if strings.EqualFold(status, ps) {
return true
}
}
return false
},
}
}
type CreateOption struct {
Method string
Query Query
Header Header
Retry *RetryOption
}
func (c *Client) Create(ctx context.Context, path string, body interface{}, opt CreateOption) (*resty.Response, error) {
if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
}
req := c.R().SetContext(ctx).SetBody(body)
req.SetQueryParamsFromValues(url.Values(opt.Query))
req.SetHeaders(opt.Header)
req = req.SetHeader("Content-Type", "application/json")
switch opt.Method {
case "POST":
return req.Post(path)
case "PUT":
return req.Put(path)
default:
return nil, fmt.Errorf("unknown create method: %s", opt.Method)
}
}
type ReadOption struct {
Query Query
Header Header
Retry *RetryOption
}
func (c *Client) Read(ctx context.Context, path string, opt ReadOption) (*resty.Response, error) {
if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
}
req := c.R().SetContext(ctx)
req.SetQueryParamsFromValues(url.Values(opt.Query))
req.SetHeaders(opt.Header)
return req.Get(path)
}
type UpdateOption struct {
Method string
MergePatchDisabled bool
Query Query
Header Header
Retry *RetryOption
}
func (c *Client) Update(ctx context.Context, path string, body interface{}, opt UpdateOption) (*resty.Response, error) {
if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
}
req := c.R().SetContext(ctx).SetBody(body)
req.SetQueryParamsFromValues(url.Values(opt.Query))
req.SetHeaders(opt.Header)
req = req.SetHeader("Content-Type", "application/json")
switch opt.Method {
case "PATCH":
return req.Patch(path)
case "PUT":
return req.Put(path)
case "POST":
return req.Post(path)
default:
return nil, fmt.Errorf("unknown update method: %s", opt.Method)
}
}
type DeleteOption struct {
Method string
Query Query
Header Header
Retry *RetryOption
}
func (c *Client) Delete(ctx context.Context, path string, opt DeleteOption) (*resty.Response, error) {
if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
}
req := c.R().SetContext(ctx)
req.SetQueryParamsFromValues(url.Values(opt.Query))
req.SetHeaders(opt.Header)
switch opt.Method {
case "DELETE":
return req.Delete(path)
case "POST":
return req.Post(path)
default:
return nil, fmt.Errorf("unknown delete method: %s", opt.Method)
}
}
type OperationOption struct {
Method string
Query Query
Header Header
Retry *RetryOption
}
func (c *Client) Operation(ctx context.Context, path string, body interface{}, opt OperationOption) (*resty.Response, error) {
if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
}
req := c.R().SetContext(ctx)
if body != "" {
req.SetBody(body)
}
req.SetQueryParamsFromValues(url.Values(opt.Query))
req.SetHeaders(opt.Header)
req = req.SetHeader("Content-Type", "application/json")
switch opt.Method {
case "POST":
return req.Post(path)
case "PUT":
return req.Put(path)
case "PATCH":
return req.Patch(path)
case "DELETE":
return req.Delete(path)
default:
return nil, fmt.Errorf("unknown create method: %s", opt.Method)
}
}
type ReadOptionDS struct {
// Method used for reading, which defaults to GET
Method string
Query Query
Header Header
Retry *RetryOption
}
func (c *Client) ReadDS(ctx context.Context, path string, opt ReadOptionDS) (*resty.Response, error) {
if opt.Retry != nil {
c.setRetry(*opt.Retry)
defer c.resetRetry()
}
req := c.R().SetContext(ctx)
req.SetQueryParamsFromValues(url.Values(opt.Query))
req.SetHeaders(opt.Header)
switch opt.Method {
case "", "GET":
return req.Get(path)
case "POST":
return req.Post(path)
case "HEAD":
return req.Head(path)
default:
return nil, fmt.Errorf("unknown read (ds) method: %s", opt.Method)
}
}