-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssclient.go
88 lines (71 loc) · 2.21 KB
/
ssclient.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
package ssclient
import (
"context"
"fmt"
"net/http"
"time"
kabs "github.com/microsoft/kiota-abstractions-go"
khttp "github.com/microsoft/kiota-http-go"
"go.artefactual.dev/ssclient/kiota"
)
func New(httpClient *http.Client, baseURL, username, key string) (*kiota.Client, error) {
if httpClient == nil {
httpClient = http.DefaultClient
}
if err := configureMiddleware(httpClient); err != nil {
return nil, fmt.Errorf("configure client middleware: %v", err)
}
adapter, err := khttp.NewNetHttpRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient(
&authProvider{username: username, key: key},
nil,
nil,
httpClient,
)
if err != nil {
return nil, fmt.Errorf("create client adapter: %v", err)
}
adapter.SetBaseUrl(baseURL)
return kiota.NewClient(adapter), nil
}
// configureMiddleware installs the middlewares needed by this client.
func configureMiddleware(client *http.Client) error {
var middlewares []khttp.Middleware
userAgentOpts := khttp.UserAgentHandlerOptions{
Enabled: true,
ProductName: "ssclient-go",
ProductVersion: "v0",
}
compressionOpts := khttp.NewCompressionOptions(false)
retryOpts := khttp.RetryHandlerOptions{
ShouldRetry: func(delay time.Duration, executionCount int, request *http.Request, response *http.Response) bool {
// TODO: we use go-retryablehttp but this could be provided instead.
return false
},
}
// We rely on the default set of middlewares provided by Kiota with a small
// number of customizations.
middlewares, err := khttp.GetDefaultMiddlewaresWithOptions(
&userAgentOpts,
&compressionOpts,
&retryOpts,
)
if err != nil {
return err
}
client.Transport = khttp.NewCustomTransportWithParentTransport(client.Transport, middlewares...)
return nil
}
type authProvider struct {
username string
key string
}
func NewStorageServiceAuthProvider(username, key string) *authProvider {
return &authProvider{
username: username,
key: key,
}
}
func (p *authProvider) AuthenticateRequest(ctx context.Context, request *kabs.RequestInformation, additionalAuthenticationContext map[string]interface{}) error {
request.Headers.Add("Authorization", fmt.Sprintf("ApiKey %s:%s", p.username, p.key))
return nil
}