request.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package http_client
  2. import (
  3. "encoding/json"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "github.com/go-resty/resty/v2"
  6. "github.com/pkg/errors"
  7. "io"
  8. )
  9. type RequestOption func(request *Request)
  10. func WithRequestHeaders(headers map[string]string) RequestOption {
  11. return func(request *Request) {
  12. request.SetHeaders(headers)
  13. }
  14. }
  15. func WithRequestQueryParams(queryParams map[string]string) RequestOption {
  16. return func(request *Request) {
  17. request.SetQueryParams(queryParams)
  18. }
  19. }
  20. func WithRequestPathParams(pathParams map[string]string) RequestOption {
  21. return func(request *Request) {
  22. request.SetPathParams(pathParams)
  23. }
  24. }
  25. func WithRequestFiles(files map[string]string) RequestOption {
  26. return func(request *Request) {
  27. request.SetFiles(files)
  28. }
  29. }
  30. func WithRequestFileReader(paramName string, fileName string, reader io.Reader) RequestOption {
  31. return func(request *Request) {
  32. request.SetFileReader(paramName, fileName, reader)
  33. }
  34. }
  35. func WithRequestFormData(data map[string]string) RequestOption {
  36. return func(request *Request) {
  37. request.SetFormData(data)
  38. }
  39. }
  40. type Request struct {
  41. request *resty.Request
  42. }
  43. func (request *Request) SetHeaders(headers map[string]string) {
  44. if headers == nil || len(headers) == 0 {
  45. return
  46. }
  47. request.request.SetHeaders(headers)
  48. }
  49. func (request *Request) SetQueryParams(queryParams map[string]string) {
  50. if queryParams == nil || len(queryParams) == 0 {
  51. return
  52. }
  53. request.request.SetQueryParams(queryParams)
  54. }
  55. func (request *Request) SetPathParams(pathParams map[string]string) {
  56. if pathParams == nil || len(pathParams) == 0 {
  57. return
  58. }
  59. request.request.SetPathParams(pathParams)
  60. }
  61. func (request *Request) SetFiles(files map[string]string) {
  62. if files == nil || len(files) == 0 {
  63. return
  64. }
  65. request.request.SetFiles(files)
  66. }
  67. func (request *Request) SetFileReader(paramName string, fileName string, reader io.Reader) {
  68. if strutils.IsStringEmpty(paramName) || strutils.IsStringEmpty(fileName) || reader == nil {
  69. return
  70. }
  71. request.request.SetFileReader(paramName, fileName, reader)
  72. }
  73. func (request *Request) SetFormData(data map[string]string) {
  74. if data == nil || len(data) == 0 {
  75. return
  76. }
  77. request.request.SetFormData(data)
  78. }
  79. func (request *Request) Post(url string, body any, opts ...RequestOption) (*Response, error) {
  80. for _, opt := range opts {
  81. opt(request)
  82. }
  83. restyRequest := request.request
  84. if body != nil {
  85. restyRequest = restyRequest.SetBody(body)
  86. }
  87. resp, err := restyRequest.Post(url)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if resp.IsError() {
  92. return nil, errors.New(resp.Status())
  93. }
  94. return &Response{response: resp}, nil
  95. }
  96. func (request *Request) Delete(url string, opts ...RequestOption) (*Response, error) {
  97. for _, opt := range opts {
  98. opt(request)
  99. }
  100. restyRequest := request.request
  101. resp, err := restyRequest.Delete(url)
  102. if err != nil {
  103. return nil, err
  104. }
  105. if resp.IsError() {
  106. return nil, errors.New(resp.Status())
  107. }
  108. return &Response{response: resp}, nil
  109. }
  110. func (request *Request) Put(url string, body any, opts ...RequestOption) (*Response, error) {
  111. for _, opt := range opts {
  112. opt(request)
  113. }
  114. restyRequest := request.request
  115. if body != nil {
  116. restyRequest = restyRequest.SetBody(body)
  117. }
  118. resp, err := restyRequest.Put(url)
  119. if err != nil {
  120. return nil, err
  121. }
  122. if resp.IsError() {
  123. return nil, errors.New(resp.Status())
  124. }
  125. return &Response{response: resp}, nil
  126. }
  127. func (request *Request) Get(url string, opts ...RequestOption) (*Response, error) {
  128. for _, opt := range opts {
  129. opt(request)
  130. }
  131. restyRequest := request.request
  132. resp, err := restyRequest.Get(url)
  133. if err != nil {
  134. return nil, err
  135. }
  136. if resp.IsError() {
  137. return nil, errors.New(resp.Status())
  138. }
  139. return &Response{response: resp}, nil
  140. }
  141. type Response struct {
  142. response *resty.Response
  143. }
  144. func (response *Response) Body() []byte {
  145. return response.response.Body()
  146. }
  147. func (response *Response) Json(resp any) error {
  148. body := response.response.Body()
  149. if body == nil || len(body) == 0 {
  150. return errors.New("响应体为空")
  151. }
  152. return json.Unmarshal(body, resp)
  153. }