-
Notifications
You must be signed in to change notification settings - Fork 5
/
atlas_client.go
125 lines (111 loc) · 3.43 KB
/
atlas_client.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
package admin // import "go.mongodb.org/atlas-sdk/v20231115012/admin"
import (
"errors"
"fmt"
"net/http"
"runtime"
"strings"
"github.com/mongodb-forks/digest"
"go.mongodb.org/atlas-sdk/v20231115012/internal/core"
)
const (
// DefaultCloudURL is default base URL for the services.
DefaultCloudURL = "https://cloud.mongodb.com"
// Version the version of the current API client inherited from.
Version = core.Version
// ClientName of the v2 API client.
ClientName = "go-atlas-sdk-admin"
)
// NewClient returns a new API Client.
func NewClient(modifiers ...ClientModifier) (*APIClient, error) {
userAgent := fmt.Sprintf("%s/%s (%s;%s)", ClientName, Version, runtime.GOOS, runtime.GOARCH)
defaultConfig := &Configuration{
HTTPClient: http.DefaultClient,
Servers: ServerConfigurations{ServerConfiguration{
URL: DefaultCloudURL,
},
},
UserAgent: userAgent,
}
for _, modifierFunction := range modifiers {
err := modifierFunction(defaultConfig)
if err != nil {
return nil, err
}
}
return NewAPIClient(defaultConfig), nil
}
// ClientModifiers lets you create function that controls configuration before creating client.
type ClientModifier func(*Configuration) error
// UseDigestAuth provides Digest authentication for Go SDK.
// UseDigestAuth is provided as helper to create a default HTTP client that supports HTTP Digest authentication.
// Warning: any previously set httpClient will be overwritten. To fully customize HttpClient use UseHTTPClient method.
func UseDigestAuth(apiKey, apiSecret string) ClientModifier {
return func(c *Configuration) error {
transport := digest.NewTransport(apiKey, apiSecret)
httpClient, err := transport.Client()
if err != nil {
return err
}
c.HTTPClient = httpClient
return nil
}
}
// Advanced modifiers.
// UseHTTPClient set custom http client implementation.
//
// Warning: UseHTTPClient overrides any previously set httpClient including the one set by UseDigestAuth.
// To set a custom http client with HTTP diggest support use:
//
// transport := digest.NewTransportWithHTTPRoundTripper(apiKey, apiSecret, yourHttpTransport)
// client := UseHTTPClient(transport.Client())
func UseHTTPClient(client *http.Client) ClientModifier {
return func(c *Configuration) error {
c.HTTPClient = client
return nil
}
}
// UseDebug enable debug level logging.
func UseDebug(debug bool) ClientModifier {
return func(c *Configuration) error {
c.Debug = debug
return nil
}
}
// UseBaseURL set custom base url. If empty, default is used.
func UseBaseURL(baseURL string) ClientModifier {
return func(c *Configuration) error {
if baseURL == "" {
baseURL = DefaultCloudURL
}
urlWithoutSuffix := strings.TrimSuffix(baseURL, "/")
c.Servers = ServerConfigurations{ServerConfiguration{
URL: urlWithoutSuffix,
}}
return nil
}
}
// UseUserAgent set custom UserAgent header.
func UseUserAgent(userAgent string) ClientModifier {
return func(c *Configuration) error {
c.UserAgent = userAgent
return nil
}
}
// AsError checks if API returned known error type.
func AsError(err error) (*ApiError, bool) {
var openapiError *GenericOpenAPIError
if ok := errors.As(err, &openapiError); !ok {
return nil, false
}
errModel := openapiError.Model()
return &errModel, true
}
// IsErrorCode returns true if the error contains the specific code.
func IsErrorCode(err error, code string) bool {
mappedErr, _ := AsError(err)
if mappedErr == nil {
return false
}
return mappedErr.GetErrorCode() == code
}