-
Notifications
You must be signed in to change notification settings - Fork 59
/
xendit.go
56 lines (44 loc) · 1.48 KB
/
xendit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Package xendit provides the binding for Xendit APIs.
package xendit
import (
"net/http"
"sync"
)
// Opt is the default Option for the API call without API client
var Opt Option = Option{
XenditURL: "https://api.xendit.co",
}
var apiRequesterWrapper APIRequesterWrapper = APIRequesterWrapper{}
var httpClient *http.Client = &http.Client{}
// Option is the wrap of the parameters needed for the API call
type Option struct {
SecretKey string // customer's secret API key
XenditURL string // should there be a need to override API base URL
}
// APIRequesterWrapper is the APIRequester with locker for setting the APIRequester
type APIRequesterWrapper struct {
apiRequester APIRequester
mu sync.RWMutex
}
// GetAPIRequester returns the xendit APIRequester.
// If it is already created, it will return the created one.
// Else, it will create a default implementation.
func GetAPIRequester() APIRequester {
if apiRequesterWrapper.apiRequester != nil {
return apiRequesterWrapper.apiRequester
}
apiRequesterWrapper.apiRequester = &APIRequesterImplementation{
HTTPClient: httpClient,
}
return apiRequesterWrapper.apiRequester
}
// SetAPIRequester sets the APIRequester for API call
func SetAPIRequester(apiRequester APIRequester) {
apiRequesterWrapper.mu.Lock()
defer apiRequesterWrapper.mu.Unlock()
apiRequesterWrapper.apiRequester = apiRequester
}
// SetHTTPClient sets the httpClient for API call
func SetHTTPClient(newHTTPClient *http.Client) {
httpClient = newHTTPClient
}