|
@@ -0,0 +1,131 @@
|
|
|
+package httpclient
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "io/ioutil"
|
|
|
+ "net"
|
|
|
+ "net/http"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "gitee.com/xuyiping_admin/pkg/logger/zaplog"
|
|
|
+ "gitee.com/xuyiping_admin/pkg/xerr"
|
|
|
+ "go.uber.org/zap"
|
|
|
+)
|
|
|
+
|
|
|
+type Service struct {
|
|
|
+ authClient *http.Client
|
|
|
+}
|
|
|
+
|
|
|
+func NewClientService() *Service {
|
|
|
+ return &Service{
|
|
|
+ authClient: &http.Client{
|
|
|
+ Timeout: time.Duration(5) * time.Second,
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+type Header struct {
|
|
|
+ Key string `json:"key"`
|
|
|
+ Value string `json:"value"`
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Service) doRequest(req *http.Request) ([]byte, error) {
|
|
|
+ resp, err := c.authClient.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ var nErr net.Error
|
|
|
+ if errors.As(err, &nErr) && nErr.Timeout() {
|
|
|
+ for i := 0; i < 3; i++ {
|
|
|
+ time.Sleep(15 * time.Second)
|
|
|
+ resp, err = c.authClient.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ zaplog.Error("ClientService", zap.Any("for", i), zap.Any("authClient.Do", err))
|
|
|
+ if i == 3 {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ zaplog.Error("ClientService", zap.Any("authClient.Do", err))
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ defer resp.Body.Close()
|
|
|
+ b, err := ioutil.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ zaplog.Error("ClientService", zap.Any("ioutil.ReadAll", err))
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ if resp.StatusCode != http.StatusOK {
|
|
|
+ if len(b) > 0 {
|
|
|
+ return nil, xerr.Customf("err:%v,body:%s", err, string(b))
|
|
|
+ } else {
|
|
|
+ return nil, xerr.Customf("err:%v", err)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return b, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Service) DoGet(url string, headers []*Header) ([]byte, error) {
|
|
|
+ req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
|
+ if err != nil {
|
|
|
+ zaplog.Error("ClientService", zap.Any("DoGet", err))
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ req.Header.Add("Accept", "application/json")
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+ if headers != nil {
|
|
|
+ for _, v := range headers {
|
|
|
+ req.Header.Add(v.Key, v.Value)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return c.doRequest(req)
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Service) DoPut(url string, body interface{}, headers []*Header) ([]byte, error) {
|
|
|
+ b, err := json.Marshal(body)
|
|
|
+ if err != nil {
|
|
|
+ zaplog.Error("ClientService", zap.Any("DoPost-Marshal", err))
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ req, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(b))
|
|
|
+ if err != nil {
|
|
|
+ zaplog.Error("ClientService", zap.Any("DoGet", err))
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ req.Header.Add("Accept", "application/json")
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+ if headers != nil {
|
|
|
+ for _, v := range headers {
|
|
|
+ req.Header.Add(v.Key, v.Value)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return c.doRequest(req)
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Service) DoPost(url string, body interface{}, headers []*Header) ([]byte, error) {
|
|
|
+ b, err := json.Marshal(body)
|
|
|
+ if err != nil {
|
|
|
+ zaplog.Error("ClientService", zap.Any("DoPost-Marshal", err))
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
|
|
|
+ if err != nil {
|
|
|
+ zaplog.Error("ClientService", zap.Any("NewRequest", err))
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
+ req.Header.Add("Accept", "application/json")
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+ if headers != nil {
|
|
|
+ for _, v := range headers {
|
|
|
+ req.Header.Add(v.Key, v.Value)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return c.doRequest(req)
|
|
|
+}
|