builder_request.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package gateway
  2. import (
  3. "git.sxidc.com/go-tools/utils/http_client"
  4. "net/http"
  5. )
  6. type BuilderRequest interface {
  7. HttpMethod() string
  8. Request(request *http_client.Request) error
  9. Response() *http_client.Response
  10. }
  11. type PostRequest struct {
  12. Url string
  13. Headers map[string]string
  14. Body any
  15. response *http_client.Response
  16. }
  17. func (req *PostRequest) HttpMethod() string {
  18. return http.MethodPost
  19. }
  20. func (req *PostRequest) RequestUrl() string {
  21. return req.Url
  22. }
  23. func (req *PostRequest) Request(request *http_client.Request) error {
  24. response, err := request.Post(req.Url, req.Body,
  25. http_client.WithRequestHeaders(req.Headers))
  26. if err != nil {
  27. return err
  28. }
  29. req.response = response
  30. return nil
  31. }
  32. func (req *PostRequest) Response() *http_client.Response {
  33. return req.response
  34. }
  35. type DeleteRequest struct {
  36. Url string
  37. Headers map[string]string
  38. PathParams map[string]string
  39. QueryParams map[string]string
  40. response *http_client.Response
  41. }
  42. func (req *DeleteRequest) HttpMethod() string {
  43. return http.MethodDelete
  44. }
  45. func (req *DeleteRequest) Request(request *http_client.Request) error {
  46. response, err := request.Delete(req.Url,
  47. http_client.WithRequestHeaders(req.Headers),
  48. http_client.WithRequestPathParams(req.PathParams),
  49. http_client.WithRequestQueryParams(req.QueryParams))
  50. if err != nil {
  51. return err
  52. }
  53. req.response = response
  54. return nil
  55. }
  56. func (req *DeleteRequest) Response() *http_client.Response {
  57. return req.response
  58. }
  59. type PutRequest struct {
  60. // 静态配置
  61. Url string
  62. Headers map[string]string
  63. Body any
  64. response *http_client.Response
  65. }
  66. func (req *PutRequest) HttpMethod() string {
  67. return http.MethodPut
  68. }
  69. func (req *PutRequest) RequestUrl() string {
  70. return req.Url
  71. }
  72. func (req *PutRequest) Request(request *http_client.Request) error {
  73. response, err := request.Put(req.Url, req.Body,
  74. http_client.WithRequestHeaders(req.Headers))
  75. if err != nil {
  76. return err
  77. }
  78. req.response = response
  79. return nil
  80. }
  81. func (req *PutRequest) Response() *http_client.Response {
  82. return req.response
  83. }
  84. type GetRequest struct {
  85. Url string
  86. Headers map[string]string
  87. PathParams map[string]string
  88. QueryParams map[string]string
  89. response *http_client.Response
  90. }
  91. func (req *GetRequest) HttpMethod() string {
  92. return http.MethodGet
  93. }
  94. func (req *GetRequest) RequestUrl() string {
  95. return req.Url
  96. }
  97. func (req *GetRequest) Request(request *http_client.Request) error {
  98. response, err := request.Get(req.Url,
  99. http_client.WithRequestHeaders(req.Headers),
  100. http_client.WithRequestPathParams(req.PathParams),
  101. http_client.WithRequestQueryParams(req.QueryParams))
  102. if err != nil {
  103. return err
  104. }
  105. req.response = response
  106. return nil
  107. }
  108. func (req *GetRequest) Response() *http_client.Response {
  109. return req.response
  110. }