-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
108 lines (87 loc) · 2.39 KB
/
option.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package mobilepulsa
import (
"crypto/md5"
"encoding/hex"
"fmt"
"net/http"
"reflect"
)
type AccessType string
const (
Production AccessType = "production"
Development AccessType = "development"
)
// Option is the wrap of the parameters needed for this API Call
type Option struct {
apiKey string
userName string
accessType AccessType
apiCall APIRequest
}
// NewOption creates new Option parameter's for API Call
func NewOption() *Option {
httpClient := &http.Client{}
return &Option{
accessType: Development,
apiCall: &APIRequestImplementation{
HTTPClient: httpClient,
},
}
}
// SetAPIKey will set api-key into parameter apiKey
func (o *Option) SetAPIKey(apiKey string) {
o.apiKey = apiKey
}
// SetUsername will set username into parameter username
func (o *Option) SetUsername(username string) {
o.userName = username
}
// GetUsername will return a username of parameter
func (o *Option) GetUsername() string {
return o.userName
}
// SetAccessProduction will set environment production into parameter accessType
func (o *Option) SetAccessProduction() {
o.accessType = Production
}
// SetAccessDevelopment will set environment development into parameter accessType
func (o *Option) SetAccessDevelopment() {
o.accessType = Development
}
// GetAccessType will set environment production into parameter accessType
func (o *Option) GetAccessType() AccessType {
return o.accessType
}
// SetHTTPClient will set http client into parameter API Call
func (o *Option) SetHTTPClient(httpClient *http.Client) {
o.apiCall = &APIRequestImplementation{
HTTPClient: httpClient,
}
}
// SetAPIRequest will set standard API Request
func (o *Option) SetAPIRequest(apiCall APIRequest) {
o.apiCall = apiCall
}
// GetAPIRequest will get an instance of APIRequest
func (o *Option) GetAPIRequest() APIRequest {
return o.apiCall
}
// Valid is for checking required parameter's for API Call
func (o *Option) Valid() error {
if o.apiKey == "" || reflect.ValueOf(o.apiKey).IsZero() {
return ErrAPIKeyNil
}
if o.userName == "" || reflect.ValueOf(o.userName).IsZero() {
return ErrUsernameNil
}
return nil
}
// Sign for generate signature request based on `username+apiKey+additional`
// with algorithm md5 hash
func (o *Option) Sign(c string) string {
val := fmt.Sprintf("%s%s%s", o.userName, o.apiKey, c)
data := []byte(val)
hash := md5.New()
hash.Write(data)
return hex.EncodeToString(hash.Sum(nil))
}