http_client.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package httpClient
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "eta/eta_mini_crm_ht/utils"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "strings"
  11. "time"
  12. )
  13. type HttpClient struct {
  14. *http.Client
  15. maxRetries int
  16. retryDelayFunc RetryDelayFunc
  17. }
  18. // NewClient 构造函数,其中 delayFunc 参数是可选的
  19. func NewClient(timeout time.Duration, maxRetries int, delayFunc ...RetryDelayFunc) *HttpClient {
  20. var df RetryDelayFunc
  21. if len(delayFunc) > 0 {
  22. df = delayFunc[0]
  23. } else {
  24. df = defaultRetryDelayFunc
  25. }
  26. return &HttpClient{
  27. Client: &http.Client{Timeout: timeout},
  28. maxRetries: maxRetries,
  29. retryDelayFunc: df,
  30. }
  31. }
  32. func DefaultClient() *HttpClient {
  33. return NewClient(time.Second*10, 3)
  34. }
  35. func defaultRetryDelayFunc(attempt int) time.Duration {
  36. delay := time.Duration(attempt) * time.Second
  37. if attempt > 0 {
  38. delay *= 2
  39. }
  40. return delay
  41. }
  42. type RetryDelayFunc func(attempt int) time.Duration
  43. func retryErr(err error) bool {
  44. return errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled)
  45. }
  46. // DoWithRetry 发送带有重试机制的HTTP请求,允许用户自定义重试延迟逻辑
  47. func (hc *HttpClient) DoWithRetry(ctx context.Context, req *http.Request) (resp *http.Response, err error) {
  48. attempt := 0
  49. for {
  50. resp, err = hc.Do(req.WithContext(ctx))
  51. if err != nil && retryErr(err) {
  52. if attempt >= hc.maxRetries {
  53. return nil, fmt.Errorf("请求失败: %w", err)
  54. }
  55. attempt++
  56. delay := hc.retryDelayFunc(attempt)
  57. time.Sleep(delay)
  58. continue
  59. }
  60. return
  61. }
  62. }
  63. func (hc *HttpClient) Post(url string, data interface{}) (resp *http.Response, err error) {
  64. dataStr, err := json.Marshal(data)
  65. if err != nil {
  66. utils.FileLog.Error("请求data json序列化失败,err:" + err.Error())
  67. }
  68. body := io.NopCloser(strings.NewReader(string(dataStr)))
  69. req, err := http.NewRequest(http.MethodPost, url, body)
  70. req.Header.Set("Content-Type", "application/json")
  71. if err != nil {
  72. utils.FileLog.Error("创建POST请求失败: %v", err)
  73. }
  74. resp, err = hc.DoWithRetry(req.Context(), req)
  75. if err == nil {
  76. code := resp.StatusCode
  77. if code != 200 {
  78. utils.FileLog.Error("请求错误应答,状态码:%d", code)
  79. errMsg := fmt.Sprintf("请求状态码异常,StatusCode:[%d]", code)
  80. respBody, respErr := io.ReadAll(resp.Body)
  81. if respErr != nil {
  82. utils.FileLog.Error("读取body失败,err:%v", err)
  83. err = errors.New(errMsg)
  84. return
  85. }
  86. utils.FileLog.Error("请求错误应答,body:%s", string(respBody))
  87. errMsg = fmt.Sprintf("%s,body:%s", errMsg, string(respBody))
  88. err = errors.New(errMsg)
  89. return
  90. }
  91. } else {
  92. utils.FileLog.Error("未知的应答错误,获取第三方授权信息失败")
  93. }
  94. return
  95. }
  96. func (hc *HttpClient) PostWithAuth(url string, data interface{}, token string) (resp *http.Response, err error) {
  97. dataStr, err := json.Marshal(data)
  98. if err != nil {
  99. utils.FileLog.Error("请求data json序列化失败,err:" + err.Error())
  100. }
  101. body := io.NopCloser(strings.NewReader(string(dataStr)))
  102. req, err := http.NewRequest(http.MethodPost, url, body)
  103. req.Header.Set("Content-Type", "application/json")
  104. req.Header.Set("Authorization", token)
  105. if err != nil {
  106. utils.FileLog.Error("创建POST请求失败: %v", err)
  107. }
  108. resp, err = hc.DoWithRetry(req.Context(), req)
  109. code := resp.StatusCode
  110. if code != 200 {
  111. utils.FileLog.Error("请求错误应答,状态码:%d", code)
  112. errMsg := fmt.Sprintf("请求状态码异常,StatusCode:[%d]", code)
  113. respBody, respErr := io.ReadAll(resp.Body)
  114. if respErr != nil {
  115. utils.FileLog.Error("读取body失败,err:%v", err)
  116. err = errors.New(errMsg)
  117. return
  118. }
  119. utils.FileLog.Error("请求错误应答,body:%s", string(respBody))
  120. errMsg = fmt.Sprintf("%s,body:%s", errMsg, string(respBody))
  121. err = errors.New(errMsg)
  122. return
  123. }
  124. return
  125. }
  126. func (hc *HttpClient) Get(url string) (resp *http.Response, err error) {
  127. req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
  128. if err != nil {
  129. utils.FileLog.Error("创建请求失败: %v", err)
  130. }
  131. resp, err = hc.DoWithRetry(req.Context(), req)
  132. return
  133. }