util.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package httpclient
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/tls"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "time"
  10. "git.bvbej.com/bvbej/base-golang/pkg/trace"
  11. "github.com/pkg/errors"
  12. "go.uber.org/zap"
  13. )
  14. const (
  15. // _StatusReadRespErr read resp body err, should re-call doHTTP again.
  16. _StatusReadRespErr = -204
  17. // _StatusDoReqErr do req err, should re-call doHTTP again.
  18. _StatusDoReqErr = -500
  19. )
  20. var defaultClient = &http.Client{
  21. Transport: &http.Transport{
  22. DisableKeepAlives: true,
  23. DisableCompression: true,
  24. TLSClientConfig: &tls.Config{
  25. InsecureSkipVerify: true,
  26. },
  27. MaxIdleConns: 100,
  28. MaxConnsPerHost: 100,
  29. MaxIdleConnsPerHost: 100,
  30. },
  31. }
  32. func doHTTP(ctx context.Context, method, url string, payload []byte, opt *option) ([]byte, int, error) {
  33. ts := time.Now()
  34. if mock := opt.mock; mock != nil {
  35. if opt.dialog != nil {
  36. opt.dialog.AppendResponse(&trace.Response{
  37. HttpCode: http.StatusOK,
  38. HttpCodeMsg: http.StatusText(http.StatusOK),
  39. Body: string(mock()),
  40. CostSeconds: time.Since(ts).Seconds(),
  41. })
  42. }
  43. return mock(), http.StatusOK, nil
  44. }
  45. req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(payload))
  46. if err != nil {
  47. return nil, -1, errors.Wrapf(err, "new request [%s %s] err", method, url)
  48. }
  49. for key, value := range opt.header {
  50. req.Header.Set(key, value[0])
  51. }
  52. if opt.basicAuth.enabled {
  53. req.SetBasicAuth(opt.basicAuth.username, opt.basicAuth.password)
  54. }
  55. resp, err := defaultClient.Do(req)
  56. if err != nil {
  57. err = errors.Wrapf(err, "do request [%s %s] err", method, url)
  58. if opt.dialog != nil {
  59. opt.dialog.AppendResponse(&trace.Response{
  60. Body: err.Error(),
  61. CostSeconds: time.Since(ts).Seconds(),
  62. })
  63. }
  64. if opt.logger != nil {
  65. opt.logger.Warn("doHTTP got err", zap.Error(err))
  66. }
  67. return nil, _StatusDoReqErr, err
  68. }
  69. defer resp.Body.Close()
  70. body, err := ioutil.ReadAll(resp.Body)
  71. if err != nil {
  72. err = errors.Wrapf(err, "read resp body from [%s %s] err", method, url)
  73. if opt.dialog != nil {
  74. opt.dialog.AppendResponse(&trace.Response{
  75. Body: err.Error(),
  76. CostSeconds: time.Since(ts).Seconds(),
  77. })
  78. }
  79. if opt.logger != nil {
  80. opt.logger.Warn("doHTTP got err", zap.Error(err))
  81. }
  82. return nil, _StatusReadRespErr, err
  83. }
  84. defer func() {
  85. if opt.dialog != nil {
  86. opt.dialog.AppendResponse(&trace.Response{
  87. Header: resp.Header,
  88. HttpCode: resp.StatusCode,
  89. HttpCodeMsg: resp.Status,
  90. Body: string(body), // unsafe
  91. CostSeconds: time.Since(ts).Seconds(),
  92. })
  93. }
  94. }()
  95. if resp.StatusCode != http.StatusOK {
  96. return nil, resp.StatusCode, newReplyErr(
  97. resp.StatusCode,
  98. body,
  99. errors.Errorf("do [%s %s] return code: %d message: %s", method, url, resp.StatusCode, string(body)),
  100. )
  101. }
  102. return body, http.StatusOK, nil
  103. }
  104. // addFormValuesIntoURL append url.Values into url string
  105. func addFormValuesIntoURL(rawURL string, form url.Values) (string, error) {
  106. if rawURL == "" {
  107. return "", errors.New("rawURL required")
  108. }
  109. if len(form) == 0 {
  110. return "", errors.New("form required")
  111. }
  112. target, err := url.Parse(rawURL)
  113. if err != nil {
  114. return "", errors.Wrapf(err, "parse rawURL `%s` err", rawURL)
  115. }
  116. urlValues := target.Query()
  117. for key, values := range form {
  118. for _, value := range values {
  119. urlValues.Add(key, value)
  120. }
  121. }
  122. target.RawQuery = urlValues.Encode()
  123. return target.String(), nil
  124. }