Explorar o código

service: add httpclient

Yi hai 4 días
pai
achega
e803e818af
Modificáronse 3 ficheiros con 153 adicións e 0 borrados
  1. 2 0
      dep/dep.go
  2. 131 0
      service/httpclient/http.go
  3. 20 0
      service/httpclient/interface.go

+ 2 - 0
dep/dep.go

@@ -7,6 +7,7 @@ import (
 	"kpt-pasture/module/crontab"
 	moduleMqtt "kpt-pasture/module/mqtt"
 	"kpt-pasture/service/asynqsvc"
+	"kpt-pasture/service/httpclient"
 	"kpt-pasture/service/redis"
 	"kpt-pasture/service/sso"
 	"kpt-pasture/service/wechat"
@@ -40,5 +41,6 @@ func Options() []di.HubOption {
 		crontab.Module,
 		moduleMqtt.Module,
 		mqttstore.Module,
+		httpclient.Module,
 	}
 }

+ 131 - 0
service/httpclient/http.go

@@ -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)
+}

+ 20 - 0
service/httpclient/interface.go

@@ -0,0 +1,20 @@
+package httpclient
+
+import (
+	"kpt-pasture/config"
+	"net/http"
+
+	"gitee.com/xuyiping_admin/pkg/di"
+)
+
+type ClientService interface {
+	doRequest(req *http.Request) ([]byte, error)
+	DoGet(url string) ([]byte, error)
+	DoPost(url string, body interface{}) ([]byte, error)
+}
+
+var Module = di.Provide(NewService)
+
+func NewService(cfg *config.AppConfig) *Service {
+	return NewClientService()
+}