builder_request.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package gateway
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-tools/utils/http_client"
  5. "net/http"
  6. )
  7. type BuilderRequest interface {
  8. HttpMethod() string
  9. RequestUrl() string
  10. Request(c *api.Context, request *http_client.Request) error
  11. Response() *http_client.Response
  12. }
  13. type PostRequest struct {
  14. Url string
  15. Headers map[string]string
  16. Body any
  17. response *http_client.Response
  18. }
  19. func (req *PostRequest) HttpMethod() string {
  20. return http.MethodPost
  21. }
  22. func (req *PostRequest) RequestUrl() string {
  23. return req.Url
  24. }
  25. func (req *PostRequest) Request(c *api.Context, request *http_client.Request) error {
  26. if req.Body == nil {
  27. body, err := c.ReadBody()
  28. if err != nil {
  29. return err
  30. }
  31. req.Body = body
  32. }
  33. response, err := request.Post(req.Url, req.Body,
  34. http_client.WithRequestHeaders(req.Headers))
  35. if err != nil {
  36. return err
  37. }
  38. req.response = response
  39. return nil
  40. }
  41. func (req *PostRequest) Response() *http_client.Response {
  42. return req.response
  43. }
  44. type DeleteRequest struct {
  45. Url string
  46. Headers map[string]string
  47. PathParams map[string]string
  48. QueryParams map[string]string
  49. response *http_client.Response
  50. }
  51. func (req *DeleteRequest) HttpMethod() string {
  52. return http.MethodDelete
  53. }
  54. func (req *DeleteRequest) RequestUrl() string {
  55. return req.Url
  56. }
  57. func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request) error {
  58. if req.QueryParams == nil || len(req.QueryParams) == 0 {
  59. req.QueryParams = c.GetAllQueryParams()
  60. }
  61. if req.PathParams == nil || len(req.PathParams) == 0 {
  62. req.PathParams = c.GetAllPathParams()
  63. }
  64. response, err := request.Delete(req.Url,
  65. http_client.WithRequestHeaders(req.Headers),
  66. http_client.WithRequestPathParams(req.PathParams),
  67. http_client.WithRequestQueryParams(req.QueryParams))
  68. if err != nil {
  69. return err
  70. }
  71. req.response = response
  72. return nil
  73. }
  74. func (req *DeleteRequest) Response() *http_client.Response {
  75. return req.response
  76. }
  77. type PutRequest struct {
  78. Url string
  79. Headers map[string]string
  80. Body []byte
  81. response *http_client.Response
  82. }
  83. func (req *PutRequest) HttpMethod() string {
  84. return http.MethodPut
  85. }
  86. func (req *PutRequest) RequestUrl() string {
  87. return req.Url
  88. }
  89. func (req *PutRequest) Request(c *api.Context, request *http_client.Request) error {
  90. if req.Body == nil {
  91. body, err := c.ReadBody()
  92. if err != nil {
  93. return err
  94. }
  95. req.Body = body
  96. }
  97. response, err := request.Put(req.Url, req.Body,
  98. http_client.WithRequestHeaders(req.Headers))
  99. if err != nil {
  100. return err
  101. }
  102. req.response = response
  103. return nil
  104. }
  105. func (req *PutRequest) Response() *http_client.Response {
  106. return req.response
  107. }
  108. type GetRequest struct {
  109. Url string
  110. Headers map[string]string
  111. PathParams map[string]string
  112. QueryParams map[string]string
  113. response *http_client.Response
  114. }
  115. func (req *GetRequest) HttpMethod() string {
  116. return http.MethodGet
  117. }
  118. func (req *GetRequest) RequestUrl() string {
  119. return req.Url
  120. }
  121. func (req *GetRequest) Request(c *api.Context, request *http_client.Request) error {
  122. if req.QueryParams == nil || len(req.QueryParams) == 0 {
  123. req.QueryParams = c.GetAllQueryParams()
  124. }
  125. if req.PathParams == nil || len(req.PathParams) == 0 {
  126. req.PathParams = c.GetAllPathParams()
  127. }
  128. response, err := request.Get(req.Url,
  129. http_client.WithRequestHeaders(req.Headers),
  130. http_client.WithRequestPathParams(req.PathParams),
  131. http_client.WithRequestQueryParams(req.QueryParams))
  132. if err != nil {
  133. return err
  134. }
  135. req.response = response
  136. return nil
  137. }
  138. func (req *GetRequest) Response() *http_client.Response {
  139. return req.response
  140. }