You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

http.go 4.7 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package tool
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "encoding/json"
  6. "github.com/go-resty/resty/v2"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "net/http"
  11. "reflect"
  12. "strconv"
  13. "time"
  14. )
  15. const (
  16. GET = "GET"
  17. PUT = "PUT"
  18. POST = "POST"
  19. DELETE = "DELETE"
  20. )
  21. const (
  22. ContentType = "Content-Type"
  23. ApplicationJson = "application/json"
  24. ApplicationFromUrlencoded = "application/x-www-from-urlencoded"
  25. )
  26. var httpClient *resty.Client = nil
  27. var httpsClient *resty.Client = nil
  28. func NewHttpsClient() *resty.Client {
  29. if httpsClient != nil {
  30. return httpsClient
  31. }
  32. c := resty.New()
  33. c.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  34. c.SetTimeout(5 * time.Second)
  35. c.SetRetryCount(3)
  36. //debug := config.GetConfig("httpclient.debug")
  37. debug := "true"
  38. if len(debug) > 0 && debug == "ON" {
  39. c.SetDebug(true)
  40. }
  41. httpsClient = c
  42. return c
  43. }
  44. func GetACHttpRequest() *resty.Request {
  45. client := resty.New()
  46. request := client.R()
  47. return request
  48. }
  49. func HttpClient(method string, url string, payload io.Reader, token string) ([]byte, error) {
  50. request, err := http.NewRequest(method, url, payload)
  51. request.Header.Add("Content-Type", "application/json")
  52. request.Header.Add("User-Agent", "API Explorer")
  53. request.Header.Add("x-auth-token", token)
  54. client := &http.Client{}
  55. res, err := client.Do(request)
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. defer res.Body.Close()
  60. body, err := io.ReadAll(res.Body)
  61. if err != nil {
  62. log.Fatal(err)
  63. }
  64. return body, err
  65. }
  66. // 发送POST请求
  67. // url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
  68. // content:请求放回的内容
  69. func HttpPost(url string, data interface{}) (content string, err error) {
  70. jsonStr, _ := json.Marshal(data)
  71. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  72. req.Header.Add("content-type", "application/json")
  73. if err != nil {
  74. return
  75. }
  76. defer req.Body.Close()
  77. client := &http.Client{Timeout: 5 * time.Second}
  78. resp, err := client.Do(req)
  79. if err != nil {
  80. return
  81. }
  82. defer resp.Body.Close()
  83. result, _ := ioutil.ReadAll(resp.Body)
  84. content = string(result)
  85. return
  86. }
  87. func HttpClientStatusCode(method string, url string, payload io.Reader, token string) (int, error) {
  88. request, err := http.NewRequest(method, url, payload)
  89. request.Header.Add("Content-Type", "application/json")
  90. request.Header.Add("User-Agent", "API Explorer")
  91. request.Header.Add("x-auth-token", token)
  92. client := &http.Client{}
  93. res, err := client.Do(request)
  94. if err != nil {
  95. log.Fatal(err)
  96. }
  97. return res.StatusCode, err
  98. }
  99. func HttpClientWithQueries[T any](method string, url string, payload io.Reader, token string, param T) ([]byte, error) {
  100. request, err := http.NewRequest(method, url, payload)
  101. request.Header.Add("Content-Type", "application/json")
  102. request.Header.Add("User-Agent", "API Explorer")
  103. request.Header.Add("x-auth-token", token)
  104. convertStructToQueryUrl(request, param)
  105. client := &http.Client{}
  106. res, err := client.Do(request)
  107. if err != nil {
  108. log.Fatal(err)
  109. }
  110. defer res.Body.Close()
  111. body, err := io.ReadAll(res.Body)
  112. if err != nil {
  113. log.Fatal(err)
  114. }
  115. return body, err
  116. }
  117. func convertStructToQueryUrl[T any](request *http.Request, param T) {
  118. if reflect.ValueOf(param).IsZero() {
  119. return
  120. }
  121. query := request.URL.Query()
  122. values := reflect.ValueOf(param)
  123. types := values.Type()
  124. for i := 0; i < values.NumField(); i++ {
  125. if !values.Field(i).IsZero() {
  126. if values.Field(i).CanInt() {
  127. query.Add(types.Field(i).Name, strconv.FormatInt(values.Field(i).Int(), 10))
  128. } else if values.Field(i).Kind() == reflect.Bool {
  129. query.Add(types.Field(i).Name, strconv.FormatBool(values.Field(i).Bool()))
  130. } else {
  131. query.Add(types.Field(i).Name, values.Field(i).String())
  132. }
  133. }
  134. }
  135. request.URL.RawQuery = query.Encode()
  136. }
  137. func HttpClientWithBodyAndCode(method string, url string, payload io.Reader, token string) (int, []byte, error) {
  138. request, err := http.NewRequest(method, url, payload)
  139. request.Header.Add("Content-Type", "application/json")
  140. request.Header.Add("User-Agent", "API Explorer")
  141. request.Header.Add("x-auth-token", token)
  142. client := &http.Client{}
  143. res, err := client.Do(request)
  144. if err != nil {
  145. log.Fatal(err)
  146. }
  147. defer res.Body.Close()
  148. body, err := io.ReadAll(res.Body)
  149. return res.StatusCode, body, err
  150. }
  151. func HttpClientWithScreen(method string, url string, payload io.Reader) (int, []byte, error) {
  152. request, err := http.NewRequest(method, url, payload)
  153. client := &http.Client{}
  154. res, err := client.Do(request)
  155. if err != nil {
  156. log.Fatal(err)
  157. }
  158. defer res.Body.Close()
  159. body, err := io.ReadAll(res.Body)
  160. return res.StatusCode, body, err
  161. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.