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
|
|
|
|