builder_request.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. )
  6. // Request 请求接口
  7. type Request interface {
  8. // Request 发送请求
  9. // 参数:
  10. // - c: 上下文
  11. // - request: HTTP客户端请求,用来发送HTTP请求
  12. // 返回值:
  13. // - 带有响应的请求接口
  14. // - 错误
  15. Request(c *api.Context, request *http_client.Request) (Request, error)
  16. // Response 获取响应
  17. // 参数: 无
  18. // 返回值:
  19. // - HTTP客户端响应
  20. Response() *http_client.Response
  21. }
  22. // PostRequest POST请求
  23. type PostRequest struct {
  24. // Url 要发送请求的完整URL
  25. Url string
  26. // Body请求体,如果为nil,则默认读取RequestBuilder中上下文中的缓存Body
  27. Body any
  28. headers map[string]string
  29. response *http_client.Response
  30. }
  31. func (req *PostRequest) Request(c *api.Context, request *http_client.Request) (Request, error) {
  32. preparedRequest, err := req.prepare(c)
  33. if err != nil {
  34. return nil, err
  35. }
  36. response, err := request.Post(preparedRequest.Url, preparedRequest.Body,
  37. http_client.WithRequestHeaders(preparedRequest.headers))
  38. if err != nil {
  39. return nil, err
  40. }
  41. preparedRequest.response = response
  42. return preparedRequest, nil
  43. }
  44. func (req *PostRequest) Response() *http_client.Response {
  45. return req.response
  46. }
  47. func (req *PostRequest) prepare(c *api.Context) (*PostRequest, error) {
  48. preparedRequest := &PostRequest{
  49. Url: req.Url,
  50. Body: req.Body,
  51. }
  52. // 赋值默认body
  53. if preparedRequest.Body == nil {
  54. cacheBody, err := c.GetBytesBody()
  55. if err != nil {
  56. return nil, err
  57. }
  58. preparedRequest.Body = cacheBody.Bytes()
  59. }
  60. // 赋值默认headers
  61. preparedRequest.headers = c.GetHeader().Map()
  62. return preparedRequest, nil
  63. }
  64. // DeleteRequest DELETE请求
  65. type DeleteRequest struct {
  66. // Url 要发送请求的完整URL
  67. Url string
  68. // 查询参数,如果为nil,则默认读取RequestBuilder中上下文中的缓存查询参数
  69. QueryParams map[string]string
  70. // 路径参数,如果为nil,则默认读取RequestBuilder中上下文中的缓存路径参数
  71. PathParams map[string]string
  72. headers map[string]string
  73. response *http_client.Response
  74. }
  75. func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request) (Request, error) {
  76. preparedRequest, err := req.prepare(c)
  77. if err != nil {
  78. return nil, err
  79. }
  80. response, err := request.Delete(preparedRequest.Url,
  81. http_client.WithRequestQueryParams(preparedRequest.QueryParams),
  82. http_client.WithRequestPathParams(preparedRequest.PathParams),
  83. http_client.WithRequestHeaders(preparedRequest.headers))
  84. if err != nil {
  85. return nil, err
  86. }
  87. preparedRequest.response = response
  88. return preparedRequest, nil
  89. }
  90. func (req *DeleteRequest) Response() *http_client.Response {
  91. return req.response
  92. }
  93. func (req *DeleteRequest) prepare(c *api.Context) (*DeleteRequest, error) {
  94. preparedRequest := &DeleteRequest{
  95. Url: req.Url,
  96. QueryParams: req.QueryParams,
  97. PathParams: req.PathParams,
  98. }
  99. // 赋值默认查询参数
  100. if preparedRequest.QueryParams == nil || len(preparedRequest.QueryParams) == 0 {
  101. preparedRequest.QueryParams = c.GetQueryParams().Map()
  102. }
  103. // 赋值默认路径参数
  104. if preparedRequest.PathParams == nil || len(preparedRequest.PathParams) == 0 {
  105. preparedRequest.PathParams = c.GetPathParams().Map()
  106. }
  107. // 赋值默认headers
  108. preparedRequest.headers = c.GetHeader().Map()
  109. return preparedRequest, nil
  110. }
  111. // PutRequest PUT请求
  112. type PutRequest struct {
  113. // Url 要发送请求的完整URL
  114. Url string
  115. // Body请求体,如果为nil,则默认读取RequestBuilder中上下文中的缓存Body
  116. Body any
  117. headers map[string]string
  118. response *http_client.Response
  119. }
  120. func (req *PutRequest) Request(c *api.Context, request *http_client.Request) (Request, error) {
  121. preparedRequest, err := req.prepare(c)
  122. if err != nil {
  123. return nil, err
  124. }
  125. response, err := request.Put(preparedRequest.Url, preparedRequest.Body,
  126. http_client.WithRequestHeaders(preparedRequest.headers))
  127. if err != nil {
  128. return nil, err
  129. }
  130. preparedRequest.response = response
  131. return preparedRequest, nil
  132. }
  133. func (req *PutRequest) Response() *http_client.Response {
  134. return req.response
  135. }
  136. func (req *PutRequest) prepare(c *api.Context) (*PutRequest, error) {
  137. preparedRequest := &PutRequest{
  138. Url: req.Url,
  139. Body: req.Body,
  140. }
  141. // 赋值默认body
  142. if preparedRequest.Body == nil {
  143. cacheBody, err := c.GetBytesBody()
  144. if err != nil {
  145. return nil, err
  146. }
  147. preparedRequest.Body = cacheBody.Bytes()
  148. }
  149. // 赋值默认headers
  150. preparedRequest.headers = c.GetHeader().Map()
  151. return preparedRequest, nil
  152. }
  153. // GetRequest GET请求
  154. type GetRequest struct {
  155. // Url 要发送请求的完整URL
  156. Url string
  157. // 查询参数,如果为nil,则默认读取RequestBuilder中上下文中的缓存查询参数
  158. QueryParams map[string]string
  159. // 路径参数,如果为nil,则默认读取RequestBuilder中上下文中的缓存路径参数
  160. PathParams map[string]string
  161. headers map[string]string
  162. response *http_client.Response
  163. }
  164. func (req *GetRequest) Request(c *api.Context, request *http_client.Request) (Request, error) {
  165. preparedRequest, err := req.prepare(c)
  166. if err != nil {
  167. return nil, err
  168. }
  169. response, err := request.Get(preparedRequest.Url,
  170. http_client.WithRequestQueryParams(preparedRequest.QueryParams),
  171. http_client.WithRequestPathParams(preparedRequest.PathParams),
  172. http_client.WithRequestHeaders(preparedRequest.headers))
  173. if err != nil {
  174. return nil, err
  175. }
  176. preparedRequest.response = response
  177. return preparedRequest, nil
  178. }
  179. func (req *GetRequest) Response() *http_client.Response {
  180. return req.response
  181. }
  182. func (req *GetRequest) prepare(c *api.Context) (*GetRequest, error) {
  183. preparedRequest := &GetRequest{
  184. Url: req.Url,
  185. QueryParams: req.QueryParams,
  186. PathParams: req.PathParams,
  187. }
  188. // 赋值默认查询参数
  189. if preparedRequest.QueryParams == nil || len(preparedRequest.QueryParams) == 0 {
  190. preparedRequest.QueryParams = c.GetQueryParams().Map()
  191. }
  192. // 赋值默认路径参数
  193. if preparedRequest.PathParams == nil || len(preparedRequest.PathParams) == 0 {
  194. preparedRequest.PathParams = c.GetPathParams().Map()
  195. }
  196. // 赋值默认headers
  197. preparedRequest.headers = c.GetHeader().Map()
  198. return preparedRequest, nil
  199. }