client_option.go   A
last analyzed

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 20
dl 0
loc 41
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A httpsms.clientOptionFunc.apply 0 2 1
A httpsms.WithBaseURL 0 4 3
A httpsms.WithHTTPClient 0 4 3
A httpsms.WithAPIKey 0 3 2
1
package httpsms
2
3
import (
4
	"net/http"
5
	"strings"
6
)
7
8
// Option is options for constructing a client
9
type Option interface {
10
	apply(config *clientConfig)
11
}
12
13
type clientOptionFunc func(config *clientConfig)
14
15
func (fn clientOptionFunc) apply(config *clientConfig) {
16
	fn(config)
17
}
18
19
// WithHTTPClient sets the underlying HTTP client used for API requests.
20
// By default, http.DefaultClient is used.
21
func WithHTTPClient(httpClient *http.Client) Option {
22
	return clientOptionFunc(func(config *clientConfig) {
23
		if httpClient != nil {
24
			config.httpClient = httpClient
25
		}
26
	})
27
}
28
29
// WithBaseURL sets the base url for the httpsms API
30
func WithBaseURL(baseURL string) Option {
31
	return clientOptionFunc(func(config *clientConfig) {
32
		if baseURL != "" {
33
			config.baseURL = strings.TrimRight(baseURL, "/")
34
		}
35
	})
36
}
37
38
// WithAPIKey sets the api key for the httpsms API
39
func WithAPIKey(apiKey string) Option {
40
	return clientOptionFunc(func(config *clientConfig) {
41
		config.apiKey = apiKey
42
	})
43
}
44