-
Notifications
You must be signed in to change notification settings - Fork 14
/
kiota_client_factory.go
153 lines (132 loc) · 5.2 KB
/
kiota_client_factory.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
// Package nethttplibrary implements the Kiota abstractions with net/http to execute the requests.
// It also provides a middleware infrastructure with some default middleware handlers like the retry handler and the redirect handler.
package nethttplibrary
import (
"errors"
abs "github.com/microsoft/kiota-abstractions-go"
nethttp "net/http"
"net/url"
"time"
)
// GetClientWithProxySettings creates a new default net/http client with a proxy url and default middleware
// Not providing any middleware would result in having default middleware provided
func GetClientWithProxySettings(proxyUrlStr string, middleware ...Middleware) (*nethttp.Client, error) {
client := getDefaultClientWithoutMiddleware()
transport, err := getTransportWithProxy(proxyUrlStr, nil, middleware...)
if err != nil {
return nil, err
}
client.Transport = transport
return client, nil
}
// GetClientWithAuthenticatedProxySettings creates a new default net/http client with a proxy url and default middleware
// Not providing any middleware would result in having default middleware provided
func GetClientWithAuthenticatedProxySettings(proxyUrlStr string, username string, password string, middleware ...Middleware) (*nethttp.Client, error) {
client := getDefaultClientWithoutMiddleware()
user := url.UserPassword(username, password)
transport, err := getTransportWithProxy(proxyUrlStr, user, middleware...)
if err != nil {
return nil, err
}
client.Transport = transport
return client, nil
}
func getTransportWithProxy(proxyUrlStr string, user *url.Userinfo, middlewares ...Middleware) (nethttp.RoundTripper, error) {
proxyURL, err := url.Parse(proxyUrlStr)
if err != nil {
return nil, err
}
if user != nil {
proxyURL.User = user
}
transport := &nethttp.Transport{
Proxy: nethttp.ProxyURL(proxyURL),
}
if len(middlewares) == 0 {
middlewares = GetDefaultMiddlewares()
}
return NewCustomTransportWithParentTransport(transport, middlewares...), nil
}
// GetDefaultClient creates a new default net/http client with the options configured for the Kiota request adapter
func GetDefaultClient(middleware ...Middleware) *nethttp.Client {
client := getDefaultClientWithoutMiddleware()
client.Transport = NewCustomTransport(middleware...)
return client
}
// used for internal unit testing
func getDefaultClientWithoutMiddleware() *nethttp.Client {
// the default client doesn't come with any other settings than making a new one does, and using the default client impacts behavior for non-kiota requests
return &nethttp.Client{
CheckRedirect: func(req *nethttp.Request, via []*nethttp.Request) error {
return nethttp.ErrUseLastResponse
},
Timeout: time.Second * 100,
}
}
// GetDefaultMiddlewares creates a new default set of middlewares for the Kiota request adapter
func GetDefaultMiddlewares() []Middleware {
return getDefaultMiddleWare(make(map[abs.RequestOptionKey]Middleware))
}
// GetDefaultMiddlewaresWithOptions creates a new default set of middlewares for the Kiota request adapter with options
func GetDefaultMiddlewaresWithOptions(requestOptions ...abs.RequestOption) ([]Middleware, error) {
if len(requestOptions) == 0 {
return GetDefaultMiddlewares(), nil
}
// map of middleware options
middlewareMap := make(map[abs.RequestOptionKey]Middleware)
for _, element := range requestOptions {
switch v := element.(type) {
case *RetryHandlerOptions:
middlewareMap[retryKeyValue] = NewRetryHandlerWithOptions(*v)
case *RedirectHandlerOptions:
middlewareMap[redirectKeyValue] = NewRedirectHandlerWithOptions(*v)
case *CompressionOptions:
middlewareMap[compressKey] = NewCompressionHandlerWithOptions(*v)
case *ParametersNameDecodingOptions:
middlewareMap[parametersNameDecodingKeyValue] = NewParametersNameDecodingHandlerWithOptions(*v)
case *UserAgentHandlerOptions:
middlewareMap[userAgentKeyValue] = NewUserAgentHandlerWithOptions(v)
case *HeadersInspectionOptions:
middlewareMap[headersInspectionKeyValue] = NewHeadersInspectionHandlerWithOptions(*v)
default:
// none of the above types
return nil, errors.New("unsupported option type")
}
}
middleware := getDefaultMiddleWare(middlewareMap)
return middleware, nil
}
// getDefaultMiddleWare creates a new default set of middlewares for the Kiota request adapter
func getDefaultMiddleWare(middlewareMap map[abs.RequestOptionKey]Middleware) []Middleware {
middlewareSource := map[abs.RequestOptionKey]func() Middleware{
retryKeyValue: func() Middleware {
return NewRetryHandler()
},
redirectKeyValue: func() Middleware {
return NewRedirectHandler()
},
compressKey: func() Middleware {
return NewCompressionHandler()
},
parametersNameDecodingKeyValue: func() Middleware {
return NewParametersNameDecodingHandler()
},
userAgentKeyValue: func() Middleware {
return NewUserAgentHandler()
},
headersInspectionKeyValue: func() Middleware {
return NewHeadersInspectionHandler()
},
}
// loop over middlewareSource and add any middleware that wasn't provided in the requestOptions
for key, value := range middlewareSource {
if _, ok := middlewareMap[key]; !ok {
middlewareMap[key] = value()
}
}
var middleware []Middleware
for _, value := range middlewareMap {
middleware = append(middleware, value)
}
return middleware
}