client.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package mhayaHttp
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "time"
  10. clog "github.com/mhaya/logger"
  11. )
  12. var (
  13. postContentType = "application/x-www-form-urlencoded"
  14. jsonContentType = "application/json"
  15. DefaultTimeout = 5 * time.Second
  16. )
  17. func GET(httpURL string, values ...map[string]string) ([]byte, *http.Response, error) {
  18. client := http.Client{Timeout: DefaultTimeout}
  19. if len(values) > 0 {
  20. rst := ToUrlValues(values[0])
  21. httpURL = AddParams(httpURL, rst)
  22. }
  23. rsp, err := client.Get(httpURL)
  24. if err != nil {
  25. return nil, rsp, err
  26. }
  27. defer func(body io.ReadCloser) {
  28. e := body.Close()
  29. if e != nil {
  30. clog.Warnf("HTTP GET [url = %s], error = %s", httpURL, e)
  31. }
  32. }(rsp.Body)
  33. bodyBytes, err := io.ReadAll(rsp.Body)
  34. if err != nil {
  35. return nil, rsp, err
  36. }
  37. return bodyBytes, rsp, nil
  38. }
  39. func POST(httpURL string, values map[string]string) ([]byte, *http.Response, error) {
  40. client := http.Client{Timeout: DefaultTimeout}
  41. rst := ToUrlValues(values)
  42. rsp, err := client.Post(httpURL, postContentType, strings.NewReader(rst.Encode()))
  43. if err != nil {
  44. return nil, rsp, err
  45. }
  46. defer func(body io.ReadCloser) {
  47. e := body.Close()
  48. if e != nil {
  49. clog.Warnf("HTTP POST [url = %s], error = %s", httpURL, e)
  50. }
  51. }(rsp.Body)
  52. bodyBytes, err := io.ReadAll(rsp.Body)
  53. if err != nil {
  54. return nil, rsp, err
  55. }
  56. return bodyBytes, rsp, nil
  57. }
  58. func PostJSON(httpURL string, values interface{}) ([]byte, *http.Response, error) {
  59. client := http.Client{Timeout: DefaultTimeout}
  60. jsonBytes, err := json.Marshal(values)
  61. if err != nil {
  62. return nil, nil, err
  63. }
  64. rsp, err := client.Post(httpURL, jsonContentType, bytes.NewBuffer(jsonBytes))
  65. if err != nil {
  66. return nil, rsp, err
  67. }
  68. defer func(body io.ReadCloser) {
  69. e := body.Close()
  70. if e != nil {
  71. clog.Warnf("HTTP PostJSON [url = %s], error = %s", httpURL, e)
  72. }
  73. }(rsp.Body)
  74. bodyBytes, err := io.ReadAll(rsp.Body)
  75. if err != nil {
  76. return nil, rsp, err
  77. }
  78. return bodyBytes, rsp, nil
  79. }
  80. func AddParams(httpURL string, params url.Values) string {
  81. if len(params) == 0 {
  82. return httpURL
  83. }
  84. if !strings.Contains(httpURL, "?") {
  85. httpURL += "?"
  86. }
  87. if strings.HasSuffix(httpURL, "?") || strings.HasSuffix(httpURL, "&") {
  88. httpURL += params.Encode()
  89. } else {
  90. httpURL += "&" + params.Encode()
  91. }
  92. return httpURL
  93. }
  94. func ToUrlValues(values map[string]string) url.Values {
  95. rst := make(url.Values)
  96. for k, v := range values {
  97. rst.Add(k, v)
  98. }
  99. return rst
  100. }