package mq import ( "encoding/json" "fmt" "git.oa00.com/go/jcook-sdk/util/http" "git.oa00.com/go/jcook-sdk/util/security" "net/url" "time" ) const maxSize = "20" const autoAck = "false" const ( PTopic = "topic" PConsumerGroupID = "consumerGroupId" PSize = "size" PAck = "ack" PAccessKey = "accessKey" PDateTime = "dateTime" PSignature = "signature" PToken = "token" PAckAction = "ackAction" PAckIndex = "ackIndex" ) type Client struct { AccessKey string SecretKey string } func NewClient(ak string, sk string) *Client { return &Client{ AccessKey: ak, SecretKey: sk, } } type UrlBase interface { GetTopicName() string GetConsumerGroupID() string GetEndPoint() string } func getUTCStr() string { return time.Now().UTC().Format("2006-01-02T15:04:05Z") } func (c *Client) Pull(r UrlBase) ([]byte, error) { mq := fmt.Sprintf("%s/%s/messages", http.FormatUrl(r.GetEndPoint(), true), "v2") value := url.Values{} data := map[string]string{ PTopic: r.GetTopicName(), PConsumerGroupID: r.GetConsumerGroupID(), PSize: maxSize, PAck: autoAck, } for k, v := range data { value.Set(k, v) } t := getUTCStr() data[PAccessKey] = c.AccessKey data[PDateTime] = t sign := security.Sha1OrderlyWithBase64(c.SecretKey, data) header := map[string]string{ PAccessKey: c.AccessKey, PDateTime: t, PSignature: sign, } return http.JGet(mq, value, header) } func (c *Client) Ack(r UrlBase, index string, action string) ([]byte, error) { ack := fmt.Sprintf("%s/%s/ack", http.FormatUrl(r.GetEndPoint(), true), "v2") t := getUTCStr() data := map[string]string{ PTopic: r.GetTopicName(), PAccessKey: c.AccessKey, PAckAction: action, PAckIndex: index, PConsumerGroupID: r.GetConsumerGroupID(), PDateTime: t, } sign := security.Sha1OrderlyWithBase64(c.SecretKey, data) header := map[string]string{ PAccessKey: c.AccessKey, PDateTime: t, PSignature: sign, } jsonStr, _ := json.Marshal(&data) return http.JPostJson(ack, jsonStr, header) }