From 1af727e7fe5320021355d1afa3c0e2129033bfbf Mon Sep 17 00:00:00 2001 From: maratrixx Date: Fri, 22 Dec 2023 16:44:52 +0800 Subject: [PATCH 1/3] bugfix: fixed volcengine sdk component error handling --- volcengine.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/volcengine.go b/volcengine.go index bcd7035..598125b 100644 --- a/volcengine.go +++ b/volcengine.go @@ -15,9 +15,9 @@ package go_sms_sender import ( - "bytes" "encoding/json" "fmt" + "strings" "github.com/volcengine/volc-sdk-golang/service/sms" ) @@ -58,20 +58,26 @@ func (c *VolcClient) SendMessage(param map[string]string, targetPhoneNumber ...s return fmt.Errorf("missing parameter: targetPhoneNumber") } - phoneNumbers := bytes.Buffer{} - phoneNumbers.WriteString(targetPhoneNumber[0]) - for _, s := range targetPhoneNumber[1:] { - phoneNumbers.WriteString(",") - phoneNumbers.WriteString(s) - } - req := &sms.SmsRequest{ SmsAccount: c.smsAccount, Sign: c.sign, TemplateID: c.template, TemplateParam: string(requestParam), - PhoneNumbers: phoneNumbers.String(), + PhoneNumbers: strings.Join(targetPhoneNumber, ","), + } + + resp, statusCode, err := c.core.Send(req) + if err != nil { + return fmt.Errorf("send message failed, message:%q", err.Error()) } - _, _, err = c.core.Send(req) - return err + + if statusCode < 200 || statusCode > 299 { + return fmt.Errorf("send message failed, statusCode:%d", statusCode) + } + + if resp.ResponseMetadata.Error != nil { + return fmt.Errorf("send message failed, code:%q, message:%q", resp.ResponseMetadata.Error.Code, resp.ResponseMetadata.Error.Message) + } + + return nil } From 206b2c4e72b297b5b5cc311c33c523a229f5adc2 Mon Sep 17 00:00:00 2001 From: hsluoyz Date: Fri, 22 Dec 2023 19:33:08 +0800 Subject: [PATCH 2/3] Update volcengine.go --- volcengine.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/volcengine.go b/volcengine.go index 598125b..044920b 100644 --- a/volcengine.go +++ b/volcengine.go @@ -68,15 +68,13 @@ func (c *VolcClient) SendMessage(param map[string]string, targetPhoneNumber ...s resp, statusCode, err := c.core.Send(req) if err != nil { - return fmt.Errorf("send message failed, message:%q", err.Error()) + return fmt.Errorf("send message failed, error: %q", err.Error()) } - if statusCode < 200 || statusCode > 299 { - return fmt.Errorf("send message failed, statusCode:%d", statusCode) + return fmt.Errorf("send message failed, statusCode :%d", statusCode) } - if resp.ResponseMetadata.Error != nil { - return fmt.Errorf("send message failed, code:%q, message:%q", resp.ResponseMetadata.Error.Code, resp.ResponseMetadata.Error.Message) + return fmt.Errorf("send message failed, code: %q, message: %q", resp.ResponseMetadata.Error.Code, resp.ResponseMetadata.Error.Message) } return nil From 7aaffe9736732f0dcbc7bd40188ed16b50911916 Mon Sep 17 00:00:00 2001 From: maratrixx Date: Fri, 22 Dec 2023 20:48:17 +0800 Subject: [PATCH 3/3] optimize: code optimization and style adjustments Signed-off-by: maratrixx --- aliyun.go | 13 +++---------- aws.go | 5 ++--- azure.go | 2 +- baidubce.go | 13 +++++-------- gccpay.go | 2 +- huawei.go | 28 ++++++++++++++-------------- huyi.go | 8 ++++---- infobip.go | 4 ++-- msg91.go | 4 ++-- netgsm.go | 12 ++++++++---- smsbao.go | 8 ++++---- submail.go | 4 ++-- tencent.go | 6 +++++- ucloud.go | 6 +++++- volcengine.go | 12 ++++++------ 15 files changed, 64 insertions(+), 63 deletions(-) diff --git a/aliyun.go b/aliyun.go index 7a6e83c..ccd399c 100644 --- a/aliyun.go +++ b/aliyun.go @@ -15,9 +15,9 @@ package go_sms_sender import ( - "bytes" "encoding/json" "fmt" + "strings" "github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi" ) @@ -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 diff --git a/aws.go b/aws.go index 15f93eb..05c56e0 100644 --- a/aws.go +++ b/aws.go @@ -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") } @@ -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") } @@ -78,7 +78,6 @@ func (a *AmazonSNSClient) SendMessage(param map[string]string, targetPhoneNumber PhoneNumber: &targetPhoneNumber[i], MessageAttributes: messageAttributes, }) - if err != nil { return err } diff --git a/azure.go b/azure.go index f1aa695..2f28a17 100644 --- a/azure.go +++ b/azure.go @@ -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") } diff --git a/baidubce.go b/baidubce.go index 7c8d765..66df989 100644 --- a/baidubce.go +++ b/baidubce.go @@ -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" @@ -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") } @@ -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, diff --git a/gccpay.go b/gccpay.go index b92dfc1..69c2031 100644 --- a/gccpay.go +++ b/gccpay.go @@ -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") } diff --git a/huawei.go b/huawei.go index b5470ac..ffb7378 100644 --- a/huawei.go +++ b/huawei.go @@ -20,7 +20,7 @@ import ( "crypto/tls" "encoding/base64" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -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 @@ -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 @@ -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() @@ -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 } diff --git a/huyi.go b/huyi.go index 9db099f..b9b0c42 100644 --- a/huyi.go +++ b/huyi.go @@ -18,7 +18,7 @@ import ( "crypto/md5" "encoding/hex" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strconv" @@ -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") } @@ -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) @@ -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 } diff --git a/infobip.go b/infobip.go index 6589ef8..c528f34 100644 --- a/infobip.go +++ b/infobip.go @@ -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") } @@ -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") } diff --git a/msg91.go b/msg91.go index cfd3d3d..a34e3bb 100644 --- a/msg91.go +++ b/msg91.go @@ -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") } @@ -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) } } diff --git a/netgsm.go b/netgsm.go index 6d2df62..a40e53b 100644 --- a/netgsm.go +++ b/netgsm.go @@ -19,7 +19,7 @@ import ( "encoding/xml" "errors" "fmt" - "io/ioutil" + "io" "net/http" ) @@ -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(`
@@ -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 } diff --git a/smsbao.go b/smsbao.go index 3e1068a..2d5f3b7 100644 --- a/smsbao.go +++ b/smsbao.go @@ -16,7 +16,7 @@ package go_sms_sender import ( "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -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] @@ -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") } @@ -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 } diff --git a/submail.go b/submail.go index 593c2f6..4ea7218 100644 --- a/submail.go +++ b/submail.go @@ -18,7 +18,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "mime/multipart" "net/http" "strings" @@ -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 } diff --git a/tencent.go b/tencent.go index 47d9a6c..a6f63e7 100644 --- a/tencent.go +++ b/tencent.go @@ -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") } @@ -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 { diff --git a/ucloud.go b/ucloud.go index 9248b4c..56d162a 100644 --- a/ucloud.go +++ b/ucloud.go @@ -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") } @@ -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) diff --git a/volcengine.go b/volcengine.go index 044920b..6c5138f 100644 --- a/volcengine.go +++ b/volcengine.go @@ -30,7 +30,7 @@ type VolcClient struct { } func GetVolcClient(accessId, accessKey, sign, templateId string, smsAccount []string) (*VolcClient, error) { - if len(smsAccount) < 1 { + if len(smsAccount) == 0 { return nil, fmt.Errorf("missing parameter: smsAccount") } @@ -49,15 +49,15 @@ func GetVolcClient(accessId, accessKey, sign, templateId string, smsAccount []st } func (c *VolcClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error { + if len(targetPhoneNumber) == 0 { + return fmt.Errorf("missing parameter: targetPhoneNumber") + } + requestParam, err := json.Marshal(param) if err != nil { return err } - if len(targetPhoneNumber) < 1 { - return fmt.Errorf("missing parameter: targetPhoneNumber") - } - req := &sms.SmsRequest{ SmsAccount: c.smsAccount, Sign: c.sign, @@ -71,7 +71,7 @@ func (c *VolcClient) SendMessage(param map[string]string, targetPhoneNumber ...s return fmt.Errorf("send message failed, error: %q", err.Error()) } if statusCode < 200 || statusCode > 299 { - return fmt.Errorf("send message failed, statusCode :%d", statusCode) + return fmt.Errorf("send message failed, statusCode: %d", statusCode) } if resp.ResponseMetadata.Error != nil { return fmt.Errorf("send message failed, code: %q, message: %q", resp.ResponseMetadata.Error.Code, resp.ResponseMetadata.Error.Message)