-
Notifications
You must be signed in to change notification settings - Fork 11
/
request_information.go
636 lines (600 loc) · 19.4 KB
/
request_information.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package abstractions
import (
"context"
"errors"
"time"
"reflect"
"strconv"
"strings"
u "net/url"
"github.com/google/uuid"
s "github.com/microsoft/kiota-abstractions-go/serialization"
stduritemplate "github.com/std-uritemplate/std-uritemplate/go/v2"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// RequestInformation represents an abstract HTTP request.
type RequestInformation struct {
// The HTTP method of the request.
Method HttpMethod
uri *u.URL
// The Request Headers.
Headers *RequestHeaders
// The Query Parameters of the request.
// Deprecated: use QueryParametersAny instead
QueryParameters map[string]string
// The Query Parameters of the request.
QueryParametersAny map[string]any
// The Request Body.
Content []byte
// The path parameters to use for the URL template when generating the URI.
// Deprecated: use PathParametersAny instead
PathParameters map[string]string
// The path parameters to use for the URL template when generating the URI.
PathParametersAny map[string]any
// The Url template for the current request.
UrlTemplate string
options map[string]RequestOption
}
const raw_url_key = "request-raw-url"
// NewRequestInformation creates a new RequestInformation object with default values.
func NewRequestInformation() *RequestInformation {
return &RequestInformation{
Headers: NewRequestHeaders(),
QueryParameters: make(map[string]string),
QueryParametersAny: make(map[string]any),
options: make(map[string]RequestOption),
PathParameters: make(map[string]string),
PathParametersAny: make(map[string]any),
}
}
// NewRequestInformationWithMethodAndUrlTemplateAndPathParameters creates a new RequestInformation object with the specified method and URL template and path parameters.
func NewRequestInformationWithMethodAndUrlTemplateAndPathParameters(method HttpMethod, urlTemplate string, pathParameters map[string]string) *RequestInformation {
value := NewRequestInformation()
value.Method = method
value.UrlTemplate = urlTemplate
value.PathParameters = pathParameters
return value
}
func ConfigureRequestInformation[T any](request *RequestInformation, config *RequestConfiguration[T]) {
if request == nil {
return
}
if config == nil {
return
}
if config.QueryParameters != nil {
request.AddQueryParameters(*(config.QueryParameters))
}
request.Headers.AddAll(config.Headers)
request.AddRequestOptions(config.Options)
}
// GetUri returns the URI of the request.
func (request *RequestInformation) GetUri() (*u.URL, error) {
if request.uri != nil {
return request.uri, nil
} else if request.UrlTemplate == "" {
return nil, errors.New("uri cannot be empty")
} else if request.PathParameters == nil {
return nil, errors.New("uri template parameters cannot be nil")
} else if request.QueryParameters == nil {
return nil, errors.New("uri query parameters cannot be nil")
} else if request.QueryParametersAny == nil {
return nil, errors.New("uri query parameters cannot be nil")
} else if request.PathParameters[raw_url_key] != "" {
uri, err := u.Parse(request.PathParameters[raw_url_key])
if err != nil {
return nil, err
}
request.SetUri(*uri)
return request.uri, nil
} else {
_, baseurlExists := request.PathParameters["baseurl"]
if !baseurlExists && strings.Contains(strings.ToLower(request.UrlTemplate), "{+baseurl}") {
return nil, errors.New("pathParameters must contain a value for \"baseurl\" for the url to be built")
}
substitutions := make(map[string]any)
for key, value := range request.PathParameters {
substitutions[key] = request.sanitizeValue(value)
}
for key, value := range request.PathParametersAny {
substitutions[key] = request.normalizeParameters(reflect.ValueOf(value), request.sanitizeValue(value), false)
}
for key, value := range request.QueryParameters {
substitutions[key] = request.sanitizeValue(value)
}
for key, value := range request.QueryParametersAny {
substitutions[key] = request.sanitizeValue(value)
}
url, err := stduritemplate.Expand(request.UrlTemplate, substitutions)
if err != nil {
return nil, err
}
uri, err := u.Parse(url)
return uri, err
}
}
func castItem[T any, R interface{}](collection []T, mutator func(t T) R) []R {
if len(collection) > 0 {
cast := make([]R, len(collection))
for i, v := range collection {
cast[i] = mutator(v)
}
return cast
}
return nil
}
func (request *RequestInformation) sanitizeValue(value any) any {
if value == nil {
return nil
}
switch v := value.(type) {
case *time.Time:
return v.Format(time.RFC3339)
case time.Time:
return v.Format(time.RFC3339)
case []*time.Time:
return castItem(v, func(t *time.Time) string {
return t.Format(time.RFC3339)
})
case []time.Time:
return castItem(v, func(t time.Time) string {
return t.Format(time.RFC3339)
})
case *s.ISODuration:
return v.String()
case s.ISODuration:
return v.String()
case []*s.ISODuration:
return castItem(v, func(v *s.ISODuration) string {
return v.String()
})
case []s.ISODuration:
return castItem(v, func(v s.ISODuration) string {
return v.String()
})
case *s.TimeOnly:
return v.String()
case s.TimeOnly:
return v.String()
case []*s.TimeOnly:
return castItem(v, func(v *s.TimeOnly) string {
return v.String()
})
case []s.TimeOnly:
return castItem(v, func(v s.TimeOnly) string {
return v.String()
})
case *s.DateOnly:
return v.String()
case s.DateOnly:
return v.String()
case []*s.DateOnly:
return castItem(v, func(v *s.DateOnly) string {
return v.String()
})
case []s.DateOnly:
return castItem(v, func(v s.DateOnly) string {
return v.String()
})
}
return value
}
// SetUri updates the URI for the request from a raw URL.
func (request *RequestInformation) SetUri(url u.URL) {
request.uri = &url
for k := range request.PathParameters {
delete(request.PathParameters, k)
}
for k := range request.QueryParameters {
delete(request.QueryParameters, k)
}
for k := range request.QueryParametersAny {
delete(request.QueryParametersAny, k)
}
}
// AddRequestOptions adds an option to the request to be read by the middleware infrastructure.
func (request *RequestInformation) AddRequestOptions(options []RequestOption) {
if options == nil {
return
}
if request.options == nil {
request.options = make(map[string]RequestOption, len(options))
}
for _, option := range options {
request.options[option.GetKey().Key] = option
}
}
// GetRequestOptions returns the options for this request. Options are unique by type. If an option of the same type is added twice, the last one wins.
func (request *RequestInformation) GetRequestOptions() []RequestOption {
if request.options == nil {
return []RequestOption{}
}
result := make([]RequestOption, len(request.options))
idx := 0
for _, option := range request.options {
result[idx] = option
idx++
}
return result
}
const contentTypeHeader = "Content-Type"
const binaryContentType = "application/octet-stream"
// SetStreamContent sets the request body to a binary stream.
// Deprecated: Use SetStreamContentAndContentType instead.
func (request *RequestInformation) SetStreamContent(content []byte) {
request.SetStreamContentAndContentType(content, binaryContentType)
}
// SetStreamContentAndContentType sets the request body to a binary stream with the specified content type.
func (request *RequestInformation) SetStreamContentAndContentType(content []byte, contentType string) {
request.Content = content
if request.Headers != nil {
request.Headers.Add(contentTypeHeader, contentType)
}
}
func (request *RequestInformation) setContentAndContentType(writer s.SerializationWriter, contentType string) error {
content, err := writer.GetSerializedContent()
if err != nil {
return err
} else if content == nil {
return errors.New("content cannot be nil")
}
request.Content = content
if request.Headers != nil {
request.Headers.TryAdd(contentTypeHeader, contentType)
}
return nil
}
func (request *RequestInformation) getSerializationWriter(requestAdapter RequestAdapter, contentType string, items ...interface{}) (s.SerializationWriter, error) {
if contentType == "" {
return nil, errors.New("content type cannot be empty")
} else if requestAdapter == nil {
return nil, errors.New("requestAdapter cannot be nil")
} else if len(items) == 0 {
return nil, errors.New("items cannot be nil or empty")
}
factory := requestAdapter.GetSerializationWriterFactory()
if factory == nil {
return nil, errors.New("factory cannot be nil")
}
writer, err := factory.GetSerializationWriter(contentType)
if err != nil {
return nil, err
} else if writer == nil {
return nil, errors.New("writer cannot be nil")
} else {
return writer, nil
}
}
func (r *RequestInformation) setRequestType(result interface{}, span trace.Span) {
if result != nil {
span.SetAttributes(attribute.String("com.microsoft.kiota.request.type", reflect.TypeOf(result).String()))
}
}
const observabilityTracerName = "github.com/microsoft/kiota-abstractions-go"
// SetContentFromParsable sets the request body from a model with the specified content type.
func (request *RequestInformation) SetContentFromParsable(ctx context.Context, requestAdapter RequestAdapter, contentType string, item s.Parsable) error {
_, span := otel.GetTracerProvider().Tracer(observabilityTracerName).Start(ctx, "SetContentFromParsable")
defer span.End()
writer, err := request.getSerializationWriter(requestAdapter, contentType, item)
if err != nil {
span.RecordError(err)
return err
}
defer writer.Close()
if multipartBody, ok := item.(MultipartBody); ok {
contentType += "; boundary=" + multipartBody.GetBoundary()
multipartBody.SetRequestAdapter(requestAdapter)
}
request.setRequestType(item, span)
err = writer.WriteObjectValue("", item)
if err != nil {
span.RecordError(err)
return err
}
err = request.setContentAndContentType(writer, contentType)
if err != nil {
span.RecordError(err)
return err
}
return nil
}
// SetContentFromParsableCollection sets the request body from a model with the specified content type.
func (request *RequestInformation) SetContentFromParsableCollection(ctx context.Context, requestAdapter RequestAdapter, contentType string, items []s.Parsable) error {
_, span := otel.GetTracerProvider().Tracer(observabilityTracerName).Start(ctx, "SetContentFromParsableCollection")
defer span.End()
writer, err := request.getSerializationWriter(requestAdapter, contentType, items)
if err != nil {
span.RecordError(err)
return err
}
defer writer.Close()
if len(items) > 0 {
request.setRequestType(items[0], span)
}
err = writer.WriteCollectionOfObjectValues("", items)
if err != nil {
span.RecordError(err)
return err
}
err = request.setContentAndContentType(writer, contentType)
if err != nil {
span.RecordError(err)
return err
}
return nil
}
// SetContentFromScalar sets the request body from a scalar value with the specified content type.
func (request *RequestInformation) SetContentFromScalar(ctx context.Context, requestAdapter RequestAdapter, contentType string, item interface{}) error {
_, span := otel.GetTracerProvider().Tracer(observabilityTracerName).Start(ctx, "SetContentFromScalar")
defer span.End()
writer, err := request.getSerializationWriter(requestAdapter, contentType, item)
if err != nil {
span.RecordError(err)
return err
}
defer writer.Close()
request.setRequestType(item, span)
if sv, ok := item.(*string); ok {
err = writer.WriteStringValue("", sv)
} else if bv, ok := item.(*bool); ok {
err = writer.WriteBoolValue("", bv)
} else if byv, ok := item.(*byte); ok {
err = writer.WriteByteValue("", byv)
} else if i8v, ok := item.(*int8); ok {
err = writer.WriteInt8Value("", i8v)
} else if i32v, ok := item.(*int32); ok {
err = writer.WriteInt32Value("", i32v)
} else if i64v, ok := item.(*int64); ok {
err = writer.WriteInt64Value("", i64v)
} else if f32v, ok := item.(*float32); ok {
err = writer.WriteFloat32Value("", f32v)
} else if f64v, ok := item.(*float64); ok {
err = writer.WriteFloat64Value("", f64v)
} else if uv, ok := item.(*uuid.UUID); ok {
err = writer.WriteUUIDValue("", uv)
} else if tv, ok := item.(*time.Time); ok {
err = writer.WriteTimeValue("", tv)
} else if dv, ok := item.(*s.ISODuration); ok {
err = writer.WriteISODurationValue("", dv)
} else if tov, ok := item.(*s.TimeOnly); ok {
err = writer.WriteTimeOnlyValue("", tov)
} else if dov, ok := item.(*s.DateOnly); ok {
err = writer.WriteDateOnlyValue("", dov)
}
if err != nil {
span.RecordError(err)
return err
}
err = request.setContentAndContentType(writer, contentType)
if err != nil {
span.RecordError(err)
return err
}
return nil
}
// SetContentFromScalarCollection sets the request body from a scalar value with the specified content type.
func (request *RequestInformation) SetContentFromScalarCollection(ctx context.Context, requestAdapter RequestAdapter, contentType string, items []interface{}) error {
_, span := otel.GetTracerProvider().Tracer(observabilityTracerName).Start(ctx, "SetContentFromScalarCollection")
defer span.End()
writer, err := request.getSerializationWriter(requestAdapter, contentType, items...)
if err != nil {
span.RecordError(err)
return err
}
defer writer.Close()
if len(items) > 0 {
value := items[0]
request.setRequestType(value, span)
if _, ok := value.(string); ok {
sc := make([]string, len(items))
for i, v := range items {
if sv, ok := v.(string); ok {
sc[i] = sv
}
}
err = writer.WriteCollectionOfStringValues("", sc)
} else if _, ok := value.(bool); ok {
bc := make([]bool, len(items))
for i, v := range items {
if sv, ok := v.(bool); ok {
bc[i] = sv
}
}
err = writer.WriteCollectionOfBoolValues("", bc)
} else if _, ok := value.(byte); ok {
byc := make([]byte, len(items))
for i, v := range items {
if sv, ok := v.(byte); ok {
byc[i] = sv
}
}
err = writer.WriteCollectionOfByteValues("", byc)
} else if _, ok := value.(int8); ok {
i8c := make([]int8, len(items))
for i, v := range items {
if sv, ok := v.(int8); ok {
i8c[i] = sv
}
}
err = writer.WriteCollectionOfInt8Values("", i8c)
} else if _, ok := value.(int32); ok {
i32c := make([]int32, len(items))
for i, v := range items {
if sv, ok := v.(int32); ok {
i32c[i] = sv
}
}
err = writer.WriteCollectionOfInt32Values("", i32c)
} else if _, ok := value.(int64); ok {
i64c := make([]int64, len(items))
for i, v := range items {
if sv, ok := v.(int64); ok {
i64c[i] = sv
}
}
err = writer.WriteCollectionOfInt64Values("", i64c)
} else if _, ok := value.(float32); ok {
f32c := make([]float32, len(items))
for i, v := range items {
if sv, ok := v.(float32); ok {
f32c[i] = sv
}
}
err = writer.WriteCollectionOfFloat32Values("", f32c)
} else if _, ok := value.(float64); ok {
f64c := make([]float64, len(items))
for i, v := range items {
if sv, ok := v.(float64); ok {
f64c[i] = sv
}
}
err = writer.WriteCollectionOfFloat64Values("", f64c)
} else if _, ok := value.(uuid.UUID); ok {
uc := make([]uuid.UUID, len(items))
for i, v := range items {
if sv, ok := v.(uuid.UUID); ok {
uc[i] = sv
}
}
err = writer.WriteCollectionOfUUIDValues("", uc)
} else if _, ok := value.(time.Time); ok {
tc := make([]time.Time, len(items))
for i, v := range items {
if sv, ok := v.(time.Time); ok {
tc[i] = sv
}
}
err = writer.WriteCollectionOfTimeValues("", tc)
} else if _, ok := value.(s.ISODuration); ok {
dc := make([]s.ISODuration, len(items))
for i, v := range items {
if sv, ok := v.(s.ISODuration); ok {
dc[i] = sv
}
}
err = writer.WriteCollectionOfISODurationValues("", dc)
} else if _, ok := value.(s.TimeOnly); ok {
toc := make([]s.TimeOnly, len(items))
for i, v := range items {
if sv, ok := v.(s.TimeOnly); ok {
toc[i] = sv
}
}
err = writer.WriteCollectionOfTimeOnlyValues("", toc)
} else if _, ok := value.(s.DateOnly); ok {
doc := make([]s.DateOnly, len(items))
for i, v := range items {
if sv, ok := v.(s.DateOnly); ok {
doc[i] = sv
}
}
err = writer.WriteCollectionOfDateOnlyValues("", doc)
} else if _, ok := value.(byte); ok {
ba := make([]byte, len(items))
for i, v := range items {
if sv, ok := v.(byte); ok {
ba[i] = sv
}
}
err = writer.WriteByteArrayValue("", ba)
}
}
if err != nil {
span.RecordError(err)
return err
}
err = request.setContentAndContentType(writer, contentType)
if err != nil {
span.RecordError(err)
return err
}
return nil
}
// AddQueryParameters adds the query parameters to the request by reading the properties from the provided object.
func (request *RequestInformation) AddQueryParameters(source any) {
if source == nil || request == nil {
return
}
valOfP := reflect.ValueOf(source)
fields := reflect.TypeOf(source)
numOfFields := fields.NumField()
for i := 0; i < numOfFields; i++ {
field := fields.Field(i)
fieldName := field.Name
fieldValue := valOfP.Field(i)
tagValue := field.Tag.Get("uriparametername")
if tagValue != "" {
fieldName = tagValue
}
value := request.sanitizeValue(fieldValue.Interface())
valueOfValue := reflect.ValueOf(value)
if valueOfValue.IsNil() {
continue
}
str, ok := value.(*string)
if ok && str != nil {
request.QueryParameters[fieldName] = *str
}
bl, ok := value.(*bool)
if ok && bl != nil {
request.QueryParameters[fieldName] = strconv.FormatBool(*bl)
}
it, ok := value.(*int32)
if ok && it != nil {
request.QueryParameters[fieldName] = strconv.FormatInt(int64(*it), 10)
}
strArr, ok := value.([]string)
if ok && len(strArr) > 0 {
// populating both query parameter fields to avoid breaking compatibility with code reading this field
request.QueryParameters[fieldName] = strings.Join(strArr, ",")
tmp := make([]any, len(strArr))
for i, v := range strArr {
tmp[i] = v
}
request.QueryParametersAny[fieldName] = tmp
}
if arr, ok := value.([]any); ok && len(arr) > 0 {
request.QueryParametersAny[fieldName] = arr
}
normalizedValue := request.normalizeParameters(valueOfValue, value, true)
if normalizedValue != nil {
request.QueryParametersAny[fieldName] = normalizedValue
}
}
}
// Normalize different types to values that can be rendered in an URL:
// enum -> string (name)
// []enum -> []string (containing names)
// []non_interface -> []any (like []int64 -> []any)
func (request *RequestInformation) normalizeParameters(valueOfValue reflect.Value, value any, returnNilIfNotNormalizable bool) any {
if valueOfValue.Kind() == reflect.Slice && valueOfValue.Len() > 0 {
//type assertions to "enums" don't work if you don't know the enum type in advance, we need to use reflection
enumArr := valueOfValue.Slice(0, valueOfValue.Len())
if _, ok := enumArr.Index(0).Interface().(kiotaEnum); ok {
// testing the first value is an enum to avoid iterating over the whole array if it's not
strRepresentations := make([]string, valueOfValue.Len())
for i := range strRepresentations {
strRepresentations[i] = enumArr.Index(i).Interface().(kiotaEnum).String()
}
return strRepresentations
} else {
anySlice := make([]any, valueOfValue.Len())
for i := range anySlice {
anySlice[i] = enumArr.Index(i).Interface()
}
return anySlice
}
} else if enum, ok := value.(kiotaEnum); ok {
return enum.String()
}
if returnNilIfNotNormalizable {
return nil
} else {
return value
}
}
type kiotaEnum interface {
String() string
}