| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- package gateway
- import (
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-tools/utils/http_client"
- )
- type UrlFormFunc func(c *api.Context, staticUrl string, historyRequests []BuilderRequest, resultMap map[string]any) (string, error)
- type HeadersFormFunc func(c *api.Context, staticHeaders map[string]string, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error)
- type BodyFormFunc func(c *api.Context, staticBody any, historyRequests []BuilderRequest, resultMap map[string]any) (any, error)
- type QueryParamsFormFunc func(c *api.Context, staticQueryParams map[string]string, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error)
- type PathParamsFormFunc func(c *api.Context, staticPathParams map[string]string, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error)
- func FormJsonBodyWithTenantIDAndUserIDFunc(tenantIDFieldName string, userIDFieldName string) BodyFormFunc {
- return func(c *api.Context, staticBody any, historyRequests []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, staticQueryParams map[string]string, historyRequests []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, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error)
- Response() *http_client.Response
- }
- type PostRequest struct {
- Url string
- Body any
- UrlFormFunc UrlFormFunc
- BodyFormFunc BodyFormFunc
- HeadersFormFunc HeadersFormFunc
- headers map[string]string
- response *http_client.Response
- }
- func (req *PostRequest) Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
- preparedRequest, err := req.prepare(c, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- response, err := request.Post(preparedRequest.Url, preparedRequest.Body,
- http_client.WithRequestHeaders(preparedRequest.headers))
- if err != nil {
- return nil, err
- }
- preparedRequest.response = response
- return preparedRequest, nil
- }
- func (req *PostRequest) Response() *http_client.Response {
- return req.response
- }
- func (req *PostRequest) prepare(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (*PostRequest, error) {
- preparedRequest := &PostRequest{
- Url: req.Url,
- Body: req.Body,
- UrlFormFunc: req.UrlFormFunc,
- HeadersFormFunc: req.HeadersFormFunc,
- BodyFormFunc: req.BodyFormFunc,
- }
- // 赋值默认body
- if req.Body == nil {
- cacheBody, err := c.GetBytesBody()
- if err != nil {
- return nil, err
- }
- preparedRequest.Body = cacheBody.Bytes()
- }
- // 赋值默认headers
- preparedRequest.headers = c.GetHeader().Map()
- // 构造url
- if preparedRequest.UrlFormFunc != nil {
- formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.Url = formedUrl
- }
- // 构造body
- if preparedRequest.BodyFormFunc != nil {
- formedBody, err := preparedRequest.BodyFormFunc(c, preparedRequest.Body, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.Body = formedBody
- }
- // 构造headers
- if preparedRequest.HeadersFormFunc != nil {
- formedHeaders, err := preparedRequest.HeadersFormFunc(c, preparedRequest.headers, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.headers = formedHeaders
- }
- return preparedRequest, nil
- }
- type DeleteRequest struct {
- Url string
- QueryParams map[string]string
- PathParams map[string]string
- UrlFormFunc UrlFormFunc
- HeadersFormFunc HeadersFormFunc
- QueryParamsFormFunc QueryParamsFormFunc
- PathParamsFormFunc PathParamsFormFunc
- headers map[string]string
- response *http_client.Response
- }
- func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
- preparedRequest, err := req.prepare(c, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- response, err := request.Delete(preparedRequest.Url,
- http_client.WithRequestQueryParams(preparedRequest.QueryParams),
- http_client.WithRequestPathParams(preparedRequest.PathParams),
- http_client.WithRequestHeaders(preparedRequest.headers))
- if err != nil {
- return nil, err
- }
- preparedRequest.response = response
- return preparedRequest, nil
- }
- func (req *DeleteRequest) Response() *http_client.Response {
- return req.response
- }
- func (req *DeleteRequest) prepare(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (*DeleteRequest, error) {
- preparedRequest := &DeleteRequest{
- Url: req.Url,
- QueryParams: req.QueryParams,
- PathParams: req.PathParams,
- UrlFormFunc: req.UrlFormFunc,
- HeadersFormFunc: req.HeadersFormFunc,
- QueryParamsFormFunc: req.QueryParamsFormFunc,
- PathParamsFormFunc: req.PathParamsFormFunc,
- }
- // 赋值默认查询参数
- if req.QueryParams == nil || len(req.QueryParams) == 0 {
- req.QueryParams = c.GetQueryParams().Map()
- }
- // 赋值默认路径参数
- if req.PathParams == nil || len(req.PathParams) == 0 {
- req.PathParams = c.GetPathParams().Map()
- }
- // 赋值默认headers
- preparedRequest.headers = c.GetHeader().Map()
- // 构造url
- if preparedRequest.UrlFormFunc != nil {
- formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.Url = formedUrl
- }
- // 构造查询参数
- if preparedRequest.QueryParamsFormFunc != nil {
- formedQueryParams, err := preparedRequest.QueryParamsFormFunc(c, preparedRequest.QueryParams, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.QueryParams = formedQueryParams
- }
- // 构造路径参数
- if preparedRequest.PathParamsFormFunc != nil {
- formedPathParams, err := preparedRequest.PathParamsFormFunc(c, preparedRequest.PathParams, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.PathParams = formedPathParams
- }
- // 构造headers
- if preparedRequest.HeadersFormFunc != nil {
- formedHeaders, err := preparedRequest.HeadersFormFunc(c, preparedRequest.headers, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.headers = formedHeaders
- }
- return preparedRequest, nil
- }
- type PutRequest struct {
- Url string
- Body any
- UrlFormFunc UrlFormFunc
- BodyFormFunc BodyFormFunc
- HeadersFormFunc HeadersFormFunc
- headers map[string]string
- response *http_client.Response
- }
- func (req *PutRequest) Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
- preparedRequest, err := req.prepare(c, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- response, err := request.Put(preparedRequest.Url, preparedRequest.Body,
- http_client.WithRequestHeaders(preparedRequest.headers))
- if err != nil {
- return nil, err
- }
- preparedRequest.response = response
- return preparedRequest, nil
- }
- func (req *PutRequest) Response() *http_client.Response {
- return req.response
- }
- func (req *PutRequest) prepare(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (*PutRequest, error) {
- preparedRequest := &PutRequest{
- Url: req.Url,
- Body: req.Body,
- UrlFormFunc: req.UrlFormFunc,
- HeadersFormFunc: req.HeadersFormFunc,
- BodyFormFunc: req.BodyFormFunc,
- }
- // 赋值默认body
- if req.Body == nil {
- cacheBody, err := c.GetBytesBody()
- if err != nil {
- return nil, err
- }
- preparedRequest.Body = cacheBody.Bytes()
- }
- // 赋值默认headers
- preparedRequest.headers = c.GetHeader().Map()
- // 构造url
- if preparedRequest.UrlFormFunc != nil {
- formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.Url = formedUrl
- }
- // 构造body
- if preparedRequest.BodyFormFunc != nil {
- formedBody, err := preparedRequest.BodyFormFunc(c, preparedRequest.Body, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.Body = formedBody
- }
- // 构造headers
- if preparedRequest.HeadersFormFunc != nil {
- formedHeaders, err := preparedRequest.HeadersFormFunc(c, preparedRequest.headers, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.headers = formedHeaders
- }
- return preparedRequest, nil
- }
- type GetRequest struct {
- Url string
- QueryParams map[string]string
- PathParams map[string]string
- UrlFormFunc UrlFormFunc
- HeadersFormFunc HeadersFormFunc
- QueryParamsFormFunc QueryParamsFormFunc
- PathParamsFormFunc PathParamsFormFunc
- headers map[string]string
- response *http_client.Response
- }
- func (req *GetRequest) Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
- preparedRequest, err := req.prepare(c, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- response, err := request.Get(preparedRequest.Url,
- http_client.WithRequestQueryParams(preparedRequest.QueryParams),
- http_client.WithRequestPathParams(preparedRequest.PathParams),
- http_client.WithRequestHeaders(preparedRequest.headers))
- if err != nil {
- return nil, err
- }
- preparedRequest.response = response
- return preparedRequest, nil
- }
- func (req *GetRequest) Response() *http_client.Response {
- return req.response
- }
- func (req *GetRequest) prepare(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (*DeleteRequest, error) {
- preparedRequest := &DeleteRequest{
- Url: req.Url,
- QueryParams: req.QueryParams,
- PathParams: req.PathParams,
- UrlFormFunc: req.UrlFormFunc,
- HeadersFormFunc: req.HeadersFormFunc,
- QueryParamsFormFunc: req.QueryParamsFormFunc,
- PathParamsFormFunc: req.PathParamsFormFunc,
- }
- // 赋值默认查询参数
- if req.QueryParams == nil || len(req.QueryParams) == 0 {
- req.QueryParams = c.GetQueryParams().Map()
- }
- // 赋值默认路径参数
- if req.PathParams == nil || len(req.PathParams) == 0 {
- req.PathParams = c.GetPathParams().Map()
- }
- // 赋值默认headers
- preparedRequest.headers = c.GetHeader().Map()
- // 构造url
- if preparedRequest.UrlFormFunc != nil {
- formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.Url = formedUrl
- }
- // 构造查询参数
- if preparedRequest.QueryParamsFormFunc != nil {
- formedQueryParams, err := preparedRequest.QueryParamsFormFunc(c, preparedRequest.QueryParams, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.QueryParams = formedQueryParams
- }
- // 构造路径参数
- if preparedRequest.PathParamsFormFunc != nil {
- formedPathParams, err := preparedRequest.PathParamsFormFunc(c, preparedRequest.PathParams, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.PathParams = formedPathParams
- }
- // 构造headers
- if preparedRequest.HeadersFormFunc != nil {
- formedHeaders, err := preparedRequest.HeadersFormFunc(c, preparedRequest.headers, historyRequests, resultMap)
- if err != nil {
- return nil, err
- }
- preparedRequest.headers = formedHeaders
- }
- return preparedRequest, nil
- }
|