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 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package httputils
  13. import (
  14. "bytes"
  15. "crypto/tls"
  16. "encoding/json"
  17. "github.com/go-resty/resty/v2"
  18. "io"
  19. "io/ioutil"
  20. "log"
  21. "net/http"
  22. "reflect"
  23. "strconv"
  24. "time"
  25. )
  26. const (
  27. GET = "GET"
  28. PUT = "PUT"
  29. POST = "POST"
  30. DELETE = "DELETE"
  31. )
  32. const (
  33. ContentType = "Content-Type"
  34. ApplicationJson = "application/json"
  35. ApplicationFromUrlencoded = "application/x-www-from-urlencoded"
  36. )
  37. var httpClient *resty.Client = nil
  38. var httpsClient *resty.Client = nil
  39. func NewHttpsClient() *resty.Client {
  40. if httpsClient != nil {
  41. return httpsClient
  42. }
  43. c := resty.New()
  44. c.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  45. c.SetTimeout(5 * time.Second)
  46. c.SetRetryCount(3)
  47. debug := "true"
  48. if len(debug) > 0 && debug == "ON" {
  49. c.SetDebug(true)
  50. }
  51. httpsClient = c
  52. return c
  53. }
  54. func GetHttpRequest() *resty.Request {
  55. client := resty.New()
  56. request := client.R()
  57. return request
  58. }
  59. func HttpClient(method string, url string, payload io.Reader, token string) ([]byte, error) {
  60. request, err := http.NewRequest(method, url, payload)
  61. request.Header.Add("Content-Type", "application/json")
  62. request.Header.Add("User-Agent", "API Explorer")
  63. request.Header.Add("x-auth-token", token)
  64. client := &http.Client{}
  65. res, err := client.Do(request)
  66. if err != nil {
  67. log.Fatal(err)
  68. }
  69. defer res.Body.Close()
  70. body, err := io.ReadAll(res.Body)
  71. if err != nil {
  72. log.Fatal(err)
  73. }
  74. return body, err
  75. }
  76. func HttpGet(method string, url string) ([]byte, error) {
  77. request, err := http.NewRequest(method, url, nil)
  78. client := &http.Client{}
  79. res, err := client.Do(request)
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. defer res.Body.Close()
  84. body, err := io.ReadAll(res.Body)
  85. if err != nil {
  86. log.Fatal(err)
  87. }
  88. return body, err
  89. }
  90. // 发送POST请求
  91. // url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
  92. // content:请求放回的内容
  93. func HttpPost(url string, data interface{}) (content string, err error) {
  94. jsonStr, _ := json.Marshal(data)
  95. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  96. req.Header.Add("content-type", "application/json")
  97. if err != nil {
  98. return
  99. }
  100. defer req.Body.Close()
  101. client := &http.Client{Timeout: 5 * time.Second}
  102. resp, err := client.Do(req)
  103. if err != nil {
  104. return
  105. }
  106. defer resp.Body.Close()
  107. result, _ := ioutil.ReadAll(resp.Body)
  108. content = string(result)
  109. return
  110. }
  111. func HttpClientStatusCode(method string, url string, payload io.Reader, token string) (int, error) {
  112. request, err := http.NewRequest(method, url, payload)
  113. request.Header.Add("Content-Type", "application/json")
  114. request.Header.Add("User-Agent", "API Explorer")
  115. request.Header.Add("x-auth-token", token)
  116. client := &http.Client{}
  117. res, err := client.Do(request)
  118. if err != nil {
  119. log.Fatal(err)
  120. }
  121. return res.StatusCode, err
  122. }
  123. func HttpClientWithQueries[T any](method string, url string, payload io.Reader, token string, param T) ([]byte, error) {
  124. request, err := http.NewRequest(method, url, payload)
  125. request.Header.Add("Content-Type", "application/json")
  126. request.Header.Add("User-Agent", "API Explorer")
  127. request.Header.Add("x-auth-token", token)
  128. convertStructToQueryUrl(request, param)
  129. client := &http.Client{}
  130. res, err := client.Do(request)
  131. if err != nil {
  132. log.Fatal(err)
  133. }
  134. defer res.Body.Close()
  135. body, err := io.ReadAll(res.Body)
  136. if err != nil {
  137. log.Fatal(err)
  138. }
  139. return body, err
  140. }
  141. func convertStructToQueryUrl[T any](request *http.Request, param T) {
  142. query := request.URL.Query()
  143. values := reflect.ValueOf(param)
  144. types := values.Type()
  145. for i := 0; i < values.NumField(); i++ {
  146. if !values.Field(i).IsZero() {
  147. if values.Field(i).CanInt() {
  148. query.Add(types.Field(i).Name, strconv.FormatInt(values.Field(i).Int(), 10))
  149. } else if values.Field(i).Kind() == reflect.Bool {
  150. query.Add(types.Field(i).Name, strconv.FormatBool(values.Field(i).Bool()))
  151. } else {
  152. query.Add(types.Field(i).Name, values.Field(i).String())
  153. }
  154. }
  155. }
  156. request.URL.RawQuery = query.Encode()
  157. }
  158. func HttpClientWithBodyAndCode(method string, url string, payload io.Reader, token string) (int, []byte, error) {
  159. request, err := http.NewRequest(method, url, payload)
  160. request.Header.Add("Content-Type", "application/json")
  161. request.Header.Add("User-Agent", "API Explorer")
  162. request.Header.Add("x-auth-token", token)
  163. client := &http.Client{}
  164. res, err := client.Do(request)
  165. if err != nil {
  166. log.Fatal(err)
  167. }
  168. defer res.Body.Close()
  169. body, err := io.ReadAll(res.Body)
  170. return res.StatusCode, body, err
  171. }
  172. func HttpClientWithScreen(method string, url string, payload io.Reader) (int, []byte, error) {
  173. request, err := http.NewRequest(method, url, payload)
  174. client := &http.Client{}
  175. res, err := client.Do(request)
  176. if err != nil {
  177. log.Fatal(err)
  178. }
  179. defer res.Body.Close()
  180. body, err := io.ReadAll(res.Body)
  181. return res.StatusCode, body, err
  182. }

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.