Repository navigation
Expand file tree
/
Copy pathdefault_request_adapter.go
More file actions
244 lines (216 loc) · 12.4 KB
/
Copy pathdefault_request_adapter.go
File metadata and controls
244 lines (216 loc) · 12.4 KB
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
package bundle
import (
"context"
"errors"
nethttp "net/http"
"sync"
abs "github.com/microsoft/kiota-abstractions-go"
absauth "github.com/microsoft/kiota-abstractions-go/authentication"
absser "github.com/microsoft/kiota-abstractions-go/serialization"
"github.com/microsoft/kiota-abstractions-go/store"
khttp "github.com/microsoft/kiota-http-go"
serform "github.com/microsoft/kiota-serialization-form-go"
serjson "github.com/microsoft/kiota-serialization-json-go"
sermultipart "github.com/microsoft/kiota-serialization-multipart-go"
sertext "github.com/microsoft/kiota-serialization-text-go"
)
// DefaultRequestAdapter is the core service used by GraphServiceClient to make requests to Microsoft Graph.
type DefaultRequestAdapter struct {
khttp.NetHttpRequestAdapter
}
// NewDefaultRequestAdapter creates a new DefaultRequestAdapter with the given parameters
func NewDefaultRequestAdapter(authenticationProvider absauth.AuthenticationProvider) (*DefaultRequestAdapter, error) {
return NewDefaultRequestAdapterWithParseNodeFactory(authenticationProvider, nil)
}
// NewDefaultRequestAdapterWithParseNodeFactory creates a new DefaultRequestAdapter with the given parameters
func NewDefaultRequestAdapterWithParseNodeFactory(authenticationProvider absauth.AuthenticationProvider, parseNodeFactory absser.ParseNodeFactory) (*DefaultRequestAdapter, error) {
return NewDefaultRequestAdapterWithParseNodeFactoryAndSerializationWriterFactory(authenticationProvider, parseNodeFactory, nil)
}
// NewDefaultRequestAdapterWithParseNodeFactoryAndSerializationWriterFactory creates a new DefaultRequestAdapter with the given parameters
func NewDefaultRequestAdapterWithParseNodeFactoryAndSerializationWriterFactory(authenticationProvider absauth.AuthenticationProvider, parseNodeFactory absser.ParseNodeFactory, serializationWriterFactory absser.SerializationWriterFactory) (*DefaultRequestAdapter, error) {
return NewDefaultRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient(authenticationProvider, parseNodeFactory, serializationWriterFactory, nil)
}
// NewDefaultRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient creates a new DefaultRequestAdapter with the given parameters
func NewDefaultRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient(authenticationProvider absauth.AuthenticationProvider, parseNodeFactory absser.ParseNodeFactory, serializationWriterFactory absser.SerializationWriterFactory, httpClient *nethttp.Client) (*DefaultRequestAdapter, error) {
return NewDefaultRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClientAndObservabilityOptions(authenticationProvider, parseNodeFactory, serializationWriterFactory, httpClient, khttp.ObservabilityOptions{})
}
// NewDefaultRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClientAndObservabilityOptions creates a new DefaultRequestAdapter with the given parameters including observability options
func NewDefaultRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClientAndObservabilityOptions(authenticationProvider absauth.AuthenticationProvider, parseNodeFactory absser.ParseNodeFactory, serializationWriterFactory absser.SerializationWriterFactory, httpClient *nethttp.Client, observabilityOptions khttp.ObservabilityOptions) (*DefaultRequestAdapter, error) {
if authenticationProvider == nil {
return nil, errors.New("authenticationProvider cannot be nil")
}
SetupDefaults()
if httpClient == nil {
httpClient = khttp.GetDefaultClient()
}
if serializationWriterFactory == nil {
serializationWriterFactory = absser.DefaultSerializationWriterFactoryInstance
}
if parseNodeFactory == nil {
parseNodeFactory = absser.DefaultParseNodeFactoryInstance
}
defaultAdapter, err := khttp.NewNetHttpRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClientAndObservabilityOptions(authenticationProvider, parseNodeFactory, serializationWriterFactory, httpClient, observabilityOptions)
if err != nil {
return nil, err
}
if defaultAdapter == nil {
return nil, errors.New("failed to initialize request adapter")
}
result := &DefaultRequestAdapter{
NetHttpRequestAdapter: *defaultAdapter,
}
return result, nil
}
// GetBaseUrl returns the base URL for the request adapter or empty string if the receiver is nil.
func (a *DefaultRequestAdapter) GetBaseUrl() string {
if a == nil {
return ""
}
return a.NetHttpRequestAdapter.GetBaseUrl()
}
// SetBaseUrl sets the base URL for the request adapter or performs a no-op if the receiver is nil.
func (a *DefaultRequestAdapter) SetBaseUrl(baseUrl string) {
if a == nil {
return
}
a.NetHttpRequestAdapter.SetBaseUrl(baseUrl)
}
// GetSerializationWriterFactory returns the serialization writer factory or nil if the receiver is nil.
func (a *DefaultRequestAdapter) GetSerializationWriterFactory() absser.SerializationWriterFactory {
if a == nil {
return nil
}
return a.NetHttpRequestAdapter.GetSerializationWriterFactory()
}
// EnableBackingStore enables the backing store proxies for the SerializationWriters and ParseNodes in use or performs a no-op if the receiver is nil.
func (a *DefaultRequestAdapter) EnableBackingStore(factory store.BackingStoreFactory) {
if a == nil {
return
}
a.NetHttpRequestAdapter.EnableBackingStore(factory)
}
// ConvertToNativeRequest converts the given RequestInformation into a native HTTP request or returns an error if the receiver is nil.
func (a *DefaultRequestAdapter) ConvertToNativeRequest(ctx context.Context, requestInfo *abs.RequestInformation) (any, error) {
if a == nil {
return nil, errors.New("request adapter cannot be nil")
}
return a.NetHttpRequestAdapter.ConvertToNativeRequest(ctx, requestInfo)
}
// Send executes the HTTP request specified by the given RequestInformation and returns the deserialized response model or an error if the receiver is nil.
func (a *DefaultRequestAdapter) Send(ctx context.Context, requestInfo *abs.RequestInformation, constructor absser.ParsableFactory, errorMappings abs.ErrorMappings) (absser.Parsable, error) {
if a == nil {
return nil, errors.New("request adapter cannot be nil")
}
return a.NetHttpRequestAdapter.Send(ctx, requestInfo, constructor, errorMappings)
}
// SendEnum executes the HTTP request specified by the given RequestInformation and returns the deserialized response model or an error if the receiver is nil.
func (a *DefaultRequestAdapter) SendEnum(ctx context.Context, requestInfo *abs.RequestInformation, parser absser.EnumFactory, errorMappings abs.ErrorMappings) (any, error) {
if a == nil {
return nil, errors.New("request adapter cannot be nil")
}
return a.NetHttpRequestAdapter.SendEnum(ctx, requestInfo, parser, errorMappings)
}
// SendCollection executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection or an error if the receiver is nil.
func (a *DefaultRequestAdapter) SendCollection(ctx context.Context, requestInfo *abs.RequestInformation, constructor absser.ParsableFactory, errorMappings abs.ErrorMappings) ([]absser.Parsable, error) {
if a == nil {
return nil, errors.New("request adapter cannot be nil")
}
return a.NetHttpRequestAdapter.SendCollection(ctx, requestInfo, constructor, errorMappings)
}
// SendEnumCollection executes the HTTP request specified by the given RequestInformation and returns the deserialized response model collection or an error if the receiver is nil.
func (a *DefaultRequestAdapter) SendEnumCollection(ctx context.Context, requestInfo *abs.RequestInformation, parser absser.EnumFactory, errorMappings abs.ErrorMappings) ([]any, error) {
if a == nil {
return nil, errors.New("request adapter cannot be nil")
}
return a.NetHttpRequestAdapter.SendEnumCollection(ctx, requestInfo, parser, errorMappings)
}
// SendPrimitive executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model or an error if the receiver is nil.
func (a *DefaultRequestAdapter) SendPrimitive(ctx context.Context, requestInfo *abs.RequestInformation, typeName string, errorMappings abs.ErrorMappings) (any, error) {
if a == nil {
return nil, errors.New("request adapter cannot be nil")
}
return a.NetHttpRequestAdapter.SendPrimitive(ctx, requestInfo, typeName, errorMappings)
}
// SendPrimitiveCollection executes the HTTP request specified by the given RequestInformation and returns the deserialized primitive response model collection or an error if the receiver is nil.
func (a *DefaultRequestAdapter) SendPrimitiveCollection(ctx context.Context, requestInfo *abs.RequestInformation, typeName string, errorMappings abs.ErrorMappings) ([]any, error) {
if a == nil {
return nil, errors.New("request adapter cannot be nil")
}
return a.NetHttpRequestAdapter.SendPrimitiveCollection(ctx, requestInfo, typeName, errorMappings)
}
// SendNoContent executes the HTTP request specified by the given RequestInformation with no return content or returns an error if the receiver is nil.
func (a *DefaultRequestAdapter) SendNoContent(ctx context.Context, requestInfo *abs.RequestInformation, errorMappings abs.ErrorMappings) error {
if a == nil {
return errors.New("request adapter cannot be nil")
}
return a.NetHttpRequestAdapter.SendNoContent(ctx, requestInfo, errorMappings)
}
var (
setupDefaultsOnce sync.Once
setupDefaultsLock sync.Mutex
)
// ResetDefaults resets the default registration state, clears default serializer/deserializer registries,
// and restores BackingStoreFactoryInstance to NewInMemoryBackingStore.
// It synchronizes reset and registration cycles against SetupDefaults via a package-level mutex.
//
// Note: Because the underlying registry lookups in kiota-abstractions-go (GetSerializationWriter and GetRootParseNode)
// read from their factory maps without locking, callers must ensure that all request adapters and registry consumers
// remain idle throughout reset and reinitialization. ResetDefaults is intended for test suites and initialization
// lifecycles between test runs or configuration phases, and must not be called concurrently with active request
// serialization or response parsing.
func ResetDefaults() {
setupDefaultsLock.Lock()
defer setupDefaultsLock.Unlock()
setupDefaultsOnce = sync.Once{}
store.BackingStoreFactoryInstance = store.NewInMemoryBackingStore
absser.DefaultSerializationWriterFactoryInstance.Lock()
if absser.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories != nil {
for k := range absser.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories {
delete(absser.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories, k)
}
} else {
absser.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]absser.SerializationWriterFactory)
}
absser.DefaultSerializationWriterFactoryInstance.Unlock()
absser.DefaultParseNodeFactoryInstance.Lock()
if absser.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories != nil {
for k := range absser.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories {
delete(absser.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories, k)
}
} else {
absser.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories = make(map[string]absser.ParseNodeFactory)
}
absser.DefaultParseNodeFactoryInstance.Unlock()
}
// SetupDefaults registers the default serializers and deserializers in the global registries.
// It is thread-safe and executes only once.
func SetupDefaults() {
setupDefaultsLock.Lock()
defer setupDefaultsLock.Unlock()
setupDefaults()
}
func setupDefaults() {
setupDefaultsOnce.Do(func() {
abs.RegisterDefaultSerializer(func() absser.SerializationWriterFactory {
return serjson.NewJsonSerializationWriterFactory()
})
abs.RegisterDefaultSerializer(func() absser.SerializationWriterFactory {
return sertext.NewTextSerializationWriterFactory()
})
abs.RegisterDefaultSerializer(func() absser.SerializationWriterFactory {
return serform.NewFormSerializationWriterFactory()
})
abs.RegisterDefaultSerializer(func() absser.SerializationWriterFactory {
return sermultipart.NewMultipartSerializationWriterFactory()
})
abs.RegisterDefaultDeserializer(func() absser.ParseNodeFactory {
return serjson.NewJsonParseNodeFactory()
})
abs.RegisterDefaultDeserializer(func() absser.ParseNodeFactory {
return sertext.NewTextParseNodeFactory()
})
abs.RegisterDefaultDeserializer(func() absser.ParseNodeFactory {
return serform.NewFormParseNodeFactory()
})
})
}