-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
59 lines (50 loc) · 1.14 KB
/
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
package lingvo
import (
"net/http"
"net/url"
)
// config contains tunables for Lingvo API client
type config struct {
httpClient *http.Client
baseURL *url.URL
userAgent string
}
var defaultConfig = config{
httpClient: http.DefaultClient,
baseURL: mustParseURL(defaultBaseURL),
userAgent: defaultUserAgent,
}
// Option configure Lingvo API client
type Option func(cfg *config)
// WithHTTPClient sets custom HTTP client for the API client. If not set,
// http.DefaultClient is used.
func WithHTTPClient(client *http.Client) Option {
return func(cfg *config) {
if client == nil {
panic("HTTP client can't be nil")
}
cfg.httpClient = client
}
}
// WithBaseURL sets custom API base URL.
func WithBaseURL(baseURL *url.URL) Option {
return func(cfg *config) {
if baseURL == nil {
panic("base URL can't be nil")
}
cfg.baseURL = baseURL
}
}
// WithUserAgent sets custom user agent for the API client.
func WithUserAgent(userAgent string) Option {
return func(cfg *config) {
cfg.userAgent = userAgent
}
}
func mustParseURL(urlStr string) *url.URL {
u, err := url.Parse(urlStr)
if err != nil {
panic(err)
}
return u
}