115 lines
3.2 KiB
Go
115 lines
3.2 KiB
Go
package jcqapi
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha1"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi"
|
|
"git.rosy.net.cn/baseapi/platformapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
)
|
|
|
|
const (
|
|
sigKey = "signature"
|
|
|
|
httpURL = "http://jcq-hb-yd-001-httpsrv-nlb-FI.jvessel-open-hb.jdcloud.com:8080"
|
|
|
|
TopicCreateOrder = "open_message_pop_order_create_E1D746D42474D5F1F1A10CECE75D99F6"
|
|
ConsumerGroupIdCreateOrder = "open_message_573819178445"
|
|
)
|
|
|
|
type API struct {
|
|
platformapi.APICookie
|
|
|
|
accessKey string
|
|
secretKey string
|
|
client *http.Client
|
|
config *platformapi.APIConfig
|
|
}
|
|
|
|
func New(accessKey, secretKey string, config ...*platformapi.APIConfig) *API {
|
|
curConfig := platformapi.DefAPIConfig
|
|
if len(config) > 0 {
|
|
curConfig = *config[0]
|
|
}
|
|
return &API{
|
|
accessKey: accessKey,
|
|
secretKey: secretKey,
|
|
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
|
config: &curConfig,
|
|
}
|
|
}
|
|
|
|
func (a *API) signParam(params map[string]interface{}) (sig string) {
|
|
var valueList []string
|
|
for k, v := range params {
|
|
if k != sigKey {
|
|
if str := fmt.Sprint(v); str != "" {
|
|
valueList = append(valueList, fmt.Sprintf("%s=%s", k, str))
|
|
}
|
|
}
|
|
}
|
|
sort.Sort(sort.StringSlice(valueList))
|
|
sig = strings.Join(valueList, "&")
|
|
key := []byte(a.secretKey)
|
|
mac := hmac.New(sha1.New, key)
|
|
mac.Write([]byte(sig))
|
|
sEnc := base64.StdEncoding.EncodeToString(mac.Sum(nil))
|
|
return sEnc
|
|
}
|
|
|
|
func (a *API) AccessAPI(action string, url string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
|
params := make(map[string]interface{})
|
|
params["accessKey"] = a.accessKey
|
|
params["dateTime"] = time.Now().UTC().Format("2006-01-02T15:04:05Z")
|
|
params = utils.MergeMaps(params, bizParams)
|
|
signStr := a.signParam(params)
|
|
params["signature"] = signStr
|
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
|
func() *http.Request {
|
|
request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, params), nil)
|
|
return request
|
|
},
|
|
a.config,
|
|
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
|
if jsonResult1 == nil {
|
|
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
|
}
|
|
if err == nil {
|
|
if jsonResult1["error"] != nil {
|
|
errLevel = platformapi.ErrLevelGeneralFail
|
|
err = utils.NewErrorCode(jsonResult1["error"].(map[string]interface{})["message"].(string), jsonResult1["error"].(map[string]interface{})["code"].(string))
|
|
baseapi.SugarLogger.Debugf("jdeclp AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
|
}
|
|
retVal = jsonResult1
|
|
}
|
|
return errLevel, err
|
|
})
|
|
return retVal, err
|
|
}
|
|
|
|
type ConsumeInfoResult struct {
|
|
RequestID string `json:"requestId"`
|
|
Result string `json:"result"`
|
|
}
|
|
|
|
//消费信息
|
|
//https://docs.jdcloud.com/cn/message-queue/consume-message
|
|
func (a *API) ConsumeInfo(topic, consumerGroupId string) (consumeInfoResult *ConsumeInfoResult, err error) {
|
|
result, err := a.AccessAPI("v1/messages", httpURL, map[string]interface{}{
|
|
"topic": topic,
|
|
"consumerGroupId": consumerGroupId,
|
|
"size": 1,
|
|
})
|
|
if err == nil {
|
|
utils.Map2StructByJson(result, &consumeInfoResult, false)
|
|
}
|
|
return consumeInfoResult, err
|
|
}
|