request.go 3.4 KB

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