client_option.go   A
last analyzed

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 22
dl 0
loc 47
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A campay.clientOptionFunc.apply 0 2 1
A campay.WithAPIUsername 0 3 2
A campay.WithEnvironment 0 3 2
A campay.WithHTTPClient 0 4 3
A campay.WithAPIPassword 0 3 2
1
package campay
2
3
import (
4
	"net/http"
5
	"strings"
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 campay endpoint for API requests
30
// By default, ProdEnvironment is used.
31
func WithEnvironment(environment Environment) ClientOption {
32
	return clientOptionFunc(func(config *clientConfig) {
33
		config.environment = Environment(strings.TrimRight(environment.String(), "/"))
34
	})
35
}
36
37
// WithAPIUsername sets the campay API username
38
func WithAPIUsername(apiUsername string) ClientOption {
39
	return clientOptionFunc(func(config *clientConfig) {
40
		config.apiUsername = apiUsername
41
	})
42
}
43
44
// WithAPIPassword sets the campay API password
45
func WithAPIPassword(apiPassword string) ClientOption {
46
	return clientOptionFunc(func(config *clientConfig) {
47
		config.apiPassword = apiPassword
48
	})
49
}
50