| Total Lines | 47 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 | }) |
||
| 50 |