-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoy.go
56 lines (45 loc) · 1.51 KB
/
oy.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 oygo
import (
"net/http"
"sync"
)
// Opt is the default Option for the API call without API client
var Opt Option = Option{
OyURL: "https://partner.oyindonesia.com",
// OyURL: "https://api-stg.oyindonesia.com",
}
var httpClient *http.Client = &http.Client{}
var apiRequesterWrapper APIRequesterWrapper = APIRequesterWrapper{}
// Option is the wrap of the parameters needed for the API call
type Option struct {
SecretKey string // customer's secret API key
Username string // customer's username
OyURL 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 oy 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
}