Test Setup Failed
Push — main ( 0c166d...77ea26 )
by Acho
02:38
created

client_option.go   A

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 33
dl 0
loc 67
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A mtnmomo.clientOptionFunc.apply 0 2 1
A mtnmomo.WithHTTPClient 0 4 3
A mtnmomo.WithBaseURL 0 4 3
A mtnmomo.WithSubscriptionKey 0 3 2
A mtnmomo.WithCollectionAccount 0 5 2
A mtnmomo.WithTargetEnvironment 0 3 2
A mtnmomo.WithDisbursementAccount 0 5 2
1
package mtnmomo
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 set's the base url for the flutterwave 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
// WithSubscriptionKey sets the delay in milliseconds before a response is gotten.
39
func WithSubscriptionKey(subscriptionKey string) Option {
40
	return clientOptionFunc(func(config *clientConfig) {
41
		config.subscriptionKey = subscriptionKey
42
	})
43
}
44
45
// WithTargetEnvironment sets the identifier of the EWP system where the transaction shall be processed.
46
func WithTargetEnvironment(targetEnvironment string) Option {
47
	return clientOptionFunc(func(config *clientConfig) {
48
		config.targetEnvironment = targetEnvironment
49
	})
50
}
51
52
// WithCollectionAccount sets the collection api account
53
func WithCollectionAccount(apiUser string, apiKey string) Option {
54
	return clientOptionFunc(func(config *clientConfig) {
55
		config.collectionAccount = &apiAccount{
56
			apiUser: apiUser,
57
			apiKey:  apiKey,
58
		}
59
	})
60
}
61
62
// WithDisbursementAccount sets the disbursement api account
63
func WithDisbursementAccount(apiUser string, apiKey string) Option {
64
	return clientOptionFunc(func(config *clientConfig) {
65
		config.disbursementAccount = &apiAccount{
66
			apiUser: apiUser,
67
			apiKey:  apiKey,
68
		}
69
	})
70
}
71