request.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package http_client
  2. import (
  3. "das/application/ports"
  4. "encoding/json"
  5. "errors"
  6. "github.com/go-resty/resty/v2"
  7. )
  8. type Request struct {
  9. request *resty.Request
  10. }
  11. func (request *Request) SetHeaders(headers map[string]string) {
  12. request.request.SetHeaders(headers)
  13. }
  14. func (request *Request) SetQueryParams(queryParams map[string]string) {
  15. request.request.SetQueryParams(queryParams)
  16. }
  17. func (request *Request) SetPathParams(pathParams map[string]string) {
  18. request.request.SetPathParams(pathParams)
  19. }
  20. func (request *Request) Post(url string, body any, opts ...ports.RequestOption) (ports.HttpResponse, error) {
  21. for _, opt := range opts {
  22. opt(request)
  23. }
  24. restyRequest := request.request
  25. resp, err := restyRequest.SetBody(body).Post(url)
  26. if err != nil {
  27. return nil, err
  28. }
  29. if resp.IsError() {
  30. return nil, errors.New(resp.Status())
  31. }
  32. return &Response{response: resp}, nil
  33. }
  34. func (request *Request) Delete(url string, opts ...ports.RequestOption) (ports.HttpResponse, error) {
  35. for _, opt := range opts {
  36. opt(request)
  37. }
  38. restyRequest := request.request
  39. resp, err := restyRequest.Delete(url)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if resp.IsError() {
  44. return nil, errors.New(resp.Status())
  45. }
  46. return &Response{response: resp}, nil
  47. }
  48. func (request *Request) Put(url string, body any, opts ...ports.RequestOption) (ports.HttpResponse, error) {
  49. for _, opt := range opts {
  50. opt(request)
  51. }
  52. restyRequest := request.request
  53. resp, err := restyRequest.SetBody(body).Put(url)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if resp.IsError() {
  58. return nil, errors.New(resp.Status())
  59. }
  60. return &Response{response: resp}, nil
  61. }
  62. func (request *Request) Get(url string, opts ...ports.RequestOption) (ports.HttpResponse, error) {
  63. for _, opt := range opts {
  64. opt(request)
  65. }
  66. restyRequest := request.request
  67. resp, err := restyRequest.Get(url)
  68. if err != nil {
  69. return nil, err
  70. }
  71. if resp.IsError() {
  72. return nil, errors.New(resp.Status())
  73. }
  74. return &Response{response: resp}, nil
  75. }
  76. type Response struct {
  77. response *resty.Response
  78. }
  79. func (response *Response) Body() []byte {
  80. return response.response.Body()
  81. }
  82. func (response *Response) Json(resp any) error {
  83. body := response.response.Body()
  84. if body == nil || len(body) == 0 {
  85. return errors.New("响应体为空")
  86. }
  87. return json.Unmarshal(body, resp)
  88. }