Skip to content

Commit

Permalink
use http constants instead of magic number
Browse files Browse the repository at this point in the history
  • Loading branch information
dzyanis committed Mar 16, 2022
1 parent 2278312 commit 93633fb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
7 changes: 4 additions & 3 deletions provider/kannel/kannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewKannel(config Config) *Kannel {
}

// Send send sms to n number of people using bulk sms api.
func (c *Kannel) Send(message sachet.Message) (err error) {
func (c *Kannel) Send(message sachet.Message) error {
for _, recipient := range message.To {
queryParams := url.Values{
"from": {message.From},
Expand Down Expand Up @@ -59,9 +59,10 @@ func (c *Kannel) Send(message sachet.Message) (err error) {
return err
}

if response.StatusCode >= 400 {
if response.StatusCode >= http.StatusBadRequest {
return fmt.Errorf("Failed sending sms. statusCode: %d", response.StatusCode)
}
}
return

return nil
}
6 changes: 3 additions & 3 deletions provider/otc/otc.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (c *OTC) loginRequest() error {
}
defer resp.Body.Close()

if resp.StatusCode >= 400 {
if resp.StatusCode >= http.StatusBadRequest {
return fmt.Errorf("OTC API request failed with HTTP status code %d", resp.StatusCode)
}

Expand Down Expand Up @@ -198,15 +198,15 @@ func (c *OTC) SendRequest(method, resource string, payload *smsRequest, attempts
}
defer resp.Body.Close()

if resp.StatusCode == 401 {
if resp.StatusCode == http.StatusUnauthorized {
// Set empty token to force login.
c.Token = ""
if attempts--; attempts > 0 {
return c.SendRequest(method, resource, payload, attempts)
} else {
return nil, err
}
} else if resp.StatusCode >= 400 {
} else if resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("OTC API request %s failed with HTTP status code %d", url, resp.StatusCode)
}

Expand Down
4 changes: 2 additions & 2 deletions provider/smsc/smsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func (c *Smsc) Send(message sachet.Message) (err error) {
return
}

func (c *Smsc) SendOne(message sachet.Message, PhoneNumber string) (err error) {
func (c *Smsc) SendOne(message sachet.Message, phoneNumber string) (err error) {
encoded_message := url.QueryEscape(message.Text)
smsURL := fmt.Sprintf("https://smsc.ru/sys/send.php?login=%s&psw=%s&phones=%s&sender=%s&fmt=0&mes=%s",
c.Login, c.Password, PhoneNumber, message.From, encoded_message)
c.Login, c.Password, phoneNumber, message.From, encoded_message)
var request *http.Request
var resp *http.Response
request, err = http.NewRequest("GET", smsURL, nil)
Expand Down
2 changes: 1 addition & 1 deletion provider/textmagic/textmagic.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewTextMagic(config Config) *TextMagic {
func (tm *TextMagic) Send(message sachet.Message) (err error) {
switch message.Type {
case "", "text":
joinedTo := strings.Join(message.To[:], ",")
joinedTo := strings.Join(message.To, ",")
_, _, err = tm.client.TextMagicApi.SendMessage(tm.auth, textmagic.SendMessageInputObject{
Text: message.Text,
Phones: joinedTo,
Expand Down

0 comments on commit 93633fb

Please sign in to comment.