123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- package gateway
- import (
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-tools/utils/http_client"
- "github.com/pkg/errors"
- )
- // ResponseSuccessCallback 网关API成功响应回调
- type ResponseSuccessCallback func(requestBuilder *RequestBuilder)
- // ResponseErrorCallback 网关API错误响应回调
- type ResponseErrorCallback func(requestBuilder *RequestBuilder, err error)
- // BeforeRequestCallback 请求发送前的回调
- type BeforeRequestCallback func(requestBuilder *RequestBuilder) error
- // RequestResponseCallback 请求响应回调
- type RequestResponseCallback func(requestBuilder *RequestBuilder, response *http_client.Response) error
- type RequestOption func(options *RequestOptions)
- type RequestOptions struct {
- beforeRequestCallback BeforeRequestCallback
- requestResponseCallback RequestResponseCallback
- }
- // WithBeforeRequestCallback 设置请求发送前的回调请求选项
- func WithBeforeRequestCallback(callback BeforeRequestCallback) RequestOption {
- return func(options *RequestOptions) {
- options.beforeRequestCallback = callback
- }
- }
- // WithRequestResponseCallback 设置请求响应回调请求选项
- func WithRequestResponseCallback(callback RequestResponseCallback) RequestOption {
- return func(options *RequestOptions) {
- options.requestResponseCallback = callback
- }
- }
- // RequestBuilder 请求构建器
- type RequestBuilder struct {
- c *api.Context
- historyRequests []Request
- resultMap map[string]any
- httpRequest *http_client.Request
- params *requestBuilderParams
- }
- func newRequestBuilder(c *api.Context, httpRequest *http_client.Request) *RequestBuilder {
- return &RequestBuilder{
- c: c,
- historyRequests: make([]Request, 0),
- resultMap: make(map[string]any),
- httpRequest: httpRequest,
- params: newRequestBuilderParams(),
- }
- }
- // Context 获取上下文
- // 参数: 无
- // 返回值:
- // - 上下文
- func (builder *RequestBuilder) Context() *api.Context {
- return builder.c
- }
- // HistoryRequests 获取历史请求(包含响应)
- // 参数: 无
- // 返回值:
- // - 历史请求
- func (builder *RequestBuilder) HistoryRequests() []Request {
- return builder.historyRequests
- }
- // GetResultMapValue 获取结果Map的值
- // 参数:
- // - key: 键
- // 返回值:
- // - 值
- func (builder *RequestBuilder) GetResultMapValue(key string) any {
- return builder.resultMap[key]
- }
- // SetResultMapValue 设置结果Map的值
- // 参数:
- // - key: 键
- // - 值
- // 返回值: 无
- func (builder *RequestBuilder) SetResultMapValue(key string, value any) {
- builder.resultMap[key] = value
- }
- // Post 定义POST请求
- // 参数:
- // - request: PostRequest
- // - opts: 请求选项
- // 返回值:
- // - RequestBuilder
- func (builder *RequestBuilder) Post(request *PostRequest, opts ...RequestOption) *RequestBuilder {
- return builder.request(request, opts...)
- }
- // Delete 定义Delete请求
- // 参数:
- // - request: DeleteRequest
- // - opts: 请求选项
- // 返回值:
- // - RequestBuilder
- func (builder *RequestBuilder) Delete(request *DeleteRequest, opts ...RequestOption) *RequestBuilder {
- return builder.request(request, opts...)
- }
- // Put 定义Put请求
- // 参数:
- // - request: PutRequest
- // - opts: 请求选项
- // 返回值:
- // - RequestBuilder
- func (builder *RequestBuilder) Put(request *PutRequest, opts ...RequestOption) *RequestBuilder {
- return builder.request(request, opts...)
- }
- // Get 定义Get请求
- // 参数:
- // - request: GetRequest
- // - opts: 请求选项
- // 返回值:
- // - RequestBuilder
- func (builder *RequestBuilder) Get(request *GetRequest, opts ...RequestOption) *RequestBuilder {
- return builder.request(request, opts...)
- }
- // ResponseSuccessCallback 设置网关API成功响应回调
- // 参数:
- // - responseSuccessCallback: 网关API成功响应回调,默认回调会将最后一个请求的响应作为网关API的响应返回
- // 返回值:
- // - RequestBuilder
- func (builder *RequestBuilder) ResponseSuccessCallback(responseSuccessCallback ResponseSuccessCallback) *RequestBuilder {
- builder.params.responseSuccessCallback = responseSuccessCallback
- return builder
- }
- // ResponseErrorCallback 设置网关API错误响应回调
- // 参数:
- // - responseErrorCallback: 网关API错误响应回调,默认回调会按照是否存在错误返回MsgResponse
- // 返回值:
- // - RequestBuilder
- func (builder *RequestBuilder) ResponseErrorCallback(responseErrorCallback ResponseErrorCallback) *RequestBuilder {
- builder.params.responseErrorCallback = responseErrorCallback
- return builder
- }
- // Request 发送构造的请求链
- // 参数: 无
- // 返回值: 无
- func (builder *RequestBuilder) Request() {
- httpRequest := builder.httpRequest
- for _, requestItem := range builder.params.requestItems {
- if requestItem.options.beforeRequestCallback != nil {
- err := requestItem.options.beforeRequestCallback(builder)
- if err != nil {
- builder.params.responseErrorCallback(builder, errors.New(err.Error()))
- return
- }
- }
- requestWithResponse, err := requestItem.builderRequest.Request(builder.c, httpRequest)
- if err != nil {
- builder.params.responseErrorCallback(builder, errors.New(err.Error()))
- return
- }
- builder.historyRequests = append(builder.historyRequests, requestWithResponse)
- if requestItem.options.requestResponseCallback != nil {
- err := requestItem.options.requestResponseCallback(builder, requestWithResponse.Response())
- if err != nil {
- builder.params.responseErrorCallback(builder, errors.New(err.Error()))
- return
- }
- }
- }
- builder.params.responseSuccessCallback(builder)
- }
- // ResponseError 利用网关API错误响应回调发送网关API响应
- // 参数:
- // - 错误
- // 返回值: 无
- func (builder *RequestBuilder) ResponseError(err error) {
- builder.params.responseErrorCallback(builder, err)
- }
- // ResponseSuccess 利用网关API成功响应回调发送网关API响应
- // 参数: 无
- // 返回值: 无
- func (builder *RequestBuilder) ResponseSuccess() {
- builder.params.responseSuccessCallback(builder)
- }
- func (builder *RequestBuilder) request(builderRequest Request, opts ...RequestOption) *RequestBuilder {
- options := new(RequestOptions)
- for _, opt := range opts {
- opt(options)
- }
- builder.params.requestItems = append(builder.params.requestItems, newBuilderRequestItem(builderRequest, options))
- return builder
- }
|