client_option.go   A
last analyzed

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 27
dl 0
loc 57
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A mobilenig.WithEnvironment 0 4 4
A mobilenig.WithAPIKey 0 3 2
A mobilenig.clientOptionFunc.apply 0 2 1
A mobilenig.WithUsername 0 3 2
A mobilenig.WithHTTPClient 0 4 3
A mobilenig.WithBaseURL 0 4 3
1
package mobilenig
2
3
import (
4
	"net/http"
5
	"net/url"
6
)
7
8
// ClientOption are options for constructing a client
9
type ClientOption 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 requests.
20
// By default, http.DefaultClient is used.
21
func WithHTTPClient(httpClient *http.Client) ClientOption {
22
	return clientOptionFunc(func(config *clientConfig) {
23
		if httpClient != nil {
24
			config.httpClient = httpClient
25
		}
26
	})
27
}
28
29
// WithEnvironment sets the MobileNig endpoint for API requests
30
// By default, LiveEnvironment is used.
31
func WithEnvironment(environment Environment) ClientOption {
32
	return clientOptionFunc(func(config *clientConfig) {
33
		if environment == LiveEnvironment || environment == TestEnvironment {
34
			config.environment = environment
35
		}
36
	})
37
}
38
39
// WithUsername sets the MobileNig API username
40
func WithUsername(username string) ClientOption {
41
	return clientOptionFunc(func(config *clientConfig) {
42
		config.username = username
43
	})
44
}
45
46
// WithAPIKey sets the MobileNig API password
47
func WithAPIKey(apiKey string) ClientOption {
48
	return clientOptionFunc(func(config *clientConfig) {
49
		config.apiKey = apiKey
50
	})
51
}
52
53
// WithBaseURL sets the MobileNig API base URL
54
func WithBaseURL(baseURL *url.URL) ClientOption {
55
	return clientOptionFunc(func(config *clientConfig) {
56
		if baseURL != nil {
57
			config.baseURL = baseURL.String()
58
		}
59
	})
60
}
61