base-golang/tool/http.go

134 lines
4.0 KiB
Go
Raw Permalink Normal View History

2024-07-23 10:23:43 +08:00
package tool
import (
"encoding/json"
2024-07-31 16:49:14 +08:00
"gitea.bvbej.com/bvbej/base-golang/pkg/aes"
"gitea.bvbej.com/bvbej/base-golang/pkg/hmac"
"gitea.bvbej.com/bvbej/base-golang/pkg/httpclient"
"gitea.bvbej.com/bvbej/base-golang/pkg/time_parse"
2024-07-23 10:23:43 +08:00
"github.com/tidwall/gjson"
netUrl "net/url"
"strconv"
"time"
)
func HttpPostByAes(url, productID, key, vi string, params netUrl.Values) (body []byte, err error) {
var signatureDatetime = time_parse.CSTLayoutString()
unix, _ := time_parse.CSTLayoutStringToUnix(signatureDatetime)
params.Set("t", strconv.FormatInt(unix, 10))
var sign string
sign, err = aes.New(key, vi).EncryptCBC(params.Encode(), false)
if err != nil {
return body, err
}
body, err = httpclient.PostForm(
url,
params,
httpclient.WithTTL(time.Second*30),
httpclient.WithHeader("Signature", sign),
httpclient.WithHeader("Signature-Datetime", signatureDatetime),
httpclient.WithHeader("Product-ID", productID),
httpclient.WithOnFailedRetry(1, time.Millisecond*200,
func(body []byte) (shouldRetry bool) {
return len(body) == 0
},
),
)
return body, err
}
func HttpPostByAesWithTimeout(url, productID, key, vi string, params netUrl.Values, timeout time.Duration) (body []byte, err error) {
var signatureDatetime = time_parse.CSTLayoutString()
unix, _ := time_parse.CSTLayoutStringToUnix(signatureDatetime)
params.Set("t", strconv.FormatInt(unix, 10))
var sign string
sign, err = aes.New(key, vi).EncryptCBC(params.Encode(), false)
if err != nil {
return body, err
}
body, err = httpclient.PostForm(
url,
params,
httpclient.WithTTL(timeout),
httpclient.WithHeader("Signature", sign),
httpclient.WithHeader("Signature-Datetime", signatureDatetime),
httpclient.WithHeader("Product-ID", productID),
httpclient.WithOnFailedRetry(3, time.Second*1,
func(body []byte) (shouldRetry bool) {
return len(body) == 0
},
),
)
return body, err
}
func DingTalkAlertMarkdown(accessToken, secret, title, text string) bool {
params := netUrl.Values{}
timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10)
signData := timestamp + "\n" + secret
sign := netUrl.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
params.Set("access_token", accessToken)
params.Set("sign", sign)
params.Set("timestamp", timestamp)
var content struct {
MsgType string `json:"msgtype"`
Markdown struct {
Title string `json:"title"`
Text string `json:"text"`
} `json:"markdown"`
}
content.MsgType = "markdown"
content.Markdown.Title = title
content.Markdown.Text = text
body, _ := json.Marshal(content)
response, err := httpclient.PostJSON(
"https://oapi.dingtalk.com/robot/send?"+params.Encode(),
body,
httpclient.WithTTL(time.Second*5),
httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
httpclient.WithOnFailedRetry(3, time.Second*1,
func(body []byte) (shouldRetry bool) {
return len(body) == 0
},
),
)
if err != nil {
return false
}
return gjson.Get(string(response), "errcode").Int() == 0
}
func DingTalkAlertText(accessToken, secret string, text string) bool {
params := netUrl.Values{}
timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10)
signData := timestamp + "\n" + secret
sign := netUrl.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
params.Set("access_token", accessToken)
params.Set("sign", sign)
params.Set("timestamp", timestamp)
var content struct {
MsgType string `json:"msgtype"`
Text struct {
Content string `json:"content"`
} `json:"text"`
}
content.MsgType = "text"
content.Text.Content = text
body, _ := json.Marshal(content)
response, err := httpclient.PostJSON(
"https://oapi.dingtalk.com/robot/send?"+params.Encode(),
body,
httpclient.WithTTL(time.Second*5),
httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
httpclient.WithOnFailedRetry(3, time.Second*1,
func(body []byte) (shouldRetry bool) {
return len(body) == 0
},
),
)
if err != nil {
return false
}
return gjson.Get(string(response), "errcode").Int() == 0
}