builder_request.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. type UrlFormFunc func(c *api.Context, staticUrl string, historyRequests []BuilderRequest, resultMap map[string]any) (string, error)
  7. type HeadersFormFunc func(c *api.Context, staticHeaders map[string]string, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error)
  8. type BodyFormFunc func(c *api.Context, staticBody any, historyRequests []BuilderRequest, resultMap map[string]any) (any, error)
  9. type QueryParamsFormFunc func(c *api.Context, staticQueryParams map[string]string, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error)
  10. type PathParamsFormFunc func(c *api.Context, staticPathParams map[string]string, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error)
  11. func FormJsonBodyWithTenantIDAndUserIDFunc(tenantIDFieldName string, userIDFieldName string) BodyFormFunc {
  12. return func(c *api.Context, staticBody any, historyRequests []BuilderRequest, resultMap map[string]any) (any, error) {
  13. err := AddJsonBodyTenantIDAndUserID(c, tenantIDFieldName, userIDFieldName)
  14. if err != nil {
  15. return nil, err
  16. }
  17. jsonBody, err := c.GetJsonBody()
  18. if err != nil {
  19. return nil, err
  20. }
  21. return jsonBody.Map(), nil
  22. }
  23. }
  24. func FormQueryParamsWithTenantIDAndUserIDFunc(tenantIDFieldName string, userIDFieldName string) QueryParamsFormFunc {
  25. return func(c *api.Context, staticQueryParams map[string]string, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error) {
  26. err := AddQueryParamsTenantIDAndUserID(c, tenantIDFieldName, userIDFieldName)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return c.GetQueryParams().Map(), nil
  31. }
  32. }
  33. type BuilderRequest interface {
  34. Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error)
  35. Response() *http_client.Response
  36. }
  37. type PostRequest struct {
  38. Url string
  39. Body any
  40. UrlFormFunc UrlFormFunc
  41. BodyFormFunc BodyFormFunc
  42. HeadersFormFunc HeadersFormFunc
  43. headers map[string]string
  44. response *http_client.Response
  45. }
  46. func (req *PostRequest) Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
  47. preparedRequest, err := req.prepare(c, historyRequests, resultMap)
  48. if err != nil {
  49. return nil, err
  50. }
  51. response, err := request.Post(preparedRequest.Url, preparedRequest.Body,
  52. http_client.WithRequestHeaders(preparedRequest.headers))
  53. if err != nil {
  54. return nil, err
  55. }
  56. preparedRequest.response = response
  57. return preparedRequest, nil
  58. }
  59. func (req *PostRequest) Response() *http_client.Response {
  60. return req.response
  61. }
  62. func (req *PostRequest) prepare(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (*PostRequest, error) {
  63. preparedRequest := &PostRequest{
  64. Url: req.Url,
  65. Body: req.Body,
  66. UrlFormFunc: req.UrlFormFunc,
  67. HeadersFormFunc: req.HeadersFormFunc,
  68. BodyFormFunc: req.BodyFormFunc,
  69. }
  70. // 赋值默认body
  71. if req.Body == nil {
  72. cacheBody, err := c.GetBytesBody()
  73. if err != nil {
  74. return nil, err
  75. }
  76. preparedRequest.Body = cacheBody.Bytes()
  77. }
  78. // 赋值默认headers
  79. preparedRequest.headers = c.GetHeader().Map()
  80. // 构造url
  81. if preparedRequest.UrlFormFunc != nil {
  82. formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
  83. if err != nil {
  84. return nil, err
  85. }
  86. preparedRequest.Url = formedUrl
  87. }
  88. // 构造body
  89. if preparedRequest.BodyFormFunc != nil {
  90. formedBody, err := preparedRequest.BodyFormFunc(c, preparedRequest.Body, historyRequests, resultMap)
  91. if err != nil {
  92. return nil, err
  93. }
  94. preparedRequest.Body = formedBody
  95. }
  96. // 构造headers
  97. if preparedRequest.HeadersFormFunc != nil {
  98. formedHeaders, err := preparedRequest.HeadersFormFunc(c, preparedRequest.headers, historyRequests, resultMap)
  99. if err != nil {
  100. return nil, err
  101. }
  102. preparedRequest.headers = formedHeaders
  103. }
  104. return preparedRequest, nil
  105. }
  106. type DeleteRequest struct {
  107. Url string
  108. QueryParams map[string]string
  109. PathParams map[string]string
  110. UrlFormFunc UrlFormFunc
  111. HeadersFormFunc HeadersFormFunc
  112. QueryParamsFormFunc QueryParamsFormFunc
  113. PathParamsFormFunc PathParamsFormFunc
  114. headers map[string]string
  115. response *http_client.Response
  116. }
  117. func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
  118. preparedRequest, err := req.prepare(c, historyRequests, resultMap)
  119. if err != nil {
  120. return nil, err
  121. }
  122. response, err := request.Delete(preparedRequest.Url,
  123. http_client.WithRequestQueryParams(preparedRequest.QueryParams),
  124. http_client.WithRequestPathParams(preparedRequest.PathParams),
  125. http_client.WithRequestHeaders(preparedRequest.headers))
  126. if err != nil {
  127. return nil, err
  128. }
  129. preparedRequest.response = response
  130. return preparedRequest, nil
  131. }
  132. func (req *DeleteRequest) Response() *http_client.Response {
  133. return req.response
  134. }
  135. func (req *DeleteRequest) prepare(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (*DeleteRequest, error) {
  136. preparedRequest := &DeleteRequest{
  137. Url: req.Url,
  138. QueryParams: req.QueryParams,
  139. PathParams: req.PathParams,
  140. UrlFormFunc: req.UrlFormFunc,
  141. HeadersFormFunc: req.HeadersFormFunc,
  142. QueryParamsFormFunc: req.QueryParamsFormFunc,
  143. PathParamsFormFunc: req.PathParamsFormFunc,
  144. }
  145. // 赋值默认查询参数
  146. if req.QueryParams == nil || len(req.QueryParams) == 0 {
  147. req.QueryParams = c.GetQueryParams().Map()
  148. }
  149. // 赋值默认路径参数
  150. if req.PathParams == nil || len(req.PathParams) == 0 {
  151. req.PathParams = c.GetPathParams().Map()
  152. }
  153. // 赋值默认headers
  154. preparedRequest.headers = c.GetHeader().Map()
  155. // 构造url
  156. if preparedRequest.UrlFormFunc != nil {
  157. formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
  158. if err != nil {
  159. return nil, err
  160. }
  161. preparedRequest.Url = formedUrl
  162. }
  163. // 构造查询参数
  164. if preparedRequest.QueryParamsFormFunc != nil {
  165. formedQueryParams, err := preparedRequest.QueryParamsFormFunc(c, preparedRequest.QueryParams, historyRequests, resultMap)
  166. if err != nil {
  167. return nil, err
  168. }
  169. preparedRequest.QueryParams = formedQueryParams
  170. }
  171. // 构造路径参数
  172. if preparedRequest.PathParamsFormFunc != nil {
  173. formedPathParams, err := preparedRequest.PathParamsFormFunc(c, preparedRequest.PathParams, historyRequests, resultMap)
  174. if err != nil {
  175. return nil, err
  176. }
  177. preparedRequest.PathParams = formedPathParams
  178. }
  179. // 构造headers
  180. if preparedRequest.HeadersFormFunc != nil {
  181. formedHeaders, err := preparedRequest.HeadersFormFunc(c, preparedRequest.headers, historyRequests, resultMap)
  182. if err != nil {
  183. return nil, err
  184. }
  185. preparedRequest.headers = formedHeaders
  186. }
  187. return preparedRequest, nil
  188. }
  189. type PutRequest struct {
  190. Url string
  191. Body any
  192. UrlFormFunc UrlFormFunc
  193. BodyFormFunc BodyFormFunc
  194. HeadersFormFunc HeadersFormFunc
  195. headers map[string]string
  196. response *http_client.Response
  197. }
  198. func (req *PutRequest) Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
  199. preparedRequest, err := req.prepare(c, historyRequests, resultMap)
  200. if err != nil {
  201. return nil, err
  202. }
  203. response, err := request.Put(preparedRequest.Url, preparedRequest.Body,
  204. http_client.WithRequestHeaders(preparedRequest.headers))
  205. if err != nil {
  206. return nil, err
  207. }
  208. preparedRequest.response = response
  209. return preparedRequest, nil
  210. }
  211. func (req *PutRequest) Response() *http_client.Response {
  212. return req.response
  213. }
  214. func (req *PutRequest) prepare(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (*PutRequest, error) {
  215. preparedRequest := &PutRequest{
  216. Url: req.Url,
  217. Body: req.Body,
  218. UrlFormFunc: req.UrlFormFunc,
  219. HeadersFormFunc: req.HeadersFormFunc,
  220. BodyFormFunc: req.BodyFormFunc,
  221. }
  222. // 赋值默认body
  223. if req.Body == nil {
  224. cacheBody, err := c.GetBytesBody()
  225. if err != nil {
  226. return nil, err
  227. }
  228. preparedRequest.Body = cacheBody.Bytes()
  229. }
  230. // 赋值默认headers
  231. preparedRequest.headers = c.GetHeader().Map()
  232. // 构造url
  233. if preparedRequest.UrlFormFunc != nil {
  234. formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
  235. if err != nil {
  236. return nil, err
  237. }
  238. preparedRequest.Url = formedUrl
  239. }
  240. // 构造body
  241. if preparedRequest.BodyFormFunc != nil {
  242. formedBody, err := preparedRequest.BodyFormFunc(c, preparedRequest.Body, historyRequests, resultMap)
  243. if err != nil {
  244. return nil, err
  245. }
  246. preparedRequest.Body = formedBody
  247. }
  248. // 构造headers
  249. if preparedRequest.HeadersFormFunc != nil {
  250. formedHeaders, err := preparedRequest.HeadersFormFunc(c, preparedRequest.headers, historyRequests, resultMap)
  251. if err != nil {
  252. return nil, err
  253. }
  254. preparedRequest.headers = formedHeaders
  255. }
  256. return preparedRequest, nil
  257. }
  258. type GetRequest struct {
  259. Url string
  260. QueryParams map[string]string
  261. PathParams map[string]string
  262. UrlFormFunc UrlFormFunc
  263. HeadersFormFunc HeadersFormFunc
  264. QueryParamsFormFunc QueryParamsFormFunc
  265. PathParamsFormFunc PathParamsFormFunc
  266. headers map[string]string
  267. response *http_client.Response
  268. }
  269. func (req *GetRequest) Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
  270. preparedRequest, err := req.prepare(c, historyRequests, resultMap)
  271. if err != nil {
  272. return nil, err
  273. }
  274. response, err := request.Get(preparedRequest.Url,
  275. http_client.WithRequestQueryParams(preparedRequest.QueryParams),
  276. http_client.WithRequestPathParams(preparedRequest.PathParams),
  277. http_client.WithRequestHeaders(preparedRequest.headers))
  278. if err != nil {
  279. return nil, err
  280. }
  281. preparedRequest.response = response
  282. return preparedRequest, nil
  283. }
  284. func (req *GetRequest) Response() *http_client.Response {
  285. return req.response
  286. }
  287. func (req *GetRequest) prepare(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (*DeleteRequest, error) {
  288. preparedRequest := &DeleteRequest{
  289. Url: req.Url,
  290. QueryParams: req.QueryParams,
  291. PathParams: req.PathParams,
  292. UrlFormFunc: req.UrlFormFunc,
  293. HeadersFormFunc: req.HeadersFormFunc,
  294. QueryParamsFormFunc: req.QueryParamsFormFunc,
  295. PathParamsFormFunc: req.PathParamsFormFunc,
  296. }
  297. // 赋值默认查询参数
  298. if req.QueryParams == nil || len(req.QueryParams) == 0 {
  299. req.QueryParams = c.GetQueryParams().Map()
  300. }
  301. // 赋值默认路径参数
  302. if req.PathParams == nil || len(req.PathParams) == 0 {
  303. req.PathParams = c.GetPathParams().Map()
  304. }
  305. // 赋值默认headers
  306. preparedRequest.headers = c.GetHeader().Map()
  307. // 构造url
  308. if preparedRequest.UrlFormFunc != nil {
  309. formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
  310. if err != nil {
  311. return nil, err
  312. }
  313. preparedRequest.Url = formedUrl
  314. }
  315. // 构造查询参数
  316. if preparedRequest.QueryParamsFormFunc != nil {
  317. formedQueryParams, err := preparedRequest.QueryParamsFormFunc(c, preparedRequest.QueryParams, historyRequests, resultMap)
  318. if err != nil {
  319. return nil, err
  320. }
  321. preparedRequest.QueryParams = formedQueryParams
  322. }
  323. // 构造路径参数
  324. if preparedRequest.PathParamsFormFunc != nil {
  325. formedPathParams, err := preparedRequest.PathParamsFormFunc(c, preparedRequest.PathParams, historyRequests, resultMap)
  326. if err != nil {
  327. return nil, err
  328. }
  329. preparedRequest.PathParams = formedPathParams
  330. }
  331. // 构造headers
  332. if preparedRequest.HeadersFormFunc != nil {
  333. formedHeaders, err := preparedRequest.HeadersFormFunc(c, preparedRequest.headers, historyRequests, resultMap)
  334. if err != nil {
  335. return nil, err
  336. }
  337. preparedRequest.headers = formedHeaders
  338. }
  339. return preparedRequest, nil
  340. }