request_builder.go 6.2 KB

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