-
Notifications
You must be signed in to change notification settings - Fork 14
/
user_agent_handler.go
106 lines (91 loc) · 3.26 KB
/
user_agent_handler.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
package nethttplibrary
import (
"fmt"
nethttp "net/http"
"strings"
abs "github.com/microsoft/kiota-abstractions-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
)
// UserAgentHandler adds the product to the user agent header.
type UserAgentHandler struct {
options UserAgentHandlerOptions
}
// NewUserAgentHandler creates a new user agent handler with the default options.
func NewUserAgentHandler() *UserAgentHandler {
return NewUserAgentHandlerWithOptions(nil)
}
// NewUserAgentHandlerWithOptions creates a new user agent handler with the specified options.
func NewUserAgentHandlerWithOptions(options *UserAgentHandlerOptions) *UserAgentHandler {
if options == nil {
options = NewUserAgentHandlerOptions()
}
return &UserAgentHandler{
options: *options,
}
}
// UserAgentHandlerOptions to use when adding the product to the user agent header.
type UserAgentHandlerOptions struct {
Enabled bool
ProductName string
ProductVersion string
}
// NewUserAgentHandlerOptions creates a new user agent handler options with the default values.
func NewUserAgentHandlerOptions() *UserAgentHandlerOptions {
return &UserAgentHandlerOptions{
Enabled: true,
ProductName: "kiota-go",
ProductVersion: "1.4.5",
}
}
var userAgentKeyValue = abs.RequestOptionKey{
Key: "UserAgentHandler",
}
type userAgentHandlerOptionsInt interface {
abs.RequestOption
GetEnabled() bool
GetProductName() string
GetProductVersion() string
}
// GetKey returns the key value to be used when the option is added to the request context
func (options *UserAgentHandlerOptions) GetKey() abs.RequestOptionKey {
return userAgentKeyValue
}
// GetEnabled returns the value of the enabled property
func (options *UserAgentHandlerOptions) GetEnabled() bool {
return options.Enabled
}
// GetProductName returns the value of the product name property
func (options *UserAgentHandlerOptions) GetProductName() string {
return options.ProductName
}
// GetProductVersion returns the value of the product version property
func (options *UserAgentHandlerOptions) GetProductVersion() string {
return options.ProductVersion
}
const userAgentHeaderKey = "User-Agent"
func (middleware UserAgentHandler) Intercept(pipeline Pipeline, middlewareIndex int, req *nethttp.Request) (*nethttp.Response, error) {
obsOptions := GetObservabilityOptionsFromRequest(req)
if obsOptions != nil {
observabilityName := obsOptions.GetTracerInstrumentationName()
ctx := req.Context()
ctx, span := otel.GetTracerProvider().Tracer(observabilityName).Start(ctx, "UserAgentHandler_Intercept")
span.SetAttributes(attribute.Bool("com.microsoft.kiota.handler.useragent.enable", true))
defer span.End()
req = req.WithContext(ctx)
}
options, ok := req.Context().Value(userAgentKeyValue).(userAgentHandlerOptionsInt)
if !ok {
options = &middleware.options
}
if options.GetEnabled() {
additionalValue := fmt.Sprintf("%s/%s", options.GetProductName(), options.GetProductVersion())
currentValue := req.Header.Get(userAgentHeaderKey)
if currentValue == "" {
req.Header.Set(userAgentHeaderKey, additionalValue)
} else if !strings.Contains(currentValue, additionalValue) {
req.Header.Set(userAgentHeaderKey, fmt.Sprintf("%s %s", currentValue, additionalValue))
}
}
return pipeline.Next(req, middlewareIndex)
}