builder_request.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 FormHeadersFunc func(c *api.Context) (map[string]string, error)
  8. type FormBodyFunc func(c *api.Context) (any, error)
  9. type FormQueryParamsFunc func(c *api.Context) (map[string]string, error)
  10. type FormPathParamsFunc func(c *api.Context) (map[string]string, error)
  11. func DefaultFormHeadersFunc(c *api.Context) (map[string]string, error) {
  12. return c.GetHeaders(), nil
  13. }
  14. func DefaultFormBodyFunc(c *api.Context) (any, error) {
  15. return c.ReadBody()
  16. }
  17. func DefaultFormQueryParamsFunc(c *api.Context) (map[string]string, error) {
  18. return c.GetAllQueryParams(), nil
  19. }
  20. func DefaultFormPathParamsFunc(c *api.Context) (map[string]string, error) {
  21. return c.GetAllPathParams(), nil
  22. }
  23. type BuilderRequest interface {
  24. HttpMethod() string
  25. RequestUrl() string
  26. Request(c *api.Context, request *http_client.Request) error
  27. Response() *http_client.Response
  28. }
  29. type PostRequest struct {
  30. Url string
  31. Headers FormHeadersFunc
  32. Body FormBodyFunc
  33. response *http_client.Response
  34. }
  35. func (req *PostRequest) HttpMethod() string {
  36. return http.MethodPost
  37. }
  38. func (req *PostRequest) RequestUrl() string {
  39. return req.Url
  40. }
  41. func (req *PostRequest) Request(c *api.Context, request *http_client.Request) error {
  42. if req.Headers == nil {
  43. req.Headers = DefaultFormHeadersFunc
  44. }
  45. if req.Body == nil {
  46. req.Body = DefaultFormBodyFunc
  47. }
  48. headers, err := req.Headers(c)
  49. if err != nil {
  50. return err
  51. }
  52. body, err := req.Body(c)
  53. if err != nil {
  54. return err
  55. }
  56. response, err := request.Post(req.Url, body,
  57. http_client.WithRequestHeaders(headers))
  58. if err != nil {
  59. return err
  60. }
  61. req.response = response
  62. return nil
  63. }
  64. func (req *PostRequest) Response() *http_client.Response {
  65. return req.response
  66. }
  67. type DeleteRequest struct {
  68. Url string
  69. Headers FormHeadersFunc
  70. PathParams FormPathParamsFunc
  71. QueryParams FormQueryParamsFunc
  72. response *http_client.Response
  73. }
  74. func (req *DeleteRequest) HttpMethod() string {
  75. return http.MethodDelete
  76. }
  77. func (req *DeleteRequest) RequestUrl() string {
  78. return req.Url
  79. }
  80. func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request) error {
  81. if req.Headers == nil {
  82. req.Headers = DefaultFormHeadersFunc
  83. }
  84. if req.QueryParams == nil {
  85. req.QueryParams = DefaultFormQueryParamsFunc
  86. }
  87. if req.PathParams == nil {
  88. req.PathParams = DefaultFormPathParamsFunc
  89. }
  90. headers, err := req.Headers(c)
  91. if err != nil {
  92. return err
  93. }
  94. queryParams, err := req.QueryParams(c)
  95. if err != nil {
  96. return err
  97. }
  98. pathParams, err := req.PathParams(c)
  99. if err != nil {
  100. return err
  101. }
  102. response, err := request.Delete(req.Url,
  103. http_client.WithRequestHeaders(headers),
  104. http_client.WithRequestPathParams(pathParams),
  105. http_client.WithRequestQueryParams(queryParams))
  106. if err != nil {
  107. return err
  108. }
  109. req.response = response
  110. return nil
  111. }
  112. func (req *DeleteRequest) Response() *http_client.Response {
  113. return req.response
  114. }
  115. type PutRequest struct {
  116. Url string
  117. Headers FormHeadersFunc
  118. Body FormBodyFunc
  119. response *http_client.Response
  120. }
  121. func (req *PutRequest) HttpMethod() string {
  122. return http.MethodPut
  123. }
  124. func (req *PutRequest) RequestUrl() string {
  125. return req.Url
  126. }
  127. func (req *PutRequest) Request(c *api.Context, request *http_client.Request) error {
  128. if req.Headers == nil {
  129. req.Headers = DefaultFormHeadersFunc
  130. }
  131. if req.Body == nil {
  132. req.Body = DefaultFormBodyFunc
  133. }
  134. headers, err := req.Headers(c)
  135. if err != nil {
  136. return err
  137. }
  138. body, err := req.Body(c)
  139. if err != nil {
  140. return err
  141. }
  142. response, err := request.Put(req.Url, body,
  143. http_client.WithRequestHeaders(headers))
  144. if err != nil {
  145. return err
  146. }
  147. req.response = response
  148. return nil
  149. }
  150. func (req *PutRequest) Response() *http_client.Response {
  151. return req.response
  152. }
  153. type GetRequest struct {
  154. Url string
  155. Headers FormHeadersFunc
  156. PathParams FormPathParamsFunc
  157. QueryParams FormQueryParamsFunc
  158. response *http_client.Response
  159. }
  160. func (req *GetRequest) HttpMethod() string {
  161. return http.MethodGet
  162. }
  163. func (req *GetRequest) RequestUrl() string {
  164. return req.Url
  165. }
  166. func (req *GetRequest) Request(c *api.Context, request *http_client.Request) error {
  167. if req.Headers == nil {
  168. req.Headers = DefaultFormHeadersFunc
  169. }
  170. if req.QueryParams == nil {
  171. req.QueryParams = DefaultFormQueryParamsFunc
  172. }
  173. if req.PathParams == nil {
  174. req.PathParams = DefaultFormPathParamsFunc
  175. }
  176. headers, err := req.Headers(c)
  177. if err != nil {
  178. return err
  179. }
  180. queryParams, err := req.QueryParams(c)
  181. if err != nil {
  182. return err
  183. }
  184. pathParams, err := req.PathParams(c)
  185. if err != nil {
  186. return err
  187. }
  188. response, err := request.Get(req.Url,
  189. http_client.WithRequestHeaders(headers),
  190. http_client.WithRequestPathParams(pathParams),
  191. http_client.WithRequestQueryParams(queryParams))
  192. if err != nil {
  193. return err
  194. }
  195. req.response = response
  196. return nil
  197. }
  198. func (req *GetRequest) Response() *http_client.Response {
  199. return req.response
  200. }