pass_through.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. package pass_through
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/gateway"
  4. "git.sxidc.com/go-tools/utils/http_client"
  5. "git.sxidc.com/go-tools/utils/strutils"
  6. "net/http"
  7. )
  8. // PostRoute POST直传API
  9. // 参数:
  10. // - builder: 该网关API构建器
  11. // - params: 网关直通参数
  12. // - opts: 网关直通选项
  13. // 返回值: 无
  14. func PostRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  15. params.passThrough(builder, http.MethodPost, opts...)
  16. }
  17. // DeleteRoute DELETE直传API
  18. // 参数:
  19. // - builder: 该网关API构建器
  20. // - params: 网关直通参数
  21. // - opts: 网关直通选项
  22. // 返回值: 无
  23. func DeleteRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  24. params.passThrough(builder, http.MethodDelete, opts...)
  25. }
  26. // PutRoute PUT直传API
  27. // 参数:
  28. // - builder: 该网关API构建器
  29. // - params: 网关直通参数
  30. // - opts: 网关直通选项
  31. // 返回值: 无
  32. func PutRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  33. params.passThrough(builder, http.MethodPut, opts...)
  34. }
  35. // GetRoute GET直传API
  36. // 参数:
  37. // - builder: 该网关API构建器
  38. // - params: 网关直通参数
  39. // - opts: 网关直通选项
  40. // 返回值: 无
  41. func GetRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  42. params.passThrough(builder, http.MethodGet, opts...)
  43. }
  44. // PostRouteWithTenantIDAndUserID POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId
  45. // 参数:
  46. // - builder: 该网关API构建器
  47. // - params: 网关直通参数
  48. // - opts: 网关直通选项
  49. // 返回值: 无
  50. func PostRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  51. params.passThrough(builder, http.MethodPost, append(opts,
  52. WithTenantIDParamsName("tenantId"),
  53. WithUserIDParamsName("userId"))...)
  54. }
  55. // PostRouteWithTenantIDAndCreateUserID POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
  56. // 参数:
  57. // - builder: 该网关API构建器
  58. // - params: 网关直通参数
  59. // - opts: 网关直通选项
  60. // 返回值: 无
  61. func PostRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  62. params.passThrough(builder, http.MethodPost, append(opts,
  63. WithTenantIDParamsName("tenantId"),
  64. WithUserIDParamsName("createUserId"))...)
  65. }
  66. // DeleteRouteWithTenantIDAndUserID DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId
  67. // 参数:
  68. // - builder: 该网关API构建器
  69. // - params: 网关直通参数
  70. // - opts: 网关直通选项
  71. // 返回值: 无
  72. func DeleteRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  73. params.passThrough(builder, http.MethodDelete, append(opts,
  74. WithTenantIDParamsName("tenantId"),
  75. WithUserIDParamsName("userId"))...)
  76. }
  77. // DeleteRouteWithTenantIDAndDeleteUserID DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
  78. // 参数:
  79. // - builder: 该网关API构建器
  80. // - params: 网关直通参数
  81. // - opts: 网关直通选项
  82. // 返回值: 无
  83. func DeleteRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  84. params.passThrough(builder, http.MethodDelete, append(opts,
  85. WithTenantIDParamsName("tenantId"),
  86. WithUserIDParamsName("deleteUserId"))...)
  87. }
  88. // PutRouteWithTenantIDAndUserID PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId
  89. // 参数:
  90. // - builder: 该网关API构建器
  91. // - params: 网关直通参数
  92. // - opts: 网关直通选项
  93. // 返回值: 无
  94. func PutRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  95. params.passThrough(builder, http.MethodPut, append(opts,
  96. WithTenantIDParamsName("tenantId"),
  97. WithUserIDParamsName("userId"))...)
  98. }
  99. // PutRouteWithTenantIDAndUpdateUserID PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
  100. // 参数:
  101. // - builder: 该网关API构建器
  102. // - params: 网关直通参数
  103. // - opts: 网关直通选项
  104. // 返回值: 无
  105. func PutRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  106. params.passThrough(builder, http.MethodPut, append(opts,
  107. WithTenantIDParamsName("tenantId"),
  108. WithUserIDParamsName("updateUserId"))...)
  109. }
  110. // PostRouteWithTenantIDAndUserInfo POST直传API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName
  111. // 参数:
  112. // - builder: 该网关API构建器
  113. // - params: 网关直通参数
  114. // - opts: 网关直通选项
  115. // 返回值: 无
  116. func PostRouteWithTenantIDAndUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  117. params.passThrough(builder, http.MethodPost, append(opts,
  118. WithTenantIDParamsName("tenantId"),
  119. WithUserIDParamsName("userId"),
  120. WithUserNameParamsName("operatorUserName"))...)
  121. }
  122. // PostRouteWithTenantIDAndCreateUserInfo POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
  123. // 参数:
  124. // - builder: 该网关API构建器
  125. // - params: 网关直通参数
  126. // - opts: 网关直通选项
  127. // 返回值: 无
  128. func PostRouteWithTenantIDAndCreateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  129. params.passThrough(builder, http.MethodPost, append(opts,
  130. WithTenantIDParamsName("tenantId"),
  131. WithUserIDParamsName("createUserId"),
  132. WithUserNameParamsName("operatorUserName"))...)
  133. }
  134. // DeleteRouteWithTenantIDAndUserInfo DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
  135. // 参数:
  136. // - builder: 该网关API构建器
  137. // - params: 网关直通参数
  138. // - opts: 网关直通选项
  139. // 返回值: 无
  140. func DeleteRouteWithTenantIDAndUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  141. params.passThrough(builder, http.MethodDelete, append(opts,
  142. WithTenantIDParamsName("tenantId"),
  143. WithUserIDParamsName("userId"),
  144. WithUserNameParamsName("operatorUserName"))...)
  145. }
  146. // DeleteRouteWithTenantIDAndDeleteUserInfo DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
  147. // 参数:
  148. // - builder: 该网关API构建器
  149. // - params: 网关直通参数
  150. // - opts: 网关直通选项
  151. // 返回值: 无
  152. func DeleteRouteWithTenantIDAndDeleteUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  153. params.passThrough(builder, http.MethodDelete, append(opts,
  154. WithTenantIDParamsName("tenantId"),
  155. WithUserIDParamsName("deleteUserId"),
  156. WithUserNameParamsName("operatorUserName"))...)
  157. }
  158. // PutRouteWithTenantIDAndUserInfo PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
  159. // 参数:
  160. // - builder: 该网关API构建器
  161. // - params: 网关直通参数
  162. // - opts: 网关直通选项
  163. // 返回值: 无
  164. func PutRouteWithTenantIDAndUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  165. params.passThrough(builder, http.MethodPut, append(opts,
  166. WithTenantIDParamsName("tenantId"),
  167. WithUserIDParamsName("userId"),
  168. WithUserNameParamsName("operatorUserName"))...)
  169. }
  170. // PutRouteWithTenantIDAndUpdateUserInfo PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
  171. // 参数:
  172. // - builder: 该网关API构建器
  173. // - params: 网关直通参数
  174. // - opts: 网关直通选项
  175. // 返回值: 无
  176. func PutRouteWithTenantIDAndUpdateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  177. params.passThrough(builder, http.MethodPut, append(opts,
  178. WithTenantIDParamsName("tenantId"),
  179. WithUserIDParamsName("updateUserId"),
  180. WithUserNameParamsName("operatorUserName"))...)
  181. }
  182. // GetRouteWithTenantIDAndUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId
  183. // 参数:
  184. // - builder: 该网关API构建器
  185. // - params: 网关直通参数
  186. // - opts: 网关直通选项
  187. // 返回值: 无
  188. func GetRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  189. params.passThrough(builder, http.MethodGet, append(opts,
  190. WithTenantIDParamsName("tenantId"),
  191. WithUserIDParamsName("userId"))...)
  192. }
  193. // GetRouteWithTenantIDAndCreateUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
  194. // 参数:
  195. // - builder: 该网关API构建器
  196. // - params: 网关直通参数
  197. // - opts: 网关直通选项
  198. // 返回值: 无
  199. func GetRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  200. params.passThrough(builder, http.MethodGet, append(opts,
  201. WithTenantIDParamsName("tenantId"),
  202. WithUserIDParamsName("createUserId"))...)
  203. }
  204. // GetRouteWithTenantIDAndDeleteUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
  205. // 参数:
  206. // - builder: 该网关API构建器
  207. // - params: 网关直通参数
  208. // - opts: 网关直通选项
  209. // 返回值: 无
  210. func GetRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  211. params.passThrough(builder, http.MethodGet, append(opts,
  212. WithTenantIDParamsName("tenantId"),
  213. WithUserIDParamsName("deleteUserId"))...)
  214. }
  215. // GetRouteWithTenantIDAndUpdateUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
  216. // 参数:
  217. // - builder: 该网关API构建器
  218. // - params: 网关直通参数
  219. // - opts: 网关直通选项
  220. // 返回值: 无
  221. func GetRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  222. params.passThrough(builder, http.MethodGet, append(opts,
  223. WithTenantIDParamsName("tenantId"),
  224. WithUserIDParamsName("updateUserId"))...)
  225. }
  226. // PostRouteWithTenantID POST直传API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
  227. // 参数:
  228. // - builder: 该网关API构建器
  229. // - params: 网关直通参数
  230. // - opts: 网关直通选项
  231. // 返回值: 无
  232. func PostRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  233. params.passThrough(builder, http.MethodPost, append(opts,
  234. WithTenantIDParamsName("tenantId"))...)
  235. }
  236. // DeleteRouteWithTenantID DELETE直传API,会在查询参数添加租户ID字段,字段名分别为tenantId
  237. // 参数:
  238. // - builder: 该网关API构建器
  239. // - params: 网关直通参数
  240. // - opts: 网关直通选项
  241. // 返回值: 无
  242. func DeleteRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  243. params.passThrough(builder, http.MethodDelete, append(opts,
  244. WithTenantIDParamsName("tenantId"))...)
  245. }
  246. // PutRouteWithTenantID PUT直传API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
  247. // 参数:
  248. // - builder: 该网关API构建器
  249. // - params: 网关直通参数
  250. // - opts: 网关直通选项
  251. // 返回值: 无
  252. func PutRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  253. params.passThrough(builder, http.MethodPut, append(opts,
  254. WithTenantIDParamsName("tenantId"))...)
  255. }
  256. // GetRouteWithTenantID GET直传API,会在查询参数添加租户ID字段,字段名分别为tenantId
  257. // 参数:
  258. // - builder: 该网关API构建器
  259. // - params: 网关直通参数
  260. // - opts: 网关直通选项
  261. // 返回值: 无
  262. func GetRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  263. params.passThrough(builder, http.MethodGet, append(opts,
  264. WithTenantIDParamsName("tenantId"))...)
  265. }
  266. // PostRouteWithUserID POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为userId
  267. // 参数:
  268. // - builder: 该网关API构建器
  269. // - params: 网关直通参数
  270. // - opts: 网关直通选项
  271. // 返回值: 无
  272. func PostRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  273. params.passThrough(builder, http.MethodPost, append(opts,
  274. WithUserIDParamsName("userId"))...)
  275. }
  276. // PostRouteWithCreateUserID POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId
  277. // 参数:
  278. // - builder: 该网关API构建器
  279. // - params: 网关直通参数
  280. // - opts: 网关直通选项
  281. // 返回值: 无
  282. func PostRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  283. params.passThrough(builder, http.MethodPost, append(opts,
  284. WithUserIDParamsName("createUserId"))...)
  285. }
  286. // DeleteRouteWithUserID DELETE直传API,会在查询参数添加用户ID字段,字段名分别为userId
  287. // 参数:
  288. // - builder: 该网关API构建器
  289. // - params: 网关直通参数
  290. // - opts: 网关直通选项
  291. // 返回值: 无
  292. func DeleteRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  293. params.passThrough(builder, http.MethodDelete, append(opts,
  294. WithUserIDParamsName("userId"))...)
  295. }
  296. // DeleteRouteWithDeleteUserID DELETE直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
  297. // 参数:
  298. // - builder: 该网关API构建器
  299. // - params: 网关直通参数
  300. // - opts: 网关直通选项
  301. // 返回值: 无
  302. func DeleteRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  303. params.passThrough(builder, http.MethodDelete, append(opts,
  304. WithUserIDParamsName("deleteUserId"))...)
  305. }
  306. // PutRouteWithUserID PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId
  307. // 参数:
  308. // - builder: 该网关API构建器
  309. // - params: 网关直通参数
  310. // - opts: 网关直通选项
  311. // 返回值: 无
  312. func PutRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  313. params.passThrough(builder, http.MethodPut, append(opts,
  314. WithUserIDParamsName("userId"))...)
  315. }
  316. // PutRouteWithUpdateUserID PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId
  317. // 参数:
  318. // - builder: 该网关API构建器
  319. // - params: 网关直通参数
  320. // - opts: 网关直通选项
  321. // 返回值: 无
  322. func PutRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  323. params.passThrough(builder, http.MethodPut, append(opts,
  324. WithUserIDParamsName("updateUserId"))...)
  325. }
  326. // PostRouteWithUserInfo POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为userId,还会添加操作者用户名,字段名为operatorUserName
  327. // 参数:
  328. // - builder: 该网关API构建器
  329. // - params: 网关直通参数
  330. // - opts: 网关直通选项
  331. // 返回值: 无
  332. func PostRouteWithUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  333. params.passThrough(builder, http.MethodPost, append(opts,
  334. WithUserIDParamsName("userId"),
  335. WithUserNameParamsName("operatorUserName"))...)
  336. }
  337. // PostRouteWithCreateUserInfo POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId,还会添加操作者用户名,字段名为operatorUserName
  338. // 参数:
  339. // - builder: 该网关API构建器
  340. // - params: 网关直通参数
  341. // - opts: 网关直通选项
  342. // 返回值: 无
  343. func PostRouteWithCreateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  344. params.passThrough(builder, http.MethodPost, append(opts,
  345. WithUserIDParamsName("createUserId"),
  346. WithUserNameParamsName("operatorUserName"))...)
  347. }
  348. // DeleteRouteWithUserInfo DELETE直传API,会在查询参数添加用户ID字段,字段名分别为userId,还会添加操作者用户名,字段名为operatorUserName
  349. // 参数:
  350. // - builder: 该网关API构建器
  351. // - params: 网关直通参数
  352. // - opts: 网关直通选项
  353. // 返回值: 无
  354. func DeleteRouteWithUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  355. params.passThrough(builder, http.MethodDelete, append(opts,
  356. WithUserIDParamsName("userId"),
  357. WithUserNameParamsName("operatorUserName"))...)
  358. }
  359. // DeleteRouteWithDeleteUserInfo DELETE直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId,还会添加操作者用户名,字段名为operatorUserName
  360. // 参数:
  361. // - builder: 该网关API构建器
  362. // - params: 网关直通参数
  363. // - opts: 网关直通选项
  364. // 返回值: 无
  365. func DeleteRouteWithDeleteUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  366. params.passThrough(builder, http.MethodDelete, append(opts,
  367. WithUserIDParamsName("deleteUserId"),
  368. WithUserNameParamsName("operatorUserName"))...)
  369. }
  370. // PutRouteWithUserInfo PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId,还会添加操作者用户名,字段名为operatorUserName
  371. // 参数:
  372. // - builder: 该网关API构建器
  373. // - params: 网关直通参数
  374. // - opts: 网关直通选项
  375. // 返回值: 无
  376. func PutRouteWithUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  377. params.passThrough(builder, http.MethodPut, append(opts,
  378. WithUserIDParamsName("userId"),
  379. WithUserNameParamsName("operatorUserName"))...)
  380. }
  381. // PutRouteWithUpdateUserInfo PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId,还会添加操作者用户名,字段名为operatorUserName
  382. // 参数:
  383. // - builder: 该网关API构建器
  384. // - params: 网关直通参数
  385. // - opts: 网关直通选项
  386. // 返回值: 无
  387. func PutRouteWithUpdateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  388. params.passThrough(builder, http.MethodPut, append(opts,
  389. WithUserIDParamsName("updateUserId"),
  390. WithUserNameParamsName("operatorUserName"))...)
  391. }
  392. // GetRouteWithUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为userId
  393. // 参数:
  394. // - builder: 该网关API构建器
  395. // - params: 网关直通参数
  396. // - opts: 网关直通选项
  397. // 返回值: 无
  398. func GetRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  399. params.passThrough(builder, http.MethodGet, append(opts,
  400. WithUserIDParamsName("userId"))...)
  401. }
  402. // GetRouteWithCreateUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为createUserId
  403. // 参数:
  404. // - builder: 该网关API构建器
  405. // - params: 网关直通参数
  406. // - opts: 网关直通选项
  407. // 返回值: 无
  408. func GetRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  409. params.passThrough(builder, http.MethodGet, append(opts,
  410. WithUserIDParamsName("createUserId"))...)
  411. }
  412. // GetRouteWithDeleteUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
  413. // 参数:
  414. // - builder: 该网关API构建器
  415. // - params: 网关直通参数
  416. // - opts: 网关直通选项
  417. // 返回值: 无
  418. func GetRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  419. params.passThrough(builder, http.MethodGet, append(opts,
  420. WithUserIDParamsName("deleteUserId"))...)
  421. }
  422. // GetRouteWithUpdateUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为updateUserId
  423. // 参数:
  424. // - builder: 该网关API构建器
  425. // - params: 网关直通参数
  426. // - opts: 网关直通选项
  427. // 返回值: 无
  428. func GetRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  429. params.passThrough(builder, http.MethodGet, append(opts,
  430. WithUserIDParamsName("updateUserId"))...)
  431. }
  432. // Simple 参数
  433. type Simple struct {
  434. // RelativePath 网关开放API的RelativePath
  435. RelativePath string
  436. // 服务的URL
  437. ServiceUrl string
  438. }
  439. func (params *Simple) passThrough(builder *gateway.Builder, httpMethod string, opts ...Option) {
  440. options := new(Options)
  441. for _, opt := range opts {
  442. opt(options)
  443. }
  444. builder.AddRoute(httpMethod, params.RelativePath,
  445. func(requestBuilder *gateway.RequestBuilder) {
  446. if options.responseSuccessCallback != nil {
  447. requestBuilder.ResponseSuccessCallback(options.responseSuccessCallback)
  448. }
  449. if options.responseErrorCallback != nil {
  450. requestBuilder.ResponseErrorCallback(options.responseErrorCallback)
  451. }
  452. if strutils.IsStringNotEmpty(options.tenantIDParamsName) ||
  453. strutils.IsStringNotEmpty(options.userIDParamsName) {
  454. if httpMethod == http.MethodPost || httpMethod == http.MethodPut {
  455. err := gateway.AddJsonBodyTenantIDAndUserInfo(requestBuilder, options.tenantIDParamsName, options.userIDParamsName, options.userNameParamsName)
  456. if err != nil {
  457. requestBuilder.ResponseError(err)
  458. return
  459. }
  460. }
  461. if httpMethod == http.MethodDelete || httpMethod == http.MethodGet {
  462. err := gateway.AddQueryParamsTenantIDAndUserInfo(requestBuilder, options.tenantIDParamsName, options.userIDParamsName, options.userNameParamsName)
  463. if err != nil {
  464. requestBuilder.ResponseError(err)
  465. return
  466. }
  467. }
  468. }
  469. if options.beforeRequestCallback != nil {
  470. err := options.beforeRequestCallback(requestBuilder)
  471. if err != nil {
  472. requestBuilder.ResponseError(err)
  473. return
  474. }
  475. }
  476. requestOptions := make([]gateway.RequestOption, 0)
  477. if options.afterRequestCallback != nil {
  478. requestOptions = append(requestOptions, gateway.WithRequestResponseCallback(
  479. func(requestBuilder *gateway.RequestBuilder, response *http_client.Response) error {
  480. err := options.afterRequestCallback(requestBuilder)
  481. if err != nil {
  482. return err
  483. }
  484. return nil
  485. }))
  486. }
  487. switch httpMethod {
  488. case http.MethodPost:
  489. requestBuilder.Post(&gateway.PostRequest{
  490. Url: params.ServiceUrl,
  491. }, requestOptions...)
  492. case http.MethodDelete:
  493. requestBuilder.Delete(&gateway.DeleteRequest{
  494. Url: params.ServiceUrl,
  495. }, requestOptions...)
  496. case http.MethodPut:
  497. requestBuilder.Put(&gateway.PutRequest{
  498. Url: params.ServiceUrl,
  499. }, requestOptions...)
  500. case http.MethodGet:
  501. requestBuilder.Get(&gateway.GetRequest{
  502. Url: params.ServiceUrl,
  503. }, requestOptions...)
  504. default:
  505. panic("不支持的请求类型")
  506. }
  507. requestBuilder.Request()
  508. }, options.middlewares...)
  509. }
  510. type RequestBuilderCallback func(requestBuilder *gateway.RequestBuilder) error
  511. type Option func(options *Options)
  512. type Options struct {
  513. // tenantIDParamsName 租户ID请求参数名称
  514. tenantIDParamsName string
  515. // userIDParamsName 用户ID请求参数名称
  516. userIDParamsName string
  517. // userNameParamsName 用户名请求参数名称
  518. userNameParamsName string
  519. // beforeRequestCallback 请求前回调
  520. beforeRequestCallback RequestBuilderCallback
  521. // afterRequestCallback 请求后回调
  522. afterRequestCallback RequestBuilderCallback
  523. // responseSuccessCallback 成功响应回调
  524. responseSuccessCallback gateway.ResponseSuccessCallback
  525. // responseErrorCallback 失败响应回调
  526. responseErrorCallback gateway.ResponseErrorCallback
  527. // 中间件
  528. middlewares []gateway.Handler
  529. }
  530. // WithTenantIDParamsName 设置请求参数中的租户ID参数的名称
  531. func WithTenantIDParamsName(tenantIDParamsName string) Option {
  532. return func(options *Options) {
  533. options.tenantIDParamsName = tenantIDParamsName
  534. }
  535. }
  536. // WithUserIDParamsName 设置请求参数中的用户ID参数的名称
  537. func WithUserIDParamsName(userIDParamsName string) Option {
  538. return func(options *Options) {
  539. options.userIDParamsName = userIDParamsName
  540. }
  541. }
  542. // WithUserNameParamsName 设置请求参数中的用户名参数的名称
  543. func WithUserNameParamsName(userNameParamsName string) Option {
  544. return func(options *Options) {
  545. options.userNameParamsName = userNameParamsName
  546. }
  547. }
  548. // WithBeforeRequestCallback 设置请求前回调
  549. func WithBeforeRequestCallback(callback RequestBuilderCallback) Option {
  550. return func(options *Options) {
  551. options.beforeRequestCallback = callback
  552. }
  553. }
  554. // WithAfterRequestCallback 设置请求后回调
  555. func WithAfterRequestCallback(callback RequestBuilderCallback) Option {
  556. return func(options *Options) {
  557. options.afterRequestCallback = callback
  558. }
  559. }
  560. // WithResponseSuccessCallback 设置成功响应回调,默认回调会将服务响应作为网关API的响应返回
  561. func WithResponseSuccessCallback(callback gateway.ResponseSuccessCallback) Option {
  562. return func(options *Options) {
  563. options.responseSuccessCallback = callback
  564. }
  565. }
  566. // WithResponseErrorCallback 设置失败响应回调,默认回调会按照是否存在错误返回MsgResponse
  567. func WithResponseErrorCallback(callback gateway.ResponseErrorCallback) Option {
  568. return func(options *Options) {
  569. options.responseErrorCallback = callback
  570. }
  571. }
  572. // WithMiddlewares 设置中间件
  573. func WithMiddlewares(middlewares ...gateway.Handler) Option {
  574. return func(options *Options) {
  575. options.middlewares = middlewares
  576. }
  577. }