Skip to content

Commit

Permalink
Add features in bos and sms
Browse files Browse the repository at this point in the history
  • Loading branch information
duanliguo committed Jul 28, 2023
1 parent 7c8c6f3 commit 1358734
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bce/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// Constants and default values for the package bce
const (
SDK_VERSION = "0.9.153"
SDK_VERSION = "0.9.154"
URI_PREFIX = "/" // now support uri without prefix "v1" so just set root path
DEFAULT_DOMAIN = "baidubce.com"
DEFAULT_PROTOCOL = "http"
Expand Down
60 changes: 59 additions & 1 deletion doc/SMS.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,60 @@ ExpireSeconds | int | 签名字符串的有效期
> **提示:**
> - 详细参数配置及限制条件,可以参考SMS API 文档[变更配额和频控](https://cloud.baidu.com/doc/SMS/s/Yjwvxrwzb)
## 手机号黑名单
### 创建手机号黑名单
通过以下代码,可以创建手机号黑名单
```go
err := client.CreateMobileBlack(&api.CreateMobileBlackArgs{
Type: "MerchantBlack",
SmsType: "CommonNotice",
SignatureIdStr: "sddd",
Phone: "12345678901",
})
if err != nil {
fmt.Printf("CreateMobileBlack error, %s", err)
return
}
fmt.Printf("CreateMobileBlack success")
```
### 查询手机号黑名单
通过以下代码,可以查询手机号黑名单
```go
err := client.GetMobileBlack(&api.GetMobileBlackArgs{
SmsType: "CommonNotice",
SignatureIdStr: "sddd",
Phone: "12345678901",
StartTime: "2023-07-18",
EndTime: "2023-07-19",
PageNo: "1",
PageSize: "10",
})
if err != nil {
fmt.Printf("GetMobileBlack error, %s", err)
return
}
fmt.Printf("GetMobileBlack success")
```
### 删除手机号黑名单
通过以下代码,可以删除手机号黑名单
```go
err := client.DeleteMobileBlack(&api.DeleteMobileBlackArgs{
Phones: "12345678901",
})
if err != nil {
fmt.Printf("DeleteMobileBlack error, %s", err)
return
}
fmt.Printf("DeleteMobileBlack success")
```
> **提示:**
> - 详细参数配置及限制条件,可以参考SMS API 文档[手机号黑名单](https://cloud.baidu.com/doc/SMS/s/Yjwvxrwzb)
# 错误处理
GO语言以error类型标识错误,SMS支持两种错误见下表:
Expand Down Expand Up @@ -447,4 +501,8 @@ if err != nil {
首次发布:
- 支持短信发送接口
- 支持签名、模板管理接口
- 支持配额、频控查看和变更
- 支持配额、频控查看和变更
## v0.9.32 [2023-07-20]
- 配额频控查询增加申请信息字段
- 增加手机号黑名单增、删、查接口
63 changes: 60 additions & 3 deletions services/bos/api/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ func HeadBucket(cli bce.Client, bucket string) (error, *bce.BceResponse) {
return nil, resp
}


// PutBucket - create a new bucket with the given name
//
// PARAMS:
Expand Down Expand Up @@ -1131,7 +1130,7 @@ func GetBucketMirror(cli bce.Client, bucket string) (*PutBucketMirrorArgs, error
if err := resp.ParseJsonBody(result); err != nil {
return nil, err
}
return result, nil
return result, nil
}

func DeleteBucketMirror(cli bce.Client, bucket string) error {
Expand All @@ -1148,4 +1147,62 @@ func DeleteBucketMirror(cli bce.Client, bucket string) error {
}
defer func() { resp.Body().Close() }()
return nil
}
}


func PutBucketTag(cli bce.Client, bucket string, putBucketTagReq PutBucketTagReq) error {
req := &bce.BceRequest{}
req.SetUri(getBucketUri(bucket))
req.SetMethod(http.PUT)
req.SetParam("tagging", "")
reqByte, _ := json.Marshal(putBucketTagReq)
body, err := bce.NewBodyFromString(string(reqByte))
if err != nil {
return err
}
req.SetBody(body)
resp := &bce.BceResponse{}
if err := SendRequest(cli, req, resp); err != nil {
return err
}
if resp.IsFail() {
return resp.ServiceError()
}
defer func() { resp.Body().Close() }()
return nil
}

func GetBucketTag(cli bce.Client, bucket string) (*PutBucketTagReq, error) {
req := &bce.BceRequest{}
req.SetUri(getBucketUri(bucket))
req.SetMethod(http.GET)
req.SetParam("tagging", "")
resp := &bce.BceResponse{}
if err := SendRequest(cli, req, resp); err != nil {
return nil, err
}
if resp.IsFail() {
return nil, resp.ServiceError()
}
result := &PutBucketTagReq{}
if err := resp.ParseJsonBody(result); err != nil {
return nil, err
}
return result, nil
}

func DeleteBucketTag(cli bce.Client, bucket string) error {
req := &bce.BceRequest{}
req.SetUri(getBucketUri(bucket))
req.SetMethod(http.DELETE)
req.SetParam("tagging", "")
resp := &bce.BceResponse{}
if err := SendRequest(cli, req, resp); err != nil {
return err
}
if resp.IsFail() {
return resp.ServiceError()
}
defer func() { resp.Body().Close() }()
return nil
}
9 changes: 9 additions & 0 deletions services/bos/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,12 @@ type PutBucketMirrorArgs struct {
BucketMirroringConfiguration []MirrorConfigurationRule `json:"bucketMirroringConfiguration"`
}


type PutBucketTagReq struct {
Tags []Tag `json:"tags"`
}

type Tag struct {
TagKey string `json:"tagKey"`
TagValue string `json:"tagValue"`
}
9 changes: 7 additions & 2 deletions services/bos/api/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ func CopyObject(cli bce.Client, bucket, object, source string,
}

if validCannedAcl(args.CannedAcl) {
req.SetHeader(http.BCE_ACL, args.CannedAcl)
}
req.SetHeader(http.BCE_ACL, args.CannedAcl)
}

if err := setUserMetadata(req, args.UserMeta); err != nil {
return nil, err
Expand Down Expand Up @@ -674,6 +674,11 @@ func DeleteMultipleObjects(cli bce.Client, bucket string,
return nil, resp.ServiceError()
}
jsonBody := &DeleteMultipleObjectsResult{}

if resp.Header(http.CONTENT_LENGTH) == "0" {
resp.Body().Close()
return jsonBody, nil
}
if err := resp.ParseJsonBody(jsonBody); err != nil {
return nil, err
}
Expand Down
154 changes: 154 additions & 0 deletions services/sms/api/mobile_black.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright 2023 Baidu, Inc.
*
* 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.
*/

// mobile_black.go - the sms MobileBlack APIs definition supported by the SMS service

package api

import (
"encoding/json"

"github.com/baidubce/bce-sdk-go/bce"
"github.com/baidubce/bce-sdk-go/http"
)

// CreateMobileBlack - create an sms MobileBlack
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - args: the arguments to create an sms mobileBlack
// RETURNS:
// - error: the return error if any occurs
func CreateMobileBlack(cli bce.Client, args *CreateMobileBlackArgs) error {
if err := CheckError(args != nil, "CreateMobileBlackArgs can not be nil"); err != nil {
return err
}
if err := CheckError(len(args.Type) > 0, "type can not be blank"); err != nil {
return err
}
if err := CheckError(len(args.Phone) > 0, "phone can not be blank"); err != nil {
return err
}
if args.Type == "SignatureBlack" {
if err := CheckError(len(args.SmsType) > 0,
"smsType can not be blank, when 'type' is 'SignatureBlack'."); err != nil {
return err
}
if err := CheckError(len(args.SignatureIdStr) > 0,
"signatureIdStr can not be blank, when 'type' is 'SignatureBlack'."); err != nil {
return err
}
}

req := &bce.BceRequest{}
req.SetUri(REQUEST_URI_BLACK)
req.SetMethod(http.POST)
req.SetHeader(http.CONTENT_TYPE, bce.DEFAULT_CONTENT_TYPE)

jsonBytes, jsonErr := json.Marshal(args)
if jsonErr != nil {
return jsonErr
}
body, err := bce.NewBodyFromBytes(jsonBytes)
if err != nil {
return err
}
req.SetBody(body)
resp := &bce.BceResponse{}
if err := cli.SendRequest(req, resp); err != nil {
return err
}
if resp.IsFail() {
return resp.ServiceError()
}
return nil
}

// DeleteMobileBlack - delete sms mobileBlack by phones
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - args: the arguments to delete sms mobileBlack
// RETURNS:
// - error: the return error if any occurs
func DeleteMobileBlack(cli bce.Client, args *DeleteMobileBlackArgs) error {
if err := CheckError(args != nil, "DeleteMobileBlackArgs can not be nil"); err != nil {
return err
}
if err := CheckError(len(args.Phones) > 0, "Phones can not be blank"); err != nil {
return err
}
return bce.NewRequestBuilder(cli).
WithMethod(http.DELETE).
WithURL(REQUEST_URI_BLACK+"/delete").
WithQueryParam("phones", args.Phones).
Do()
}

// GetMobileBlack - get sms mobileBlackList
//
// PARAMS:
// - cli: the client agent which can perform sending request
// - args: the arguments to get sms mobileBlackList
// RETURNS:
// - error: the return error if any occurs
// - *api.GetMobileBlackResult: the result of get sms MobileBlackList
func GetMobileBlack(cli bce.Client, args *GetMobileBlackArgs) (*GetMobileBlackResult, error) {
if err := CheckError(args != nil, "GetMobileBlackArgs can not be nil"); err != nil {
return nil, err
}

paramsMap := make(map[string]string)
if len(args.Phone) > 0 {
paramsMap["phone"] = args.Phone
}
if len(args.SmsType) > 0 {
paramsMap["smsType"] = args.SmsType
}
if len(args.SignatureIdStr) > 0 {
paramsMap["signatureIdStr"] = args.SignatureIdStr
}
if len(args.StartTime) > 0 {
paramsMap["startTime"] = args.StartTime
}
if len(args.EndTime) > 0 {
paramsMap["endTime"] = args.EndTime
}
if len(args.PageNo) > 0 {
paramsMap["pageNo"] = args.PageNo
}
if len(args.PageSize) > 0 {
paramsMap["pageSize"] = args.PageSize
}

req := &bce.BceRequest{}
req.SetUri(REQUEST_URI_BLACK)
req.SetMethod(http.GET)
req.SetHeader(http.CONTENT_TYPE, bce.DEFAULT_CONTENT_TYPE)
req.SetParams(paramsMap)

resp := &bce.BceResponse{}
if err := cli.SendRequest(req, resp); err != nil {
return nil, err
}
if resp.IsFail() {
return nil, resp.ServiceError()
}

result := &GetMobileBlackResult{}
if err := resp.ParseJsonBody(result); err != nil {
return nil, err
}
return result, nil
}
Loading

0 comments on commit 1358734

Please sign in to comment.