-
Notifications
You must be signed in to change notification settings - Fork 4
/
sms_clone_repo.go
156 lines (122 loc) · 3.87 KB
/
sms_clone_repo.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
package sms_nigeria_go
import (
"errors"
"strings"
)
type SmsCloneNotification struct {
Username string
Password string
Sender string
Recipient string
Message string
}
type SmsCloneCredential struct {
Username string
Password string
}
type SmsCloneResponse struct {
BatchCode string
BatchDescription string
StatusCode string
Recipient string
MessageID string
MessageStatus string
StatusDescription string
}
type SmsCloneRepository struct{}
func (*SmsCloneRepository) SmsClone(sms *SmsCloneNotification, route string) (response SmsCloneResponse, err error) {
var smsData SmsCloneNotification
var smsClone SmsCloneResponse
var preparedURL string
smsData.Sender = sms.Sender
smsData.Recipient = sms.Recipient
smsData.Message = sms.Message
switch route {
case SmsCloneNormalRoute:
preparedURL = SmsCloneNormalRouteURLCreate + "?username=" + sms.Username +
"&password=" + sms.Password + "&sender=" + sms.Sender + "&recipient=" + sms.Recipient + "&message=" +
strings.Replace(sms.Message, " ", "%20", -1)
case SmsCloneDndRoute:
preparedURL = SmsCloneDndRouteURLCreate + "?username=" + sms.Username +
"&password=" + sms.Password + "&sender=" + sms.Sender + "&recipient=" + sms.Recipient + "&message=" +
strings.Replace(sms.Message, " ", "%20", -1)
case SmsCloneNormalAndDndRoute:
preparedURL = SmsCloneNormalAndDndRouteURLCreate + "?username=" + sms.Username +
"&password=" + sms.Password + "&sender=" + sms.Sender + "&recipient=" + sms.Recipient + "&message=" +
strings.Replace(sms.Message, " ", "%20", -1)
default:
preparedURL = SmsCloneNormalRouteURLCreate + "?username=" + sms.Username +
"&password=" + sms.Password + "&sender=" + sms.Sender + "&recipient=" + sms.Recipient + "&message=" +
strings.Replace(sms.Message, " ", "%20", -1)
}
// contact endpoint
var result []byte
result, err = ContactEndpoint(preparedURL)
if err != nil {
return SmsCloneResponse{}, err
}
smsClone, err = ResponseParser(string(result))
if err != nil {
return SmsCloneResponse{}, err
}
return smsClone, err
}
func (*SmsCloneRepository) SmsCloneCheckBalance(sms *SmsCloneCredential) (response string, err error) {
preparedURL := SmsCloneCheckBalanceURL + "?username=" + sms.Username +
"&password=" + sms.Password
// contact endpoint
var result []byte
result, err = ContactEndpoint(preparedURL)
if err != nil {
return response, err
}
response = string(result)
return response, nil
}
func (*SmsCloneRepository) ValidateSmsCloneInput(smsInfo *SmsCloneNotification) (err map[string]interface{}) {
err = make(map[string]interface{})
if smsInfo.Username == "" {
err["Username"] = ErrMissingUsername
}
if smsInfo.Sender == "" {
err["Sender"] = ErrMissingSender
}
if smsInfo.Recipient == "" {
err["Recipient"] = ErrMissingRecipient
}
if smsInfo.Message == "" {
err["Message"] = ErrMissingMessage
}
if smsInfo.Password == "" {
err["Password"] = ErrMissingPassword
}
return
}
func (*SmsCloneRepository) ValidateSmsCloneCredentials(smsInfo *SmsCloneCredential) (err map[string]interface{}) {
err = make(map[string]interface{})
if smsInfo.Username == "" {
err["Username"] = ErrMissingUsername
}
if smsInfo.Password == "" {
err["Password"] = ErrMissingPassword
}
return
}
func ResponseParser(text string) (parsedResponse SmsCloneResponse, err error) {
result := strings.Split(text, "|")
if len(result) <= 1 {
err = errors.New(PossibleCredentialsErr)
return SmsCloneResponse{}, err
}
first := strings.Split(result[0], "-")
second := strings.Split(result[0], ":")
third := strings.Split(first[1], ":")
parsedResponse.BatchCode = first[0]
parsedResponse.BatchDescription = third[0]
parsedResponse.StatusCode = second[1]
parsedResponse.Recipient = result[1]
parsedResponse.MessageID = result[2]
parsedResponse.MessageStatus = result[3]
parsedResponse.StatusDescription = result[4]
return
}