| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package acceleronix
- import (
- "crypto/hmac"
- "crypto/sha256"
- "encoding/hex"
- "encoding/json"
- "fwh-toolbox-server/utils/httpClient"
- "net/http"
- "strconv"
- "time"
- "go.uber.org/zap"
- )
- // API文档地址:https://cmp.acceleronix.io/document/home
- const (
- mainUrl = "https://cmp.acceleronix.io/gateway/openapi"
- appKey = "0cE57qK03qsA9158"
- appSecret = "6051y5YS"
- timeout time.Duration = 30 * time.Second // SmartHub请求超时30s
- )
- var acceleronixHttpClient = httpClient.HttpClient{}
- func makeSignature(timestamp, requestBody string) string {
- message := appKey + timestamp + requestBody
- // 创建 HMAC-SHA256
- h := hmac.New(sha256.New, []byte(appSecret))
- // 写入数据
- h.Write([]byte(message))
- // 计算摘要
- signature := h.Sum(nil)
- // hex 编码
- return hex.EncodeToString(signature)
- }
- func bodyToStableString(body any) (string, error) {
- if body == nil {
- return "", nil
- }
- switch v := body.(type) {
- case string:
- return v, nil
- case []byte:
- return string(v), nil
- default:
- b, err := json.Marshal(v)
- if err != nil {
- return "", err
- }
- return string(b), nil
- }
- }
- func acceleronixHttpPost(url string, body any) (*httpClient.Response, error) {
- t := timeout
- timestamp := strconv.FormatInt(time.Now().Unix(), 10)
- bodyStr, err := bodyToStableString(body)
- if err != nil {
- zap.L().Error("body marshal failed.", zap.Any("body", body), zap.String("err", err.Error()))
- return nil, err
- }
- opt := httpClient.HttpClientOptions{
- Timeout: &t,
- Header: &http.Header{
- "APP-Key": {appKey},
- "Signature": {makeSignature(timestamp, bodyStr)},
- "Timestamp": {timestamp},
- "Accept": {"application/json", "text/plain", "*/*"},
- },
- }
- return acceleronixHttpClient.HttpPost(mainUrl+url, body, &opt)
- }
|