Skip to content

Commit

Permalink
rollback mns
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Aug 19, 2024
1 parent 93ee11a commit 399a72b
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 0 deletions.
74 changes: 74 additions & 0 deletions services/dybaseapi/mns/batch_delete_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package mns

//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.

import (
"encoding/xml"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
)


func (client *Queue) BatchDeleteMessage(request *BatchDeleteMessageRequest) (response *BatchDeleteMessageResponse, err error) {
response = CreateBatchDeleteMessageResponse()
err = client.DoActionWithSigner(request, response)
return
}

type BatchDeleteMessageRequest struct {
*requests.RoaRequest
QueueName string `position:"Path" name:"QueueName"`
}

type BatchDeleteMessageResponse struct {
*responses.BaseResponse
RequestId string `json:"RequestId" xml:"RequestId"`
Code string `json:"Code" xml:"Code"`
}

type ReceiptHandles struct {
XMLName xml.Name `xml:"ReceiptHandles"`
Xmlns string `xml:"xmlns,attr"`
Handles []string `xml:"ReceiptHandle"`
}

func (request *BatchDeleteMessageRequest) SetReceiptHandles(receiptHandles []string) {
receiptHandlesObj := &ReceiptHandles{
Xmlns: "http://mns.aliyuncs.com/doc/v1/",
Handles: receiptHandles,
}
content, err := xml.Marshal(receiptHandlesObj)
if err != nil {
panic(err)
}
request.SetContent([]byte(xml.Header + string(content)))
}

func CreateBatchDeleteMessageRequest() (request *BatchDeleteMessageRequest) {
request = &BatchDeleteMessageRequest{
RoaRequest: &requests.RoaRequest{
},
}
request.InitWithApiInfo("MNS", "2015-06-06","BatchDeleteMessage","/queues/[QueueName]/messages", "", "")
request.Method = "DELETE"
request.Headers["x-mns-version"] = "2015-06-06"
request.AcceptFormat = "XML"
return
}

func CreateBatchDeleteMessageResponse() (response *BatchDeleteMessageResponse) {
response = &BatchDeleteMessageResponse{
BaseResponse: &responses.BaseResponse{},
}
return
}
57 changes: 57 additions & 0 deletions services/dybaseapi/mns/batch_receive_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package mns

//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.

import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
)


func (client *Queue) BatchReceiveMessage(request *BatchReceiveMessageRequest) (response *BatchReceiveMessageResponse, err error) {
response = CreateBatchReceiveMessageResponse()
err = client.DoActionWithSigner(request, response)
return
}

type BatchReceiveMessageRequest struct {
*requests.RoaRequest
QueueName string `position:"Path" name:"QueueName"`
NumOfMessages requests.Integer `position:"Query" name:"numOfMessages"`
WaitSeconds requests.Integer `position:"Query" name:"waitseconds"`
}

type BatchReceiveMessageResponse struct {
*responses.BaseResponse
RequestId string `json:"RequestId" xml:"RequestId"`
Code string `json:"Code" xml:"Code"`
Message []Message `json:"Message" xml:"Message"`
}

func CreateBatchReceiveMessageRequest() (request *BatchReceiveMessageRequest) {
request = &BatchReceiveMessageRequest{
RoaRequest: &requests.RoaRequest{},
}
request.InitWithApiInfo("MNS", "2015-06-06","BatchReceiveMessage","/queues/[QueueName]/messages", "", "")
request.Method = "GET"
request.Headers["x-mns-version"] = "2015-06-06"
request.AcceptFormat = "XML"
return
}

func CreateBatchReceiveMessageResponse() (response *BatchReceiveMessageResponse) {
response = &BatchReceiveMessageResponse{
BaseResponse: &responses.BaseResponse{},
}
return
}
110 changes: 110 additions & 0 deletions services/dybaseapi/mns/mns_roa_signature_composer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package mns

import (
"bytes"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
"sort"
"strings"
)

/*
* 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.
*/


func signMnsRoaRequest(request requests.AcsRequest, signer auth.Signer) (err error) {
completeROASignParams(request, signer)
stringToSign := buildRoaStringToSign(request)
request.SetStringToSign(stringToSign)
signature := signer.Sign(stringToSign, "")
accessKeyId, err := signer.GetAccessKeyId()
if err != nil {
return nil
}

request.GetHeaders()["Authorization"] = "MNS " + accessKeyId + ":" + signature

return
}

func completeROASignParams(request requests.AcsRequest, signer auth.Signer) {
headerParams := request.GetHeaders()

// complete query params
queryParams := request.GetQueryParams()
if extraParam := signer.GetExtraParam(); extraParam != nil {
for key, value := range extraParam {
if key == "SecurityToken" {
headerParams["security-token"] = value
continue
}

queryParams[key] = value
}
}

// complete header params
headerParams["Date"] = utils.GetTimeInFormatRFC2616()
if request.GetFormParams() != nil && len(request.GetFormParams()) > 0 {
formString := utils.GetUrlFormedMap(request.GetFormParams())
request.SetContent([]byte(formString))
headerParams["Content-Type"] = requests.Form
}
contentMD5 := utils.GetMD5Base64(request.GetContent())
headerParams["Content-MD5"] = contentMD5
if _, contains := headerParams["Content-Type"]; !contains {
headerParams["Content-Type"] = "text/xml"
}
}

func buildRoaStringToSign(request requests.AcsRequest) (stringToSign string) {

headers := request.GetHeaders()

stringToSignBuilder := bytes.Buffer{}
stringToSignBuilder.WriteString(request.GetMethod())
stringToSignBuilder.WriteString(requests.HeaderSeparator)

// append header keys for sign
appendIfContain(headers, &stringToSignBuilder, "Content-MD5", requests.HeaderSeparator)
appendIfContain(headers, &stringToSignBuilder, "Content-Type", requests.HeaderSeparator)
appendIfContain(headers, &stringToSignBuilder, "Date", requests.HeaderSeparator)

// sort and append headers witch starts with 'x-mns-'
var acsHeaders []string
for key := range headers {
if strings.HasPrefix(key, "x-mns-") {
acsHeaders = append(acsHeaders, key)
}
}
sort.Strings(acsHeaders)
for _, key := range acsHeaders {
stringToSignBuilder.WriteString(key + ":" + headers[key])
stringToSignBuilder.WriteString(requests.HeaderSeparator)
}

// append query params
stringToSignBuilder.WriteString(request.BuildQueries())
stringToSign = stringToSignBuilder.String()
return
}

func appendIfContain(sourceMap map[string]string, target *bytes.Buffer, key, separator string) {
if value, contain := sourceMap[key]; contain && len(value) > 0 {
target.WriteString(sourceMap[key])
target.WriteString(separator)
}
}

Loading

0 comments on commit 399a72b

Please sign in to comment.