remove grequests import

This commit is contained in:
liuzhihang1
2025-03-07 08:50:21 +00:00
parent b83881ca32
commit 6c117f062d

View File

@@ -1,30 +1,50 @@
package logic package logic
import ( import (
"io"
"net/http" "net/http"
"strings" "strings"
"github.com/lzh-1625/go_process_manager/internal/app/repository" "github.com/lzh-1625/go_process_manager/internal/app/repository"
"github.com/lzh-1625/go_process_manager/log"
"github.com/levigross/grequests"
) )
type pushLogic struct{} type pushLogic struct {
httpClient *http.Client
}
var PushLogic = new(pushLogic) var PushLogic = &pushLogic{
httpClient: &http.Client{
Transport: http.DefaultTransport,
},
}
func (p *pushLogic) Push(ids []int, placeholders map[string]string) { func (p *pushLogic) Push(ids []int, placeholders map[string]string) {
pl := repository.PushRepository.GetPushConfigByIds(ids) pl := repository.PushRepository.GetPushConfigByIds(ids)
for _, v := range pl { for _, v := range pl {
if v.Enable { if v.Enable {
if v.Method == http.MethodGet { var resp *http.Response
grequests.Get(p.getReplaceMessage(placeholders, v.Url), nil) var reader io.Reader = nil
} var url string = v.Url
if v.Method == http.MethodPost { if v.Method == http.MethodPost {
grequests.Post(v.Url, &grequests.RequestOptions{ reader = strings.NewReader(p.getReplaceMessage(placeholders, v.Body))
JSON: p.getReplaceMessage(placeholders, v.Body),
})
} }
if v.Method == http.MethodGet {
url = p.getReplaceMessage(placeholders, url)
}
req, err := http.NewRequest(v.Method, url, reader)
if err != nil {
log.Logger.Warnw("推送失败", "err", err, "remark", v.Remark)
continue
}
req.Header.Add("content-type", "application/json")
resp, err = p.httpClient.Do(req)
if err != nil {
log.Logger.Warnw("推送失败", "err", err, "remark", v.Remark)
continue
}
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
} }
} }
} }