|
@@ -18,27 +18,27 @@ const (
|
|
post = "POST"
|
|
post = "POST"
|
|
)
|
|
)
|
|
|
|
|
|
-type HttpClient interface {
|
|
|
|
- Get(url string, head http.Header, params map[string]interface{}) (Response, error)
|
|
|
|
- Post(url string, head http.Header, data interface{}) (Response, error)
|
|
|
|
- PostFromData(url string, fromData url.Values) (Response, error)
|
|
|
|
- doRequest(req *http.Request) (Response, error)
|
|
|
|
|
|
+type HttpClient[T any] interface {
|
|
|
|
+ Get(url string, head http.Header, params map[string]interface{}) (Response[T], error)
|
|
|
|
+ Post(url string, head http.Header, data interface{}) (Response[T], error)
|
|
|
|
+ PostFromData(url string, fromData url.Values) (Response[T], error)
|
|
|
|
+ doRequest(req *http.Request) (Response[T], error)
|
|
}
|
|
}
|
|
|
|
|
|
-type Response struct {
|
|
|
|
|
|
+type Response[T any] struct {
|
|
Code int `json:"code"`
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Message string `json:"message"`
|
|
- Data string `json:"data"`
|
|
|
|
|
|
+ Data T `json:"data"`
|
|
TraceId string `json:"traceId"`
|
|
TraceId string `json:"traceId"`
|
|
}
|
|
}
|
|
|
|
|
|
// DefaultHttpClient 结构体实现 SSOClient 接口
|
|
// DefaultHttpClient 结构体实现 SSOClient 接口
|
|
-type DefaultHttpClient struct {
|
|
|
|
|
|
+type DefaultHttpClient[T any] struct {
|
|
client *http.Client
|
|
client *http.Client
|
|
retryStrategy RetryStrategy
|
|
retryStrategy RetryStrategy
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *DefaultHttpClient) PostFromData(urlStr string, fromData url.Values) (Response, error) {
|
|
|
|
|
|
+func (c *DefaultHttpClient[T]) PostFromData(urlStr string, fromData url.Values) (Response[T], error) {
|
|
dataStr, err := json.Marshal(fromData)
|
|
dataStr, err := json.Marshal(fromData)
|
|
if err != nil {
|
|
if err != nil {
|
|
global.FILE_LOG.Error("formData参数json序列化失败,err:" + err.Error())
|
|
global.FILE_LOG.Error("formData参数json序列化失败,err:" + err.Error())
|
|
@@ -46,7 +46,7 @@ func (c *DefaultHttpClient) PostFromData(urlStr string, fromData url.Values) (Re
|
|
global.FILE_LOG.Info("post请求:" + urlStr + ",formData:" + string(dataStr))
|
|
global.FILE_LOG.Info("post请求:" + urlStr + ",formData:" + string(dataStr))
|
|
return c.doPostForm(urlStr, fromData)
|
|
return c.doPostForm(urlStr, fromData)
|
|
}
|
|
}
|
|
-func (c *DefaultHttpClient) Post(urlStr string, head http.Header, data interface{}) (response Response, err error) {
|
|
|
|
|
|
+func (c *DefaultHttpClient[T]) Post(urlStr string, head http.Header, data interface{}) (response Response[T], err error) {
|
|
dataStr, err := json.Marshal(data)
|
|
dataStr, err := json.Marshal(data)
|
|
if err != nil {
|
|
if err != nil {
|
|
global.FILE_LOG.Error("请求data json序列化失败,err:" + err.Error())
|
|
global.FILE_LOG.Error("请求data json序列化失败,err:" + err.Error())
|
|
@@ -70,7 +70,7 @@ func (c *DefaultHttpClient) Post(urlStr string, head http.Header, data interface
|
|
return c.doRequest(req)
|
|
return c.doRequest(req)
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *DefaultHttpClient) Get(urlStr string, head http.Header, params map[string]interface{}) (response Response, err error) {
|
|
|
|
|
|
+func (c *DefaultHttpClient[T]) Get(urlStr string, head http.Header, params map[string]interface{}) (response Response[T], err error) {
|
|
if len(params) > 0 {
|
|
if len(params) > 0 {
|
|
queryParams := url.Values{}
|
|
queryParams := url.Values{}
|
|
for key, value := range params {
|
|
for key, value := range params {
|
|
@@ -92,14 +92,14 @@ func (c *DefaultHttpClient) Get(urlStr string, head http.Header, params map[stri
|
|
return c.doRequest(req)
|
|
return c.doRequest(req)
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *DefaultHttpClient) RetryStrategy(retryStrategy RetryStrategy) *DefaultHttpClient {
|
|
|
|
|
|
+func (c *DefaultHttpClient[T]) RetryStrategy(retryStrategy RetryStrategy) *DefaultHttpClient[T] {
|
|
c.retryStrategy = retryStrategy
|
|
c.retryStrategy = retryStrategy
|
|
return c
|
|
return c
|
|
}
|
|
}
|
|
|
|
|
|
// retryRequest 尝试执行请求并根据重试策略重试
|
|
// retryRequest 尝试执行请求并根据重试策略重试
|
|
// retryRequest 尝试执行请求并根据重试策略重试
|
|
// retryRequest 尝试执行请求并根据重试策略重试
|
|
-func (c *DefaultHttpClient) retryRequest(requestFunc func() (*http.Response, error)) (result *http.Response, err error) {
|
|
|
|
|
|
+func (c *DefaultHttpClient[T]) retryRequest(requestFunc func() (*http.Response, error)) (result *http.Response, err error) {
|
|
for i := 0; ; i++ {
|
|
for i := 0; ; i++ {
|
|
result, err = requestFunc()
|
|
result, err = requestFunc()
|
|
if err == nil || !c.retryStrategy.ShouldRetry(i, err) {
|
|
if err == nil || !c.retryStrategy.ShouldRetry(i, err) {
|
|
@@ -110,7 +110,7 @@ func (c *DefaultHttpClient) retryRequest(requestFunc func() (*http.Response, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (c *DefaultHttpClient) doPostForm(urlStr string, values url.Values) (response Response, err error) {
|
|
|
|
|
|
+func (c *DefaultHttpClient[T]) doPostForm(urlStr string, values url.Values) (response Response[T], err error) {
|
|
global.FILE_LOG.Info("发送请求:")
|
|
global.FILE_LOG.Info("发送请求:")
|
|
//ctx 用Background
|
|
//ctx 用Background
|
|
resp, err := c.retryRequest(func() (*http.Response, error) {
|
|
resp, err := c.retryRequest(func() (*http.Response, error) {
|
|
@@ -126,7 +126,7 @@ func (c *DefaultHttpClient) doPostForm(urlStr string, values url.Values) (respon
|
|
global.FILE_LOG.Error("http请求失败,读取body失败:" + err.Error())
|
|
global.FILE_LOG.Error("http请求失败,读取body失败:" + err.Error())
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- global.FILE_LOG.Error("应答为:" + string(repBody))
|
|
|
|
|
|
+ global.FILE_LOG.Info("应答为:" + string(repBody))
|
|
if resp.StatusCode != http.StatusOK {
|
|
if resp.StatusCode != http.StatusOK {
|
|
global.FILE_LOG.Error("http请求异常,请求状态为:" + resp.Status)
|
|
global.FILE_LOG.Error("http请求异常,请求状态为:" + resp.Status)
|
|
return
|
|
return
|
|
@@ -140,7 +140,7 @@ func (c *DefaultHttpClient) doPostForm(urlStr string, values url.Values) (respon
|
|
}
|
|
}
|
|
|
|
|
|
// DoRequest 执行 HTTP 请求
|
|
// DoRequest 执行 HTTP 请求
|
|
-func (c *DefaultHttpClient) doRequest(req *http.Request) (response Response, err error) {
|
|
|
|
|
|
+func (c *DefaultHttpClient[T]) doRequest(req *http.Request) (response Response[T], err error) {
|
|
global.FILE_LOG.Info("发送请求:")
|
|
global.FILE_LOG.Info("发送请求:")
|
|
//ctx 用Background
|
|
//ctx 用Background
|
|
resp, err := c.retryRequest(func() (*http.Response, error) {
|
|
resp, err := c.retryRequest(func() (*http.Response, error) {
|
|
@@ -200,8 +200,8 @@ func (s *FixedRetryStrategy) RetryDelay(attempt int) time.Duration {
|
|
return time.Duration(attempt) * s.delay
|
|
return time.Duration(attempt) * s.delay
|
|
}
|
|
}
|
|
|
|
|
|
-func CreateDefault() *DefaultHttpClient {
|
|
|
|
- return &DefaultHttpClient{
|
|
|
|
|
|
+func CreateDefault[T any]() *DefaultHttpClient[T] {
|
|
|
|
+ return &DefaultHttpClient[T]{
|
|
client: &http.Client{Timeout: 10 * time.Second},
|
|
client: &http.Client{Timeout: 10 * time.Second},
|
|
retryStrategy: newFixedRetryStrategy(3, 3*time.Second),
|
|
retryStrategy: newFixedRetryStrategy(3, 3*time.Second),
|
|
}
|
|
}
|