From 6126eceb5b83b185fa02541ce8d2da03ff25a490 Mon Sep 17 00:00:00 2001 From: jiangyong27 Date: Tue, 25 Jun 2024 16:50:22 +0800 Subject: [PATCH] http --- push/getui.go | 8 +++--- push/http.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 push/http.go diff --git a/push/getui.go b/push/getui.go index 8c546ea..6afe2f3 100644 --- a/push/getui.go +++ b/push/getui.go @@ -94,7 +94,7 @@ func (g *Getui) Token() string { params["appkey"] = g.config.AppKey params["sign"] = util.Sha256(signStr) reqBody, _ := json.Marshal(params) - rspBody, err := util.HttpPostJson(reqUrl, nil, reqBody) + rspBody, err := HttpPostJson(reqUrl, nil, reqBody) if err != nil { log.Errorf("http post [%s] error :%s", string(reqBody), err.Error()) return "" @@ -170,7 +170,7 @@ func (g *Getui) Push(req *GetuiMessage) (*GetuiPushRsp, error) { return nil, err } - rspBody, err := util.HttpPostJson(reqUrl, map[string]string{"token": g.Token()}, reqBody) + rspBody, err := HttpPostJson(reqUrl, map[string]string{"token": g.Token()}, reqBody) if err != nil { log.Errorf("goutil http post error :%s", err.Error()) return nil, err @@ -194,7 +194,7 @@ func (g *Getui) Push(req *GetuiMessage) (*GetuiPushRsp, error) { func (g *Getui) GetUserStatus(cid string) (*GetuiStatus, error) { reqUrl := fmt.Sprintf("https://restapi.getui.com/v2/%s/user/status/%s", g.config.AppId, cid) - rspBody, err := util.HttpGet(reqUrl, map[string]string{ + rspBody, err := HttpGet(reqUrl, map[string]string{ "token": g.Token(), }) @@ -221,7 +221,7 @@ func (g *Getui) GetUserStatus(cid string) (*GetuiStatus, error) { func (g *Getui) GetUserDetail(cid string) (*GetuiDetail, error) { reqUrl := fmt.Sprintf("https://restapi.getui.com/v2/%s/user/detail/%s", g.config.AppId, cid) - rspBody, err := util.HttpGet(reqUrl, map[string]string{ + rspBody, err := HttpGet(reqUrl, map[string]string{ "token": g.Token(), }) diff --git a/push/http.go b/push/http.go new file mode 100644 index 0000000..2ab3810 --- /dev/null +++ b/push/http.go @@ -0,0 +1,78 @@ +package push + +import ( + "bytes" + "crypto/tls" + "fmt" + "io" + "net/http" + "time" +) + +// Get 请求 link:请求url +func HttpGet(link string, header map[string]string) ([]byte, error) { + client := &http.Client{Timeout: 20 * time.Second} + //忽略https的证书 + client.Transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + + req, err := http.NewRequest("GET", link, nil) + if err != nil { + return nil, err + } + if header != nil { + for k, v := range header { + req.Header.Add(k, v) + } + } + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { + return io.ReadAll(resp.Body) + } + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("%d:%s", resp.StatusCode, resp.Status) + } + return io.ReadAll(resp.Body) +} + +// PostJson 请求 +func HttpPostJson(link string, header map[string]string, json []byte) ([]byte, error) { + client := &http.Client{Timeout: 20 * time.Second} + //忽略https的证书 + client.Transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + + req, err := http.NewRequest("POST", link, bytes.NewBuffer(json)) + if err != nil { + return nil, err + } + if header != nil { + for k, v := range header { + req.Header.Add(k, v) + } + } + req.Header.Add("Content-Type", "application/json") + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized { + return io.ReadAll(resp.Body) + } + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("%d:%s", resp.StatusCode, resp.Status) + } + return io.ReadAll(resp.Body) +}