From 9fc8a7e2b6b49a16e9227cc9cd0f0029abbf1ff1 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Tue, 28 May 2024 18:03:32 +0800 Subject: [PATCH] feat: remove useless code --- README.md | 1 - basic.go | 3 - sendcloud.go | 189 --------------------------------------------------- 3 files changed, 193 deletions(-) delete mode 100644 sendcloud.go diff --git a/README.md b/README.md index 472060e..9134aaf 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,6 @@ We support the following SMS providers, welcome to contribute. - [Azure ACS](https://azure.microsoft.com/en-us/products/communication-services) - [GCCPAY](https://gccpay.com/) - [Infobip](https://www.infobip.com/) -- [SendCloud](https://www.sendcloud.net/) - [SUBMAIL](https://en.mysubmail.com/) - [SmsBao](https://www.smsbao.com/) - [Alibaba Cloud](https://www.aliyun.com/product/sms) diff --git a/basic.go b/basic.go index bb3b511..f077af9 100644 --- a/basic.go +++ b/basic.go @@ -23,7 +23,6 @@ const ( Msg91 = "Msg91 SMS" GCCPAY = "GCCPAY SMS" Infobip = "Infobip SMS" - SendCloud = "SendCloud SMS" SUBMAIL = "SUBMAIL SMS" SmsBao = "SmsBao SMS" Aliyun = "Aliyun SMS" @@ -57,8 +56,6 @@ func NewSmsClient(provider string, accessId string, accessKey string, sign strin return GetGCCPAYClient(accessId, accessKey, template) case Infobip: return GetInfobipClient(accessId, accessKey, template, other) - case SendCloud: - return GetSendCloudClient(accessId, accessKey, template) case SUBMAIL: return GetSubmailClient(accessId, accessKey, template) case SmsBao: diff --git a/sendcloud.go b/sendcloud.go deleted file mode 100644 index 8d064b6..0000000 --- a/sendcloud.go +++ /dev/null @@ -1,189 +0,0 @@ -// Copyright 2024 The Casdoor Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package go_sms_sender - -import ( - "crypto/sha256" - "encoding/hex" - "encoding/json" - "errors" - "fmt" - "io" - "net/http" - "net/url" - "strconv" - "strings" - "time" -) - -// SendCloudResponseData ResponseData structure holds the response from SendCloud API. -type SendCloudResponseData struct { - Result bool `json:"result"` - StatusCode int `json:"statusCode"` - Message string `json:"message"` - Info interface{} `json:"info"` -} - -// SendCloudConfig Config holds the configuration for the SMS sending service. -type SendCloudConfig struct { - CharSet string - Server string - SendSMSAPI string - MaxReceivers int -} - -type SendCloudClient struct { - SmsUser string - SmsKey string - TemplateId int - MsgType int - Config SendCloudConfig -} - -func GetSendCloudClient(smsUser string, smsKey string, template string) (*SendCloudClient, error) { - templateId, err := strconv.Atoi(template) - if err != nil { - return nil, fmt.Errorf("template id should be number") - } - - msgType, err := strconv.Atoi("0") - if err != nil { - return nil, fmt.Errorf("msgType id should be number") - } - - return &SendCloudClient{ - SmsUser: smsUser, - SmsKey: smsKey, - TemplateId: templateId, - MsgType: msgType, - Config: SendCloudConfig{ - CharSet: "utf-8", - Server: "https://api.sendcloud.net", - SendSMSAPI: "/smsapi/send", - MaxReceivers: 100, - }, - }, nil -} - -// SendMessage sends an SMS using SendCloud API. -func (client *SendCloudClient) SendMessage(param map[string]string, targetPhoneNumber ...string) error { - if err := client.validateSendCloudSms(targetPhoneNumber); err != nil { - return fmt.Errorf("failed to send message: %w", err) - } - if err := validateConfig(client.Config); err != nil { - return fmt.Errorf("failed to send message: %w", err) - } - - params, err := client.prepareParams(param, targetPhoneNumber) - if err != nil { - return fmt.Errorf("failed to send message: %w", err) - } - - signature := calculateSignature(params, client.SmsKey) - params.Set("signature", signature) - - resp, err := http.PostForm(client.Config.Server+client.Config.SendSMSAPI, params) - if err != nil { - return fmt.Errorf("failed to send HTTP POST request: %w", err) - } - defer func() { - err := resp.Body.Close() - if err != nil { - fmt.Println(err) - return - } - }() - - if resp.StatusCode != http.StatusOK { - return fmt.Errorf("HTTP POST request failed with status code %d", resp.StatusCode) - } - - body, err := io.ReadAll(resp.Body) - if err != nil { - return fmt.Errorf("failed to read response body: %w", err) - } - - var responseData SendCloudResponseData - if err := json.Unmarshal(body, &responseData); err != nil { - return fmt.Errorf("failed to unmarshal response body: %w", err) - } - - if !responseData.Result { - return fmt.Errorf("SMS sending failed: %s", responseData.Message) - } - - return nil -} - -// prepareParams prepares parameters for sending SMS. -func (client *SendCloudClient) prepareParams(vars map[string]string, targetPhoneNumbers []string) (url.Values, error) { - params := url.Values{} - params.Set("smsUser", client.SmsUser) - params.Set("msgType", strconv.Itoa(client.MsgType)) - params.Set("phone", strings.Join(targetPhoneNumbers, ",")) - params.Set("templateId", strconv.Itoa(client.TemplateId)) - params.Set("timestamp", strconv.FormatInt(time.Now().UnixNano()/int64(time.Millisecond), 10)) - - if len(vars) > 0 { - varsJSON, err := json.Marshal(vars) - if err != nil { - return nil, fmt.Errorf("failed to marshal vars: %v", err) - } - params.Set("vars", string(varsJSON)) - } - - return params, nil -} - -// validateConfig validates the SMS sending configuration. -func validateConfig(config SendCloudConfig) error { - switch { - case config.CharSet == "": - return errors.New("charSet cannot be empty") - case config.Server == "": - return errors.New("server cannot be empty") - case config.SendSMSAPI == "": - return errors.New("sendSMSAPI cannot be empty") - case config.MaxReceivers <= 0: - return errors.New("maxReceivers must be greater than zero") - } - - return nil -} - -// validateSendCloudSms validates the SendCloudSms data. -func (client *SendCloudClient) validateSendCloudSms(targetPhoneNumbers []string) error { - switch { - case client.TemplateId == 0: - return errors.New("templateId cannot be zero") - case client.MsgType < 0: - return errors.New("msgType cannot be negative") - case len(targetPhoneNumbers) == 0: - return errors.New("phone cannot be empty") - } - return nil -} - -// calculateSignature calculates the signature for the request. -func calculateSignature(params url.Values, key string) string { - //fmt.Sprintf("%s&msgType=%s&phone=%s&smsUser=%s&templateId=%s×tamp=%s&vars=%s") - signStr := fmt.Sprintf("%s&msgType=%s&phone=%s&smsUser=%s&templateId=%s×tamp=%s&vars=%s&%s", - key, params.Get("msgType"), params.Get("phone"), - params.Get("smsUser"), params.Get("templateId"), - params.Get("timestamp"), params.Get("vars"), key) - hasher := sha256.New() - hasher.Write([]byte(signStr)) - return hex.EncodeToString(hasher.Sum(nil)) -}