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.9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. func HttpGet(method string, url string) ([]byte, error) {
  67. request, err := http.NewRequest(method, url, nil)
  68. client := &http.Client{}
  69. res, err := client.Do(request)
  70. if err != nil {
  71. log.Fatal(err)
  72. }
  73. defer res.Body.Close()
  74. body, err := io.ReadAll(res.Body)
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. return body, err
  79. }
  80. // 发送POST请求
  81. // url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
  82. // content:请求放回的内容
  83. func HttpPost(url string, data interface{}) (content string, err error) {
  84. jsonStr, _ := json.Marshal(data)
  85. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  86. req.Header.Add("content-type", "application/json")
  87. if err != nil {
  88. return
  89. }
  90. defer req.Body.Close()
  91. client := &http.Client{Timeout: 5 * time.Second}
  92. resp, err := client.Do(req)
  93. if err != nil {
  94. return
  95. }
  96. defer resp.Body.Close()
  97. result, _ := ioutil.ReadAll(resp.Body)
  98. content = string(result)
  99. return
  100. }
  101. func HttpClientStatusCode(method string, url string, payload io.Reader, token string) (int, error) {
  102. request, err := http.NewRequest(method, url, payload)
  103. request.Header.Add("Content-Type", "application/json")
  104. request.Header.Add("User-Agent", "API Explorer")
  105. request.Header.Add("x-auth-token", token)
  106. client := &http.Client{}
  107. res, err := client.Do(request)
  108. if err != nil {
  109. log.Fatal(err)
  110. }
  111. return res.StatusCode, err
  112. }
  113. func HttpClientWithQueries[T any](method string, url string, payload io.Reader, token string, param T) ([]byte, error) {
  114. request, err := http.NewRequest(method, url, payload)
  115. request.Header.Add("Content-Type", "application/json")
  116. request.Header.Add("User-Agent", "API Explorer")
  117. request.Header.Add("x-auth-token", token)
  118. convertStructToQueryUrl(request, param)
  119. client := &http.Client{}
  120. res, err := client.Do(request)
  121. if err != nil {
  122. log.Fatal(err)
  123. }
  124. defer res.Body.Close()
  125. body, err := io.ReadAll(res.Body)
  126. if err != nil {
  127. log.Fatal(err)
  128. }
  129. return body, err
  130. }
  131. func convertStructToQueryUrl[T any](request *http.Request, param T) {
  132. query := request.URL.Query()
  133. values := reflect.ValueOf(param)
  134. types := values.Type()
  135. for i := 0; i < values.NumField(); i++ {
  136. if !values.Field(i).IsZero() {
  137. if values.Field(i).CanInt() {
  138. query.Add(types.Field(i).Name, strconv.FormatInt(values.Field(i).Int(), 10))
  139. } else if values.Field(i).Kind() == reflect.Bool {
  140. query.Add(types.Field(i).Name, strconv.FormatBool(values.Field(i).Bool()))
  141. } else {
  142. query.Add(types.Field(i).Name, values.Field(i).String())
  143. }
  144. }
  145. }
  146. request.URL.RawQuery = query.Encode()
  147. }
  148. func HttpClientWithBodyAndCode(method string, url string, payload io.Reader, token string) (int, []byte, error) {
  149. request, err := http.NewRequest(method, url, payload)
  150. request.Header.Add("Content-Type", "application/json")
  151. request.Header.Add("User-Agent", "API Explorer")
  152. request.Header.Add("x-auth-token", token)
  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. }
  162. func HttpClientWithScreen(method string, url string, payload io.Reader) (int, []byte, error) {
  163. request, err := http.NewRequest(method, url, payload)
  164. client := &http.Client{}
  165. res, err := client.Do(request)
  166. if err != nil {
  167. log.Fatal(err)
  168. }
  169. defer res.Body.Close()
  170. body, err := io.ReadAll(res.Body)
  171. return res.StatusCode, body, err
  172. }

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.