123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- 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(c *RequestBuilderContext)
- // ResponseErrorCallback 网关API错误响应回调
- type ResponseErrorCallback func(c *RequestBuilderContext, err error)
- // BeforeRequestCallback 请求发送前的回调
- type BeforeRequestCallback func(c *RequestBuilderContext) error
- // RequestResponseCallback 请求响应回调
- type RequestResponseCallback func(c *RequestBuilderContext, 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
- }
- }
- // RequestBuilderContext 请求构建器上下文
- type RequestBuilderContext struct {
- apiContext *api.Context
- historyRequests []Request
- resultMap map[string]any
- }
- func newRequestBuilderContext(c *api.Context) *RequestBuilderContext {
- return &RequestBuilderContext{
- apiContext: c,
- historyRequests: make([]Request, 0),
- resultMap: make(map[string]any),
- }
- }
- // ApiContext 获取Api上下文
- // 参数: 无
- // 返回值:
- // - 上下文
- func (c *RequestBuilderContext) ApiContext() *api.Context {
- return c.apiContext
- }
- // HistoryRequests 获取历史请求(包含响应)
- // 参数: 无
- // 返回值:
- // - 历史请求
- func (c *RequestBuilderContext) HistoryRequests() []Request {
- return c.historyRequests
- }
- // GetResultMapValue 获取结果Map的值
- // 参数:
- // - key: 键
- // 返回值:
- // - 值
- func (c *RequestBuilderContext) GetResultMapValue(key string) any {
- return c.resultMap[key]
- }
- // SetResultMapValue 设置结果Map的值
- // 参数:
- // - key: 键
- // - 值
- // 返回值: 无
- func (c *RequestBuilderContext) SetResultMapValue(key string, value any) {
- c.resultMap[key] = value
- }
- // RequestBuilder 请求构建器
- type RequestBuilder struct {
- c *RequestBuilderContext
- httpRequest *http_client.Request
- params *requestBuilderParams
- }
- func newRequestBuilder(c *api.Context, httpRequest *http_client.Request) *RequestBuilder {
- return &RequestBuilder{
- c: newRequestBuilderContext(c),
- httpRequest: httpRequest,
- params: newRequestBuilderParams(),
- }
- }
- // ApiContext 获取Api上下文
- // 参数: 无
- // 返回值:
- // - 上下文
- func (builder *RequestBuilder) ApiContext() *api.Context {
- return builder.c.apiContext
- }
- // 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.c)
- if err != nil {
- builder.params.responseErrorCallback(builder.c, errors.New(err.Error()))
- return
- }
- }
- requestWithResponse, err := requestItem.builderRequest.Request(builder.c.apiContext, httpRequest)
- if err != nil {
- builder.params.responseErrorCallback(builder.c, errors.New(err.Error()))
- return
- }
- builder.c.historyRequests = append(builder.c.historyRequests, requestWithResponse)
- if requestItem.options.requestResponseCallback != nil {
- err := requestItem.options.requestResponseCallback(builder.c, requestWithResponse.Response())
- if err != nil {
- builder.params.responseErrorCallback(builder.c, errors.New(err.Error()))
- return
- }
- }
- }
- builder.params.responseSuccessCallback(builder.c)
- }
- // ResponseError 利用网关API错误响应回调发送网关API响应
- // 参数:
- // - 错误
- // 返回值: 无
- func (builder *RequestBuilder) ResponseError(err error) {
- builder.params.responseErrorCallback(builder.c, err)
- }
- // ResponseSuccess 利用网关API成功响应回调发送网关API响应
- // 参数: 无
- // 返回值: 无
- func (builder *RequestBuilder) ResponseSuccess() {
- builder.params.responseSuccessCallback(builder.c)
- }
- 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
- }
|