forked from scaleway/scaleway-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_option.go
203 lines (170 loc) · 5.06 KB
/
client_option.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
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
package scw
import (
"net/http"
"net/url"
"github.com/scaleway/scaleway-sdk-go/internal/auth"
"github.com/scaleway/scaleway-sdk-go/internal/errors"
)
// ClientOption is a function which applies options to a settings object.
type ClientOption func(*settings)
// httpClient wraps the net/http Client Do method
type httpClient interface {
Do(*http.Request) (*http.Response, error)
}
// WithHTTPClient client option allows passing a custom http.Client which will be used for all requests.
func WithHTTPClient(httpClient httpClient) ClientOption {
return func(s *settings) {
s.httpClient = httpClient
}
}
// WithoutAuth client option sets the client token to an empty token.
func WithoutAuth() ClientOption {
return func(s *settings) {
s.token = auth.NewNoAuth()
}
}
// WithAuth client option sets the client access key and secret key.
func WithAuth(accessKey, secretKey string) ClientOption {
return func(s *settings) {
s.token = auth.NewToken(accessKey, secretKey)
}
}
// WithAPIURL client option overrides the API URL of the Scaleway API to the given URL.
func WithAPIURL(apiURL string) ClientOption {
return func(s *settings) {
s.apiURL = apiURL
}
}
// WithInsecure client option enables insecure transport on the client.
func WithInsecure() ClientOption {
return func(s *settings) {
s.insecure = true
}
}
// WithUserAgent client option append a user agent to the default user agent of the SDK.
func WithUserAgent(ua string) ClientOption {
return func(s *settings) {
if s.userAgent != "" && ua != "" {
s.userAgent += " "
}
s.userAgent += ua
}
}
// withDefaultUserAgent client option overrides the default user agent of the SDK.
func withDefaultUserAgent(ua string) ClientOption {
return func(s *settings) {
s.userAgent = ua
}
}
// WithProfile client option configures a client from the given profile.
func WithProfile(p *Profile) ClientOption {
return func(s *settings) {
accessKey := ""
if p.AccessKey != nil {
accessKey = *p.AccessKey
}
if p.SecretKey != nil {
s.token = auth.NewToken(accessKey, *p.SecretKey)
}
if p.APIURL != nil {
s.apiURL = *p.APIURL
}
if p.Insecure != nil {
s.insecure = *p.Insecure
}
if p.DefaultOrganizationID != nil {
organizationID := *p.DefaultOrganizationID
s.defaultOrganizationID = &organizationID
}
if p.DefaultRegion != nil {
defaultRegion := Region(*p.DefaultRegion)
s.defaultRegion = &defaultRegion
}
if p.DefaultZone != nil {
defaultZone := Zone(*p.DefaultZone)
s.defaultZone = &defaultZone
}
}
}
// WithProfile client option configures a client from the environment variables.
func WithEnv() ClientOption {
return WithProfile(LoadEnvProfile())
}
// WithDefaultOrganizationID client option sets the client default organization ID.
//
// It will be used as the default value of the organization_id field in all requests made with this client.
func WithDefaultOrganizationID(organizationID string) ClientOption {
return func(s *settings) {
s.defaultOrganizationID = &organizationID
}
}
// WithDefaultRegion client option sets the client default region.
//
// It will be used as the default value of the region field in all requests made with this client.
func WithDefaultRegion(region Region) ClientOption {
return func(s *settings) {
s.defaultRegion = ®ion
}
}
// WithDefaultZone client option sets the client default zone.
//
// It will be used as the default value of the zone field in all requests made with this client.
func WithDefaultZone(zone Zone) ClientOption {
return func(s *settings) {
s.defaultZone = &zone
}
}
// WithDefaultPageSize client option overrides the default page size of the SDK.
//
// It will be used as the default value of the page_size field in all requests made with this client.
func WithDefaultPageSize(pageSize int32) ClientOption {
return func(s *settings) {
s.defaultPageSize = &pageSize
}
}
// settings hold the values of all client options
type settings struct {
apiURL string
token auth.Auth
userAgent string
httpClient httpClient
insecure bool
defaultOrganizationID *string
defaultRegion *Region
defaultZone *Zone
defaultPageSize *int32
}
func newSettings() *settings {
return &settings{}
}
func (s *settings) apply(opts []ClientOption) {
for _, opt := range opts {
opt(s)
}
}
func (s *settings) validate() error {
var err error
if s.token == nil {
return errors.New("no credential option provided")
}
_, err = url.Parse(s.apiURL)
if err != nil {
return errors.Wrap(err, "invalid url %s", s.apiURL)
}
// TODO: Check OrganizationID format
if s.defaultOrganizationID != nil && *s.defaultOrganizationID == "" {
return errors.New("default organization id cannot be empty")
}
// TODO: Check Region format
if s.defaultRegion != nil && *s.defaultRegion == "" {
return errors.New("default region cannot be empty")
}
// TODO: Check Zone format
if s.defaultZone != nil && *s.defaultZone == "" {
return errors.New("default zone cannot be empty")
}
if s.defaultPageSize != nil && *s.defaultPageSize <= 0 {
return errors.New("default page size cannot be <= 0")
}
return nil
}