Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: code optimization and style adjustments #38

Merged
merged 4 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions aliyun.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package go_sms_sender

import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
)
Expand Down Expand Up @@ -55,20 +55,13 @@ func (c *AliyunClient) SendMessage(param map[string]string, targetPhoneNumber ..
return err
}

if len(targetPhoneNumber) < 1 {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

phoneNumbers := bytes.Buffer{}
phoneNumbers.WriteString(targetPhoneNumber[0])
for _, s := range targetPhoneNumber[1:] {
phoneNumbers.WriteString(",")
phoneNumbers.WriteString(s)
}

request := dysmsapi.CreateSendSmsRequest()
request.Scheme = "https"
request.PhoneNumbers = phoneNumbers.String()
request.PhoneNumbers = strings.Join(targetPhoneNumber, ",")
request.TemplateCode = c.template
request.TemplateParam = string(requestParam)
request.SignName = c.sign
Expand Down
5 changes: 2 additions & 3 deletions aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type AmazonSNSClient struct {
}

func GetAmazonSNSClient(accessKeyID string, secretAccessKey string, template string, region []string) (*AmazonSNSClient, error) {
if len(region) < 1 {
if len(region) == 0 {
return nil, fmt.Errorf("missing parameter: region")
}

Expand Down Expand Up @@ -60,7 +60,7 @@ func (a *AmazonSNSClient) SendMessage(param map[string]string, targetPhoneNumber

bodyContent := fmt.Sprintf(a.template, code)

if len(targetPhoneNumber) < 1 {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

Expand All @@ -78,7 +78,6 @@ func (a *AmazonSNSClient) SendMessage(param map[string]string, targetPhoneNumber
PhoneNumber: &targetPhoneNumber[i],
MessageAttributes: messageAttributes,
})

if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func GetACSClient(accessToken string, message string, other []string) (*ACSClien
}

func (a *ACSClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
if len(targetPhoneNumber) < 1 {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

Expand Down
13 changes: 5 additions & 8 deletions baidubce.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
package go_sms_sender

import (
"bytes"
"fmt"
"strings"

"github.com/baidubce/bce-sdk-go/services/sms"
"github.com/baidubce/bce-sdk-go/services/sms/api"
Expand All @@ -29,7 +29,7 @@ type BaiduClient struct {
}

func GetBceClient(accessId, accessKey, sign, template string, endpoint []string) (*BaiduClient, error) {
if len(endpoint) < 1 {
if len(endpoint) == 0 {
return nil, fmt.Errorf("missing parameter: endpoint")
}

Expand All @@ -53,18 +53,15 @@ func (c *BaiduClient) SendMessage(param map[string]string, targetPhoneNumber ...
return fmt.Errorf("missing parameter: code")
}

phoneNumbers := bytes.Buffer{}
phoneNumbers.WriteString(targetPhoneNumber[0])
for _, s := range targetPhoneNumber[1:] {
phoneNumbers.WriteString(",")
phoneNumbers.WriteString(s)
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

contentMap := make(map[string]interface{})
contentMap["code"] = code

sendSmsArgs := &api.SendSmsArgs{
Mobile: phoneNumbers.String(),
Mobile: strings.Join(targetPhoneNumber, ","),
SignatureId: c.sign,
Template: c.template,
ContentVar: contentMap,
Expand Down
2 changes: 1 addition & 1 deletion gccpay.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *GCCPAYClient) SendMessage(param map[string]string, targetPhoneNumber ..
return fmt.Errorf("missing parameter: code")
}

if len(targetPhoneNumber) < 1 {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

Expand Down
28 changes: 14 additions & 14 deletions huawei.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"crypto/tls"
"encoding/base64"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand All @@ -29,8 +29,10 @@ import (
"github.com/google/uuid"
)

const WSSE_HEADER_FORMAT = "UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\""
const AUTH_HEADER_VALUE = "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\""
const (
WSSE_HEADER_FORMAT = "UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\""
AUTH_HEADER_VALUE = "WSSE realm=\"SDP\",profile=\"UsernameToken\",type=\"Appkey\""
)

type HuaweiClient struct {
accessId string
Expand Down Expand Up @@ -67,16 +69,14 @@ func (c *HuaweiClient) SendMessage(param map[string]string, targetPhoneNumber ..
return fmt.Errorf("missing parameter: code")
}

templateParas := fmt.Sprintf("[\"%s\"]", code)

phoneNumbers := bytes.Buffer{}
phoneNumbers.WriteString(targetPhoneNumber[0])
for _, s := range targetPhoneNumber[1:] {
phoneNumbers.WriteString(",")
phoneNumbers.WriteString(s)
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

body := buildRequestBody(c.sender, phoneNumbers.String(), c.template, templateParas, "", c.sign)
phoneNumbers := strings.Join(targetPhoneNumber, ",")
templateParas := fmt.Sprintf("[\"%s\"]", code)

body := buildRequestBody(c.sender, phoneNumbers, c.template, templateParas, "", c.sign)
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Authorization"] = AUTH_HEADER_VALUE
Expand All @@ -101,8 +101,8 @@ func buildRequestBody(sender, receiver, templateId, templateParas, statusCallBac
}

func buildWsseHeader(appKey, appSecret string) string {
var cTime = time.Now().Format("2006-01-02T15:04:05Z")
var nonce = uuid.New().String()
cTime := time.Now().Format("2006-01-02T15:04:05Z")
nonce := uuid.New().String()
nonce = strings.ReplaceAll(nonce, "-", "")

h := sha256.New()
Expand Down Expand Up @@ -133,7 +133,7 @@ func post(url string, param []byte, headers map[string]string) (string, error) {
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand Down
8 changes: 4 additions & 4 deletions huyi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -52,7 +52,7 @@ func (hc *HuyiClient) SendMessage(param map[string]string, targetPhoneNumber ...
return fmt.Errorf("missing parameter: code")
}

if len(targetPhoneNumber) < 1 {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missin parer: trgetPhoneNumber")
}

Expand All @@ -68,7 +68,7 @@ func (hc *HuyiClient) SendMessage(param map[string]string, targetPhoneNumber ...
v.Set("password", GetMd5String(password))
v.Set("mobile", mobile)

body := strings.NewReader(v.Encode()) //encode form data
body := strings.NewReader(v.Encode()) // encode form data
client := &http.Client{}
req, _ := http.NewRequest("POST", "http://106.ihuyi.com/webservice/sms.php?method=Submit&format=json", body)

Expand All @@ -79,7 +79,7 @@ func (hc *HuyiClient) SendMessage(param map[string]string, targetPhoneNumber ...
return err
}
defer resp.Body.Close() // ! close ReadCloser
_, err = ioutil.ReadAll(resp.Body)
_, err = io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions infobip.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Destination struct {
}

func GetInfobipClient(sender string, apiKey string, template string, baseUrl []string) (*InfobipClient, error) {
if len(baseUrl) < 1 {
if len(baseUrl) == 0 {
return nil, fmt.Errorf("missing parameter: baseUrl")
}

Expand All @@ -74,7 +74,7 @@ func (c *InfobipClient) SendMessage(param map[string]string, targetPhoneNumber .
return fmt.Errorf("missing parameter: code")
}

if len(targetPhoneNumber) < 1 {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missin parer: trgetPhoneNumber")
}

Expand Down
4 changes: 2 additions & 2 deletions msg91.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func GetMsg91Client(senderId string, authKey string, templateId string) (*Msg91C
}

func (m *Msg91Client) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
if len(targetPhoneNumber) < 1 {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

Expand All @@ -57,7 +57,7 @@ func (m *Msg91Client) SendMessage(param map[string]string, targetPhoneNumber ...

err = postMsg91SendRequest(url, strings.NewReader(payload), m.authKey)
if err != nil {
return fmt.Errorf("send message failed%v", err)
return fmt.Errorf("send message failed: %v", err)
}
}

Expand Down
12 changes: 8 additions & 4 deletions netgsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -47,8 +47,12 @@ func GetNetgsmClient(accessId, accessKey, sign, template string) (*NetgsmClient,
}, nil
}

func (c *NetgsmClient) SendMessage(param map[string]string, targetPhoneNumbers ...string) error {
for _, phoneNumber := range targetPhoneNumbers {
func (c *NetgsmClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

for _, phoneNumber := range targetPhoneNumber {
data := fmt.Sprintf(`
<mainbody>
<header>
Expand Down Expand Up @@ -101,7 +105,7 @@ func (c *NetgsmClient) postXML(url, xmlData string, headers map[string]string) (
}
defer resp.Body.Close()

respBody, err := ioutil.ReadAll(resp.Body)
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand Down
8 changes: 4 additions & 4 deletions smsbao.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package go_sms_sender

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand All @@ -32,7 +32,7 @@ type SmsBaoClient struct {

func GetSmsbaoClient(username string, apikey string, sign string, template string, other []string) (*SmsBaoClient, error) {
var goodsid string
if len(other) < 1 {
if len(other) == 0 {
goodsid = ""
} else {
goodsid = other[0]
Expand All @@ -52,7 +52,7 @@ func (c *SmsBaoClient) SendMessage(param map[string]string, targetPhoneNumber ..
return fmt.Errorf("missing parameter: code")
}

if len(targetPhoneNumber) < 1 {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

Expand All @@ -73,7 +73,7 @@ func (c *SmsBaoClient) SendMessage(param map[string]string, targetPhoneNumber ..
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions submail.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"mime/multipart"
"net/http"
"strings"
Expand Down Expand Up @@ -96,7 +96,7 @@ func (c *SubmailClient) SendMessage(param map[string]string, targetPhoneNumber .
return err
}

result, err := ioutil.ReadAll(resp.Body)
result, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion tencent.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type TencentClient struct {
}

func GetTencentClient(accessId string, accessKey string, sign string, templateId string, appId []string) (*TencentClient, error) {
if len(appId) < 1 {
if len(appId) == 0 {
return nil, fmt.Errorf("missing parameter: appId")
}

Expand All @@ -56,6 +56,10 @@ func GetTencentClient(accessId string, accessKey string, sign string, templateId
}

func (c *TencentClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error {
if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

var paramArray []string
index := 0
for {
Expand Down
6 changes: 5 additions & 1 deletion ucloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type UcloudClient struct {
}

func GetUcloudClient(publicKey string, privateKey string, sign string, template string, projectId []string) (*UcloudClient, error) {
if len(projectId) < 1 {
if len(projectId) == 0 {
return nil, fmt.Errorf("missing parameter: projectId")
}

Expand Down Expand Up @@ -63,6 +63,10 @@ func (c *UcloudClient) SendMessage(param map[string]string, targetPhoneNumber ..
return fmt.Errorf("missing parameter: code")
}

if len(targetPhoneNumber) == 0 {
return fmt.Errorf("missing parameter: targetPhoneNumber")
}

req := c.core.NewSendUSMSMessageRequest()
req.SigContent = ucloud.String(c.Sign)
req.TemplateId = ucloud.String(c.Template)
Expand Down
Loading
Loading