-
Notifications
You must be signed in to change notification settings - Fork 13
/
client_config.go
72 lines (60 loc) · 1.4 KB
/
client_config.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
// Code generated for API Clients. DO NOT EDIT.
package ngrok
import (
"net/http"
"net/url"
)
var (
defaultBaseURL, _ = url.Parse("https://api.ngrok.com")
)
type ClientConfig struct {
APIKey string
BaseURL *url.URL
HTTPClient *http.Client
UserAgent *string
}
func NewClientConfig(apiKey string, opts ...ClientConfigOption) *ClientConfig {
var configOpts clientConfigOpts
configOpts.parseOptions(opts)
return &ClientConfig{
APIKey: apiKey,
BaseURL: configOpts.baseURL,
HTTPClient: configOpts.httpClient,
UserAgent: configOpts.userAgent,
}
}
type clientConfigOpts struct {
baseURL *url.URL
httpClient *http.Client
userAgent *string
}
type ClientConfigOption func(cc *clientConfigOpts)
func (cc *clientConfigOpts) parseOptions(opts []ClientConfigOption) {
for _, o := range opts {
o(cc)
}
cc.setDefaults()
}
func (cc *clientConfigOpts) setDefaults() {
if cc.httpClient == nil {
cc.httpClient = http.DefaultClient
}
if cc.baseURL == nil {
cc.baseURL = defaultBaseURL
}
}
func WithBaseURL(baseURL string) ClientConfigOption {
return func(cc *clientConfigOpts) {
cc.baseURL, _ = url.Parse(baseURL)
}
}
func WithHTTPClient(httpClient *http.Client) ClientConfigOption {
return func(cc *clientConfigOpts) {
cc.httpClient = httpClient
}
}
func WithUserAgent(userAgent string) ClientConfigOption {
return func(cc *clientConfigOpts) {
cc.userAgent = &userAgent
}
}