-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudlayer.go
More file actions
65 lines (55 loc) · 1.6 KB
/
cloudlayer.go
File metadata and controls
65 lines (55 loc) · 1.6 KB
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
package cloudlayer
import (
"net/http"
"net/url"
"time"
)
// Version is the current version of the cloudlayer-go SDK.
const Version = "0.1.0"
const defaultBaseURL = "https://api.cloudlayer.io"
// Client is the CloudLayer.io API client. Create one with [NewClient].
type Client struct {
apiKey string
apiVersion APIVersion
baseURL string
httpClient *http.Client
maxRetries int
userAgent string
customHeaders map[string]string
}
// NewClient creates a new CloudLayer.io API client.
//
// The apiKey and apiVersion parameters are required. Use [V1] or [V2] for the
// API version. Functional options can be used to customize the client.
//
// client, err := cloudlayer.NewClient("your-api-key", cloudlayer.V2)
func NewClient(apiKey string, apiVersion APIVersion, opts ...ClientOption) (*Client, error) {
if apiKey == "" {
return nil, &ConfigError{Message: "apiKey must not be empty"}
}
if apiVersion != V1 && apiVersion != V2 {
return nil, &ConfigError{Message: "apiVersion must be V1 or V2"}
}
c := &Client{
apiKey: apiKey,
apiVersion: apiVersion,
baseURL: defaultBaseURL,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
maxRetries: 2,
userAgent: "cloudlayerio-go/" + Version,
}
for _, opt := range opts {
opt(c)
}
// Validate baseURL after options are applied
if _, err := url.ParseRequestURI(c.baseURL); err != nil {
return nil, &ConfigError{Message: "baseURL is not a valid URL: " + c.baseURL}
}
// Validate timeout
if c.httpClient.Timeout <= 0 {
return nil, &ConfigError{Message: "timeout must be greater than 0"}
}
return c, nil
}