-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
58 lines (50 loc) · 1.26 KB
/
options.go
File metadata and controls
58 lines (50 loc) · 1.26 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
package cloudlayer
import (
"net/http"
"time"
)
// ClientOption configures a [Client]. Use With* functions to create options.
type ClientOption func(*Client)
// WithBaseURL sets the API base URL. Defaults to "https://api.cloudlayer.io".
func WithBaseURL(url string) ClientOption {
return func(c *Client) {
c.baseURL = url
}
}
// WithHTTPClient sets a custom [http.Client] for requests.
func WithHTTPClient(hc *http.Client) ClientOption {
return func(c *Client) {
c.httpClient = hc
}
}
// WithTimeout sets the HTTP client timeout. Defaults to 30 seconds.
func WithTimeout(d time.Duration) ClientOption {
return func(c *Client) {
c.httpClient.Timeout = d
}
}
// WithMaxRetries sets the maximum number of retries for retryable requests.
// Clamped to [0, 5]. Defaults to 2.
func WithMaxRetries(n int) ClientOption {
return func(c *Client) {
if n < 0 {
n = 0
}
if n > 5 {
n = 5
}
c.maxRetries = n
}
}
// WithUserAgent sets the User-Agent header. Defaults to "cloudlayerio-go/{version}".
func WithUserAgent(ua string) ClientOption {
return func(c *Client) {
c.userAgent = ua
}
}
// WithHeaders sets additional headers to include on every request.
func WithHeaders(h map[string]string) ClientOption {
return func(c *Client) {
c.customHeaders = h
}
}