builder_request.go 6.6 KB

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