zqy_httpclient.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package thirdparty
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "gd_management/common.in/config"
  6. "gd_management/common.in/utils"
  7. "fmt"
  8. "go.uber.org/zap"
  9. "io/ioutil"
  10. "net/http"
  11. "strings"
  12. "time"
  13. )
  14. type HttpRequestWithHead struct {
  15. Url string
  16. Method string
  17. Head map[string]string
  18. TimeOut time.Duration
  19. Query map[string]string
  20. Body []byte
  21. }
  22. func (h HttpRequestWithHead) request() ([]byte, error) {
  23. request, err := http.NewRequest(h.Method, h.Url, bytes.NewReader(h.Body))
  24. if h.Method == http.MethodPost ||
  25. h.Method == http.MethodPut ||
  26. h.Method == http.MethodDelete {
  27. request.Header.Add("Content-Type", "application/json")
  28. }
  29. for k, v := range h.Head {
  30. request.Header.Add(k, v)
  31. }
  32. //跳过证书验证
  33. tr := &http.Transport{
  34. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  35. }
  36. client := http.Client{
  37. Transport: tr,
  38. Timeout: h.TimeOut,
  39. }
  40. resp, err := client.Do(request)
  41. if err != nil {
  42. return nil, err
  43. }
  44. defer resp.Body.Close()
  45. if resp.StatusCode != http.StatusOK {
  46. if resp.Body == nil {
  47. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  48. }
  49. contents, err := ioutil.ReadAll(resp.Body)
  50. if err != nil {
  51. return nil, fmt.Errorf("wrong status code: %d", resp.StatusCode)
  52. }
  53. return nil, fmt.Errorf("%s", contents)
  54. }
  55. contents, err := ioutil.ReadAll(resp.Body)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return contents, nil
  60. }
  61. func joinGetRequestArgs(data map[string]string) string {
  62. var path string
  63. if data != nil {
  64. path += "?"
  65. for k, v := range data {
  66. path += k + "=" + v + "&"
  67. }
  68. path = strings.TrimRight(path, "&")
  69. return path
  70. }
  71. return path
  72. }
  73. func HttpGetWithHead(path string, head map[string]string,
  74. query map[string]string) ([]byte, error) {
  75. if query != nil {
  76. path += joinGetRequestArgs(query)
  77. }
  78. h := HttpRequestWithHead{
  79. Url: path,
  80. Method: http.MethodGet,
  81. Head: head,
  82. TimeOut: 60 * time.Second,
  83. }
  84. return h.request()
  85. }
  86. func ZqyHttpGet(api string, data map[string]string) (result []byte, err error) {
  87. //fullApi := che300FullURL(api)
  88. defer func() {
  89. l.Info("ZqyHttpGet",
  90. zap.String("api", api),
  91. zap.String("request", utils.MarshalJsonString(data)),
  92. zap.String("response", utils.MarshalJsonString(result)))
  93. }()
  94. if data == nil {
  95. data = make(map[string]string)
  96. }
  97. data["key"] = config.Conf.ThirdPart.ZqyKey
  98. result, err = HttpGetWithHead(api, nil, data)
  99. if err != nil {
  100. return nil, err
  101. }
  102. return
  103. }