package gateway import ( "git.sxidc.com/go-framework/baize/framework/core/api" "git.sxidc.com/go-tools/utils/http_client" ) type UrlTransferFunc func(c *api.Context, url string, historyRequest []BuilderRequest, resultMap map[string]any) (string, error) type HeadersFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error) type BodyFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (any, error) type QueryParamsFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error) type PathParamsFormFunc func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error) func FormJsonBodyWithTenantIDAndUserIDFunc(tenantIDFieldName string, userIDFieldName string) BodyFormFunc { return func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (any, error) { err := AddJsonBodyTenantIDAndUserID(c, tenantIDFieldName, userIDFieldName) if err != nil { return nil, err } jsonBody, err := c.GetJsonBody() if err != nil { return nil, err } return jsonBody.Map(), nil } } func FormQueryParamsWithTenantIDAndUserIDFunc(tenantIDFieldName string, userIDFieldName string) QueryParamsFormFunc { return func(c *api.Context, historyRequest []BuilderRequest, resultMap map[string]any) (map[string]string, error) { err := AddQueryParamsTenantIDAndUserID(c, tenantIDFieldName, userIDFieldName) if err != nil { return nil, err } return c.GetQueryParams().Map(), nil } } type BuilderRequest interface { Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) Response() *http_client.Response } type PostRequestOption func(options *PostRequestOptions) type PostRequestOptions struct { urlTransferFunc UrlTransferFunc headersFormFunc HeadersFormFunc bodyFormFunc BodyFormFunc } func PostRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) PostRequestOption { return func(options *PostRequestOptions) { options.urlTransferFunc = urlTransferFunc } } func PostRequestWithHeadersForm(headersFormFunc HeadersFormFunc) PostRequestOption { return func(options *PostRequestOptions) { options.headersFormFunc = headersFormFunc } } func PostRequestWithBodyForm(bodyFormFunc BodyFormFunc) PostRequestOption { return func(options *PostRequestOptions) { options.bodyFormFunc = bodyFormFunc } } type PostRequest struct { url string response *http_client.Response options *PostRequestOptions } func NewPostRequest(url string, opts ...PostRequestOption) *PostRequest { options := new(PostRequestOptions) for _, opt := range opts { opt(options) } if options.urlTransferFunc == nil { options.urlTransferFunc = defaultUrlTransferFunc } if options.headersFormFunc == nil { options.headersFormFunc = defaultHeadersFormFunc } if options.bodyFormFunc == nil { options.bodyFormFunc = defaultBodyFormFunc } return &PostRequest{ url: url, options: options, } } func (req *PostRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) { url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap) if err != nil { return nil, err } headers, err := req.options.headersFormFunc(c, historyRequest, resultMap) if err != nil { return nil, err } body, err := req.options.bodyFormFunc(c, historyRequest, resultMap) if err != nil { return nil, err } response, err := request.Post(url, body, http_client.WithRequestHeaders(headers)) if err != nil { return nil, err } return &PostRequest{ url: url, response: response, options: &PostRequestOptions{ urlTransferFunc: req.options.urlTransferFunc, headersFormFunc: req.options.headersFormFunc, bodyFormFunc: req.options.bodyFormFunc, }, }, nil } func (req *PostRequest) Response() *http_client.Response { return req.response } type DeleteRequestOption func(options *DeleteRequestOptions) type DeleteRequestOptions struct { urlTransferFunc UrlTransferFunc headersFormFunc HeadersFormFunc pathParamsFormFunc PathParamsFormFunc queryParamsFormFunc QueryParamsFormFunc } func DeleteRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) DeleteRequestOption { return func(options *DeleteRequestOptions) { options.urlTransferFunc = urlTransferFunc } } func DeleteRequestWithHeadersForm(headersFormFunc HeadersFormFunc) DeleteRequestOption { return func(options *DeleteRequestOptions) { options.headersFormFunc = headersFormFunc } } func DeleteRequestWithPathParamsForm(pathParamsFormFunc PathParamsFormFunc) DeleteRequestOption { return func(options *DeleteRequestOptions) { options.pathParamsFormFunc = pathParamsFormFunc } } func DeleteRequestWithQueryParamsForm(queryParamsFormFunc QueryParamsFormFunc) DeleteRequestOption { return func(options *DeleteRequestOptions) { options.queryParamsFormFunc = queryParamsFormFunc } } type DeleteRequest struct { url string response *http_client.Response options *DeleteRequestOptions } func NewDeleteRequest(url string, opts ...DeleteRequestOption) *DeleteRequest { options := new(DeleteRequestOptions) for _, opt := range opts { opt(options) } if options.urlTransferFunc == nil { options.urlTransferFunc = defaultUrlTransferFunc } if options.headersFormFunc == nil { options.headersFormFunc = defaultHeadersFormFunc } if options.pathParamsFormFunc == nil { options.pathParamsFormFunc = defaultPathParamsFormFunc } if options.queryParamsFormFunc == nil { options.queryParamsFormFunc = defaultQueryParamsFormFunc } return &DeleteRequest{ url: url, options: options, } } func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) { url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap) if err != nil { return nil, err } headers, err := req.options.headersFormFunc(c, historyRequest, resultMap) if err != nil { return nil, err } pathParams, err := req.options.pathParamsFormFunc(c, historyRequest, resultMap) if err != nil { return nil, err } queryParams, err := req.options.queryParamsFormFunc(c, historyRequest, resultMap) if err != nil { return nil, err } response, err := request.Delete(url, http_client.WithRequestHeaders(headers), http_client.WithRequestPathParams(pathParams), http_client.WithRequestQueryParams(queryParams)) if err != nil { return nil, err } return &DeleteRequest{ url: url, response: response, options: &DeleteRequestOptions{ urlTransferFunc: req.options.urlTransferFunc, headersFormFunc: req.options.headersFormFunc, pathParamsFormFunc: req.options.pathParamsFormFunc, queryParamsFormFunc: req.options.queryParamsFormFunc, }, }, nil } func (req *DeleteRequest) Response() *http_client.Response { return req.response } type PutRequestOption func(options *PutRequestOptions) type PutRequestOptions struct { urlTransferFunc UrlTransferFunc headersFormFunc HeadersFormFunc bodyFormFunc BodyFormFunc } func PutRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) PutRequestOption { return func(options *PutRequestOptions) { options.urlTransferFunc = urlTransferFunc } } func PutRequestWithHeadersForm(headersFormFunc HeadersFormFunc) PutRequestOption { return func(options *PutRequestOptions) { options.headersFormFunc = headersFormFunc } } func PutRequestWithBodyForm(bodyFormFunc BodyFormFunc) PutRequestOption { return func(options *PutRequestOptions) { options.bodyFormFunc = bodyFormFunc } } type PutRequest struct { url string response *http_client.Response options *PutRequestOptions } func NewPutRequest(url string, opts ...PutRequestOption) *PutRequest { options := new(PutRequestOptions) for _, opt := range opts { opt(options) } if options.urlTransferFunc == nil { options.urlTransferFunc = defaultUrlTransferFunc } if options.headersFormFunc == nil { options.headersFormFunc = defaultHeadersFormFunc } if options.bodyFormFunc == nil { options.bodyFormFunc = defaultBodyFormFunc } return &PutRequest{ url: url, options: options, } } func (req *PutRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) { url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap) if err != nil { return nil, err } headers, err := req.options.headersFormFunc(c, historyRequest, resultMap) if err != nil { return nil, err } body, err := req.options.bodyFormFunc(c, historyRequest, resultMap) if err != nil { return nil, err } response, err := request.Put(url, body, http_client.WithRequestHeaders(headers)) if err != nil { return nil, err } return &PutRequest{ url: url, response: response, options: &PutRequestOptions{ urlTransferFunc: req.options.urlTransferFunc, headersFormFunc: req.options.headersFormFunc, bodyFormFunc: req.options.bodyFormFunc, }, }, nil } func (req *PutRequest) Response() *http_client.Response { return req.response } type GetRequestOption func(options *GetRequestOptions) type GetRequestOptions struct { urlTransferFunc UrlTransferFunc headersFormFunc HeadersFormFunc pathParamsFormFunc PathParamsFormFunc queryParamsFormFunc QueryParamsFormFunc } func GetRequestWithUrlTransferFunc(urlTransferFunc UrlTransferFunc) GetRequestOption { return func(options *GetRequestOptions) { options.urlTransferFunc = urlTransferFunc } } func GetRequestWithHeadersForm(headersFormFunc HeadersFormFunc) GetRequestOption { return func(options *GetRequestOptions) { options.headersFormFunc = headersFormFunc } } func GetRequestWithPathParamsForm(pathParamsFormFunc PathParamsFormFunc) GetRequestOption { return func(options *GetRequestOptions) { options.pathParamsFormFunc = pathParamsFormFunc } } func GetRequestWithQueryParamsForm(queryParamsFormFunc QueryParamsFormFunc) GetRequestOption { return func(options *GetRequestOptions) { options.queryParamsFormFunc = queryParamsFormFunc } } type GetRequest struct { url string headers map[string]string pathParams map[string]string queryParams map[string]string response *http_client.Response options *GetRequestOptions } func NewGetRequest(url string, opts ...GetRequestOption) *GetRequest { options := new(GetRequestOptions) for _, opt := range opts { opt(options) } if options.urlTransferFunc == nil { options.urlTransferFunc = defaultUrlTransferFunc } if options.headersFormFunc == nil { options.headersFormFunc = defaultHeadersFormFunc } if options.pathParamsFormFunc == nil { options.pathParamsFormFunc = defaultPathParamsFormFunc } if options.queryParamsFormFunc == nil { options.queryParamsFormFunc = defaultQueryParamsFormFunc } return &GetRequest{ url: url, options: options, } } func (req *GetRequest) Request(c *api.Context, request *http_client.Request, historyRequest []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) { url, err := req.options.urlTransferFunc(c, req.url, historyRequest, resultMap) if err != nil { return nil, err } headers, err := req.options.headersFormFunc(c, historyRequest, resultMap) if err != nil { return nil, err } pathParams, err := req.options.pathParamsFormFunc(c, historyRequest, resultMap) if err != nil { return nil, err } queryParams, err := req.options.queryParamsFormFunc(c, historyRequest, resultMap) if err != nil { return nil, err } response, err := request.Get(url, http_client.WithRequestHeaders(headers), http_client.WithRequestPathParams(pathParams), http_client.WithRequestQueryParams(queryParams)) if err != nil { return nil, err } return &GetRequest{ url: url, headers: headers, pathParams: pathParams, queryParams: queryParams, response: response, options: &GetRequestOptions{ urlTransferFunc: req.options.urlTransferFunc, headersFormFunc: req.options.headersFormFunc, pathParamsFormFunc: req.options.pathParamsFormFunc, queryParamsFormFunc: req.options.queryParamsFormFunc, }, }, nil } func (req *GetRequest) Response() *http_client.Response { return req.response } func defaultUrlTransferFunc(_ *api.Context, url string, _ []BuilderRequest, _ map[string]any) (string, error) { return url, nil } func defaultHeadersFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) { return c.GetHeaders(), nil } func defaultBodyFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (any, error) { cachedBody, err := c.GetBytesBody() if err != nil { return nil, err } return cachedBody.Bytes(), nil } func defaultQueryParamsFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) { return c.GetQueryParams().Map(), nil } func defaultPathParamsFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) { return c.GetPathParams().Map(), nil }