builder_request.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 UrlTransferFunc func(c *api.Context, url string, historyRequest []BuilderRequest, resultMap map[string]any) (string, error)
  7. type HeadersFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error)
  8. type BodyFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (any, error)
  9. type QueryParamsFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error)
  10. type PathParamsFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error)
  11. func FormJsonBodyWithTenantIDAndUserIDFunc(tenantIDFieldName string, userIDFieldName string) BodyFormFunc {
  12. return func(c *api.Context, historyRequest []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, historyRequest []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, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error)
  35. Response() *http_client.Response
  36. }
  37. type PostRequestOption func(options *PostRequestOptions)
  38. type PostRequestOptions struct {
  39. urlTransferFunc UrlTransferFunc
  40. headersFormFunc HeadersFormFunc
  41. bodyFormFunc BodyFormFunc
  42. }
  43. func PostRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) PostRequestOption {
  44. return func(options *PostRequestOptions) {
  45. options.urlTransferFunc = urlTransferFunc
  46. }
  47. }
  48. func PostRequestWithHeadersForm(headersFormFunc HeadersFormFunc) PostRequestOption {
  49. return func(options *PostRequestOptions) {
  50. options.headersFormFunc = headersFormFunc
  51. }
  52. }
  53. func PostRequestWithBodyForm(bodyFormFunc BodyFormFunc) PostRequestOption {
  54. return func(options *PostRequestOptions) {
  55. options.bodyFormFunc = bodyFormFunc
  56. }
  57. }
  58. type PostRequest struct {
  59. url string
  60. response *http_client.Response
  61. options *PostRequestOptions
  62. }
  63. func NewPostRequest(url string, opts ...PostRequestOption) *PostRequest {
  64. options := new(PostRequestOptions)
  65. for _, opt := range opts {
  66. opt(options)
  67. }
  68. if options.urlTransferFunc == nil {
  69. options.urlTransferFunc = defaultUrlTransferFunc
  70. }
  71. if options.headersFormFunc == nil {
  72. options.headersFormFunc = defaultHeadersFormFunc
  73. }
  74. if options.bodyFormFunc == nil {
  75. options.bodyFormFunc = defaultBodyFormFunc
  76. }
  77. return &PostRequest{
  78. url: url,
  79. options: options,
  80. }
  81. }
  82. func (req *PostRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
  83. url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap)
  84. if err != nil {
  85. return nil, err
  86. }
  87. headers, err := req.options.headersFormFunc(c, historyRequest, resultMap)
  88. if err != nil {
  89. return nil, err
  90. }
  91. body, err := req.options.bodyFormFunc(c, historyRequest, resultMap)
  92. if err != nil {
  93. return nil, err
  94. }
  95. response, err := request.Post(url, body, http_client.WithRequestHeaders(headers))
  96. if err != nil {
  97. return nil, err
  98. }
  99. return &PostRequest{
  100. url: url,
  101. response: response,
  102. options: &PostRequestOptions{
  103. urlTransferFunc: req.options.urlTransferFunc,
  104. headersFormFunc: req.options.headersFormFunc,
  105. bodyFormFunc: req.options.bodyFormFunc,
  106. },
  107. }, nil
  108. }
  109. func (req *PostRequest) Response() *http_client.Response {
  110. return req.response
  111. }
  112. type DeleteRequestOption func(options *DeleteRequestOptions)
  113. type DeleteRequestOptions struct {
  114. urlTransferFunc UrlTransferFunc
  115. headersFormFunc HeadersFormFunc
  116. pathParamsFormFunc PathParamsFormFunc
  117. queryParamsFormFunc QueryParamsFormFunc
  118. }
  119. func DeleteRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) DeleteRequestOption {
  120. return func(options *DeleteRequestOptions) {
  121. options.urlTransferFunc = urlTransferFunc
  122. }
  123. }
  124. func DeleteRequestWithHeadersForm(headersFormFunc HeadersFormFunc) DeleteRequestOption {
  125. return func(options *DeleteRequestOptions) {
  126. options.headersFormFunc = headersFormFunc
  127. }
  128. }
  129. func DeleteRequestWithPathParamsForm(pathParamsFormFunc PathParamsFormFunc) DeleteRequestOption {
  130. return func(options *DeleteRequestOptions) {
  131. options.pathParamsFormFunc = pathParamsFormFunc
  132. }
  133. }
  134. func DeleteRequestWithQueryParamsForm(queryParamsFormFunc QueryParamsFormFunc) DeleteRequestOption {
  135. return func(options *DeleteRequestOptions) {
  136. options.queryParamsFormFunc = queryParamsFormFunc
  137. }
  138. }
  139. type DeleteRequest struct {
  140. url string
  141. response *http_client.Response
  142. options *DeleteRequestOptions
  143. }
  144. func NewDeleteRequest(url string, opts ...DeleteRequestOption) *DeleteRequest {
  145. options := new(DeleteRequestOptions)
  146. for _, opt := range opts {
  147. opt(options)
  148. }
  149. if options.urlTransferFunc == nil {
  150. options.urlTransferFunc = defaultUrlTransferFunc
  151. }
  152. if options.headersFormFunc == nil {
  153. options.headersFormFunc = defaultHeadersFormFunc
  154. }
  155. if options.pathParamsFormFunc == nil {
  156. options.pathParamsFormFunc = defaultPathParamsFormFunc
  157. }
  158. if options.queryParamsFormFunc == nil {
  159. options.queryParamsFormFunc = defaultQueryParamsFormFunc
  160. }
  161. return &DeleteRequest{
  162. url: url,
  163. options: options,
  164. }
  165. }
  166. func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
  167. url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap)
  168. if err != nil {
  169. return nil, err
  170. }
  171. headers, err := req.options.headersFormFunc(c, historyRequest, resultMap)
  172. if err != nil {
  173. return nil, err
  174. }
  175. pathParams, err := req.options.pathParamsFormFunc(c, historyRequest, resultMap)
  176. if err != nil {
  177. return nil, err
  178. }
  179. queryParams, err := req.options.queryParamsFormFunc(c, historyRequest, resultMap)
  180. if err != nil {
  181. return nil, err
  182. }
  183. response, err := request.Delete(url,
  184. http_client.WithRequestHeaders(headers),
  185. http_client.WithRequestPathParams(pathParams),
  186. http_client.WithRequestQueryParams(queryParams))
  187. if err != nil {
  188. return nil, err
  189. }
  190. return &DeleteRequest{
  191. url: url,
  192. response: response,
  193. options: &DeleteRequestOptions{
  194. urlTransferFunc: req.options.urlTransferFunc,
  195. headersFormFunc: req.options.headersFormFunc,
  196. pathParamsFormFunc: req.options.pathParamsFormFunc,
  197. queryParamsFormFunc: req.options.queryParamsFormFunc,
  198. },
  199. }, nil
  200. }
  201. func (req *DeleteRequest) Response() *http_client.Response {
  202. return req.response
  203. }
  204. type PutRequestOption func(options *PutRequestOptions)
  205. type PutRequestOptions struct {
  206. urlTransferFunc UrlTransferFunc
  207. headersFormFunc HeadersFormFunc
  208. bodyFormFunc BodyFormFunc
  209. }
  210. func PutRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) PutRequestOption {
  211. return func(options *PutRequestOptions) {
  212. options.urlTransferFunc = urlTransferFunc
  213. }
  214. }
  215. func PutRequestWithHeadersForm(headersFormFunc HeadersFormFunc) PutRequestOption {
  216. return func(options *PutRequestOptions) {
  217. options.headersFormFunc = headersFormFunc
  218. }
  219. }
  220. func PutRequestWithBodyForm(bodyFormFunc BodyFormFunc) PutRequestOption {
  221. return func(options *PutRequestOptions) {
  222. options.bodyFormFunc = bodyFormFunc
  223. }
  224. }
  225. type PutRequest struct {
  226. url string
  227. response *http_client.Response
  228. options *PutRequestOptions
  229. }
  230. func NewPutRequest(url string, opts ...PutRequestOption) *PutRequest {
  231. options := new(PutRequestOptions)
  232. for _, opt := range opts {
  233. opt(options)
  234. }
  235. if options.urlTransferFunc == nil {
  236. options.urlTransferFunc = defaultUrlTransferFunc
  237. }
  238. if options.headersFormFunc == nil {
  239. options.headersFormFunc = defaultHeadersFormFunc
  240. }
  241. if options.bodyFormFunc == nil {
  242. options.bodyFormFunc = defaultBodyFormFunc
  243. }
  244. return &PutRequest{
  245. url: url,
  246. options: options,
  247. }
  248. }
  249. func (req *PutRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
  250. url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap)
  251. if err != nil {
  252. return nil, err
  253. }
  254. headers, err := req.options.headersFormFunc(c, historyRequest, resultMap)
  255. if err != nil {
  256. return nil, err
  257. }
  258. body, err := req.options.bodyFormFunc(c, historyRequest, resultMap)
  259. if err != nil {
  260. return nil, err
  261. }
  262. response, err := request.Put(url, body, http_client.WithRequestHeaders(headers))
  263. if err != nil {
  264. return nil, err
  265. }
  266. return &PutRequest{
  267. url: url,
  268. response: response,
  269. options: &PutRequestOptions{
  270. urlTransferFunc: req.options.urlTransferFunc,
  271. headersFormFunc: req.options.headersFormFunc,
  272. bodyFormFunc: req.options.bodyFormFunc,
  273. },
  274. }, nil
  275. }
  276. func (req *PutRequest) Response() *http_client.Response {
  277. return req.response
  278. }
  279. type GetRequestOption func(options *GetRequestOptions)
  280. type GetRequestOptions struct {
  281. urlTransferFunc UrlTransferFunc
  282. headersFormFunc HeadersFormFunc
  283. pathParamsFormFunc PathParamsFormFunc
  284. queryParamsFormFunc QueryParamsFormFunc
  285. }
  286. func GetRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) GetRequestOption {
  287. return func(options *GetRequestOptions) {
  288. options.urlTransferFunc = urlTransferFunc
  289. }
  290. }
  291. func GetRequestWithHeadersForm(headersFormFunc HeadersFormFunc) GetRequestOption {
  292. return func(options *GetRequestOptions) {
  293. options.headersFormFunc = headersFormFunc
  294. }
  295. }
  296. func GetRequestWithPathParamsForm(pathParamsFormFunc PathParamsFormFunc) GetRequestOption {
  297. return func(options *GetRequestOptions) {
  298. options.pathParamsFormFunc = pathParamsFormFunc
  299. }
  300. }
  301. func GetRequestWithQueryParamsForm(queryParamsFormFunc QueryParamsFormFunc) GetRequestOption {
  302. return func(options *GetRequestOptions) {
  303. options.queryParamsFormFunc = queryParamsFormFunc
  304. }
  305. }
  306. type GetRequest struct {
  307. url string
  308. headers map[string]string
  309. pathParams map[string]string
  310. queryParams map[string]string
  311. response *http_client.Response
  312. options *GetRequestOptions
  313. }
  314. func NewGetRequest(url string, opts ...GetRequestOption) *GetRequest {
  315. options := new(GetRequestOptions)
  316. for _, opt := range opts {
  317. opt(options)
  318. }
  319. if options.urlTransferFunc == nil {
  320. options.urlTransferFunc = defaultUrlTransferFunc
  321. }
  322. if options.headersFormFunc == nil {
  323. options.headersFormFunc = defaultHeadersFormFunc
  324. }
  325. if options.pathParamsFormFunc == nil {
  326. options.pathParamsFormFunc = defaultPathParamsFormFunc
  327. }
  328. if options.queryParamsFormFunc == nil {
  329. options.queryParamsFormFunc = defaultQueryParamsFormFunc
  330. }
  331. return &GetRequest{
  332. url: url,
  333. options: options,
  334. }
  335. }
  336. func (req *GetRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
  337. url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap)
  338. if err != nil {
  339. return nil, err
  340. }
  341. headers, err := req.options.headersFormFunc(c, historyRequest, resultMap)
  342. if err != nil {
  343. return nil, err
  344. }
  345. pathParams, err := req.options.pathParamsFormFunc(c, historyRequest, resultMap)
  346. if err != nil {
  347. return nil, err
  348. }
  349. queryParams, err := req.options.queryParamsFormFunc(c, historyRequest, resultMap)
  350. if err != nil {
  351. return nil, err
  352. }
  353. response, err := request.Get(url,
  354. http_client.WithRequestHeaders(headers),
  355. http_client.WithRequestPathParams(pathParams),
  356. http_client.WithRequestQueryParams(queryParams))
  357. if err != nil {
  358. return nil, err
  359. }
  360. return &GetRequest{
  361. url: url,
  362. headers: headers,
  363. pathParams: pathParams,
  364. queryParams: queryParams,
  365. response: response,
  366. options: &GetRequestOptions{
  367. urlTransferFunc: req.options.urlTransferFunc,
  368. headersFormFunc: req.options.headersFormFunc,
  369. pathParamsFormFunc: req.options.pathParamsFormFunc,
  370. queryParamsFormFunc: req.options.queryParamsFormFunc,
  371. },
  372. }, nil
  373. }
  374. func (req *GetRequest) Response() *http_client.Response {
  375. return req.response
  376. }
  377. func defaultUrlTransferFunc(_ *api.Context, url string, _ []BuilderRequest, _ map[string]any) (string, error) {
  378. return url, nil
  379. }
  380. func defaultHeadersFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) {
  381. return c.GetHeaders(), nil
  382. }
  383. func defaultBodyFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (any, error) {
  384. cachedBody, err := c.GetBytesBody()
  385. if err != nil {
  386. return nil, err
  387. }
  388. return cachedBody.Bytes(), nil
  389. }
  390. func defaultQueryParamsFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) {
  391. return c.GetQueryParams().Map(), nil
  392. }
  393. func defaultPathParamsFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) {
  394. return c.GetPathParams().Map(), nil
  395. }