request_builder.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. "github.com/pkg/errors"
  6. )
  7. // ResponseSuccessCallback 网关API成功响应回调
  8. type ResponseSuccessCallback func(c *RequestBuilderContext)
  9. // ResponseErrorCallback 网关API错误响应回调
  10. type ResponseErrorCallback func(c *RequestBuilderContext, err error)
  11. // BeforeRequestCallback 请求发送前的回调
  12. type BeforeRequestCallback func(c *RequestBuilderContext) error
  13. // RequestResponseCallback 请求响应回调
  14. type RequestResponseCallback func(c *RequestBuilderContext, response *http_client.Response) error
  15. type RequestOption func(options *RequestOptions)
  16. type RequestOptions struct {
  17. beforeRequestCallback BeforeRequestCallback
  18. requestResponseCallback RequestResponseCallback
  19. }
  20. // WithBeforeRequestCallback 设置请求发送前的回调请求选项
  21. func WithBeforeRequestCallback(callback BeforeRequestCallback) RequestOption {
  22. return func(options *RequestOptions) {
  23. options.beforeRequestCallback = callback
  24. }
  25. }
  26. // WithRequestResponseCallback 设置请求响应回调请求选项
  27. func WithRequestResponseCallback(callback RequestResponseCallback) RequestOption {
  28. return func(options *RequestOptions) {
  29. options.requestResponseCallback = callback
  30. }
  31. }
  32. // RequestBuilderContext 请求构建器上下文
  33. type RequestBuilderContext struct {
  34. apiContext *api.Context
  35. historyRequests []Request
  36. resultMap map[string]any
  37. }
  38. func newRequestBuilderContext(c *api.Context) *RequestBuilderContext {
  39. return &RequestBuilderContext{
  40. apiContext: c,
  41. historyRequests: make([]Request, 0),
  42. resultMap: make(map[string]any),
  43. }
  44. }
  45. // ApiContext 获取Api上下文
  46. // 参数: 无
  47. // 返回值:
  48. // - 上下文
  49. func (c *RequestBuilderContext) ApiContext() *api.Context {
  50. return c.apiContext
  51. }
  52. // HistoryRequests 获取历史请求(包含响应)
  53. // 参数: 无
  54. // 返回值:
  55. // - 历史请求
  56. func (c *RequestBuilderContext) HistoryRequests() []Request {
  57. return c.historyRequests
  58. }
  59. // GetResultMapValue 获取结果Map的值
  60. // 参数:
  61. // - key: 键
  62. // 返回值:
  63. // - 值
  64. func (c *RequestBuilderContext) GetResultMapValue(key string) any {
  65. return c.resultMap[key]
  66. }
  67. // SetResultMapValue 设置结果Map的值
  68. // 参数:
  69. // - key: 键
  70. // - 值
  71. // 返回值: 无
  72. func (c *RequestBuilderContext) SetResultMapValue(key string, value any) {
  73. c.resultMap[key] = value
  74. }
  75. // RequestBuilder 请求构建器
  76. type RequestBuilder struct {
  77. c *RequestBuilderContext
  78. httpRequest *http_client.Request
  79. params *requestBuilderParams
  80. }
  81. func newRequestBuilder(c *api.Context, httpRequest *http_client.Request) *RequestBuilder {
  82. return &RequestBuilder{
  83. c: newRequestBuilderContext(c),
  84. httpRequest: httpRequest,
  85. params: newRequestBuilderParams(),
  86. }
  87. }
  88. // ApiContext 获取Api上下文
  89. // 参数: 无
  90. // 返回值:
  91. // - 上下文
  92. func (builder *RequestBuilder) ApiContext() *api.Context {
  93. return builder.c.apiContext
  94. }
  95. // Post 定义POST请求
  96. // 参数:
  97. // - request: PostRequest
  98. // - opts: 请求选项
  99. // 返回值:
  100. // - RequestBuilder
  101. func (builder *RequestBuilder) Post(request *PostRequest, opts ...RequestOption) *RequestBuilder {
  102. return builder.request(request, opts...)
  103. }
  104. // Delete 定义Delete请求
  105. // 参数:
  106. // - request: DeleteRequest
  107. // - opts: 请求选项
  108. // 返回值:
  109. // - RequestBuilder
  110. func (builder *RequestBuilder) Delete(request *DeleteRequest, opts ...RequestOption) *RequestBuilder {
  111. return builder.request(request, opts...)
  112. }
  113. // Put 定义Put请求
  114. // 参数:
  115. // - request: PutRequest
  116. // - opts: 请求选项
  117. // 返回值:
  118. // - RequestBuilder
  119. func (builder *RequestBuilder) Put(request *PutRequest, opts ...RequestOption) *RequestBuilder {
  120. return builder.request(request, opts...)
  121. }
  122. // Get 定义Get请求
  123. // 参数:
  124. // - request: GetRequest
  125. // - opts: 请求选项
  126. // 返回值:
  127. // - RequestBuilder
  128. func (builder *RequestBuilder) Get(request *GetRequest, opts ...RequestOption) *RequestBuilder {
  129. return builder.request(request, opts...)
  130. }
  131. // ResponseSuccessCallback 设置网关API成功响应回调
  132. // 参数:
  133. // - responseSuccessCallback: 网关API成功响应回调,默认回调会将最后一个请求的响应作为网关API的响应返回
  134. // 返回值:
  135. // - RequestBuilder
  136. func (builder *RequestBuilder) ResponseSuccessCallback(responseSuccessCallback ResponseSuccessCallback) *RequestBuilder {
  137. builder.params.responseSuccessCallback = responseSuccessCallback
  138. return builder
  139. }
  140. // ResponseErrorCallback 设置网关API错误响应回调
  141. // 参数:
  142. // - responseErrorCallback: 网关API错误响应回调,默认回调会按照是否存在错误返回MsgResponse
  143. // 返回值:
  144. // - RequestBuilder
  145. func (builder *RequestBuilder) ResponseErrorCallback(responseErrorCallback ResponseErrorCallback) *RequestBuilder {
  146. builder.params.responseErrorCallback = responseErrorCallback
  147. return builder
  148. }
  149. // Request 发送构造的请求链
  150. // 参数: 无
  151. // 返回值: 无
  152. func (builder *RequestBuilder) Request() {
  153. httpRequest := builder.httpRequest
  154. for _, requestItem := range builder.params.requestItems {
  155. if requestItem.options.beforeRequestCallback != nil {
  156. err := requestItem.options.beforeRequestCallback(builder.c)
  157. if err != nil {
  158. builder.params.responseErrorCallback(builder.c, errors.New(err.Error()))
  159. return
  160. }
  161. }
  162. requestWithResponse, err := requestItem.builderRequest.Request(builder.c.apiContext, httpRequest)
  163. if err != nil {
  164. builder.params.responseErrorCallback(builder.c, errors.New(err.Error()))
  165. return
  166. }
  167. builder.c.historyRequests = append(builder.c.historyRequests, requestWithResponse)
  168. if requestItem.options.requestResponseCallback != nil {
  169. err := requestItem.options.requestResponseCallback(builder.c, requestWithResponse.Response())
  170. if err != nil {
  171. builder.params.responseErrorCallback(builder.c, errors.New(err.Error()))
  172. return
  173. }
  174. }
  175. }
  176. builder.params.responseSuccessCallback(builder.c)
  177. }
  178. // ResponseError 利用网关API错误响应回调发送网关API响应
  179. // 参数:
  180. // - 错误
  181. // 返回值: 无
  182. func (builder *RequestBuilder) ResponseError(err error) {
  183. builder.params.responseErrorCallback(builder.c, err)
  184. }
  185. // ResponseSuccess 利用网关API成功响应回调发送网关API响应
  186. // 参数: 无
  187. // 返回值: 无
  188. func (builder *RequestBuilder) ResponseSuccess() {
  189. builder.params.responseSuccessCallback(builder.c)
  190. }
  191. func (builder *RequestBuilder) request(builderRequest Request, opts ...RequestOption) *RequestBuilder {
  192. options := new(RequestOptions)
  193. for _, opt := range opts {
  194. opt(options)
  195. }
  196. builder.params.requestItems = append(builder.params.requestItems, newBuilderRequestItem(builderRequest, options))
  197. return builder
  198. }