-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
212 lines (183 loc) · 5.74 KB
/
client.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package gosms
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"strconv"
"sync"
"time"
)
// TokenTimeOut indicates refresh time of the token for accessing APIs.
const TokenTimeOut = 20 * time.Minute
// DefaultBulkURL is used to send requests to SMS provider by default.
const DefaultBulkURL = "https://restfulsms.com/api"
var (
tokenTimestamp time.Time
cachedToken string
locker sync.Mutex
)
type (
// Config holds the data that is required for constructing Token.
Config struct {
BaseURL string
APIKey string
SecretKey string
DisableCache bool
}
// BulkSMSProvider exposes the methods of bulk SMS system.
BulkSMSProvider interface {
GetCredit() (int, error)
SendVerificationCode(mobile, code string) (string, error)
SendByTemplate(mobile string, templateId int, params map[string]string) (string, error)
}
// TokenProvider is used to fetch the token from the server.
TokenProvider interface {
Get() (string, error)
}
// Token handles the requests for providing token
Token struct {
Config Config
}
BulkSMS struct {
BaseURL string
Token TokenProvider
}
tokenResult struct {
TokenKey string `json:"TokenKey"`
IsSuccessful bool `json:"IsSuccessful"`
Message string `json:"Message"`
}
creditResult struct {
Credit float32 `json:"Credit"`
IsSuccessful bool `json:"IsSuccessful"`
Message string `json:"Message"`
}
verificationCodeResult struct {
VerificationCodeId float64 `json:"VerificationCodeId"`
IsSuccessful bool `json:"IsSuccessful"`
Message string `json:"Message"`
}
)
// NewBulkSMSClient creates a value that handles all requests for bulk SMS system.
func NewBulkSMSClient(token TokenProvider, url string) BulkSMSProvider {
return &BulkSMS{
BaseURL: url,
Token: token,
}
}
// NewToken provides value for fetching token from the server.
func NewToken(config Config) TokenProvider {
url := config.BaseURL
if url == "" {
url = DefaultBulkURL
}
token := &Token{Config: config}
token.Config.BaseURL = url
return token
}
// Get method fetches token from the server.
// It is thread-safe and handles the caching mechanism by default to prevent unnecessary requests.
func (t *Token) Get() (string, error) {
if !t.Config.DisableCache && (time.Now().Sub(tokenTimestamp) < TokenTimeOut) {
return cachedToken, nil
}
locker.Lock()
defer locker.Unlock()
url := t.Config.BaseURL + "/token"
data := struct {
UserApiKey string `json:"UserApiKey"`
SecretKey string `json:"SecretKey"`
}{UserApiKey: t.Config.APIKey, SecretKey: t.Config.SecretKey}
b, _ := json.Marshal(&data)
r, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
r.Header.Add("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(r)
result := tokenResult{}
_ = json.NewDecoder(resp.Body).Decode(&result)
if result.IsSuccessful {
cachedToken = result.TokenKey
tokenTimestamp = time.Now()
return result.TokenKey, nil
}
return "", errors.New("invalid API key or secret key")
}
// GetCredit fetches the amount of the SMS count that remains on the account.
// It uses the token that provides by the Token.Get() method.
func (b *BulkSMS) GetCredit() (int, error) {
url := b.BaseURL + "/credit"
r, _ := http.NewRequest(http.MethodGet, url, nil)
token, err := b.Token.Get()
if err != nil {
return 0, err
}
r.Header.Add("Content-Type", "application/json")
r.Header.Add("x-sms-ir-secure-token", token)
resp, _ := http.DefaultClient.Do(r)
data := creditResult{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return 0, err
}
defer resp.Body.Close()
if data.IsSuccessful {
return int(data.Credit), nil
}
return 0, errors.New("invalid token")
}
// SendVerificationCode sends a value(code) with default message template to the provided mobile number.
func (b *BulkSMS) SendVerificationCode(mobile, code string) (string, error) {
url := b.BaseURL + "/VerificationCode"
body := struct {
MobileNumber string `json:"MobileNumber"`
Code string `json:"Code"`
}{
MobileNumber: mobile,
Code: code,
}
bs, _ := json.Marshal(&body)
r, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(bs))
token, _ := b.Token.Get()
r.Header.Add("Content-Type", "application/json")
r.Header.Add("x-sms-ir-secure-token", token)
resp, _ := http.DefaultClient.Do(r)
result := verificationCodeResult{}
_ = json.NewDecoder(resp.Body).Decode(&result)
if result.IsSuccessful {
return strconv.FormatFloat(result.VerificationCodeId, 'f', 0, 64), nil
}
return "0", errors.New("invalid mobile")
}
// SendByTemplate sends a bunch of key-value pair of data with a provided template(TemplateId) to the given mobile number.
func (b *BulkSMS) SendByTemplate(mobile string, templateId int, params map[string]string) (string, error) {
url := b.BaseURL + "/UltraFastSend"
type param struct {
Parameter string `json:"Parameter"`
ParameterValue string `json:"ParameterValue"`
}
body := struct {
Mobile string `json:"Mobile"`
TemplateId int `json:"TemplateId"`
ParameterArray []*param `json:"ParameterArray"`
}{
Mobile: mobile,
TemplateId: templateId,
}
if params != nil {
for key, value := range params {
body.ParameterArray = append(body.ParameterArray, ¶m{Parameter: key, ParameterValue: value})
}
}
bs, _ := json.Marshal(&body)
r, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(bs))
token, _ := b.Token.Get()
r.Header.Add("Content-Type", "application/json")
r.Header.Add("x-sms-ir-secure-token", token)
resp, _ := http.DefaultClient.Do(r)
result := verificationCodeResult{}
_ = json.NewDecoder(resp.Body).Decode(&result)
if result.IsSuccessful {
return strconv.FormatFloat(result.VerificationCodeId, 'f', 0, 64), nil
}
return "0", errors.New("invalid data")
}