|
|
@@ -5,14 +5,14 @@ import (
|
|
|
"git.sxidc.com/go-tools/utils/http_client"
|
|
|
)
|
|
|
|
|
|
-type UrlTransferFunc func(c *api.Context, url string, historyRequests []BuilderRequest, resultMap map[string]any) (string, error)
|
|
|
-type HeadersFormFunc func(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error)
|
|
|
-type BodyFormFunc func(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (any, error)
|
|
|
-type QueryParamsFormFunc func(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error)
|
|
|
-type PathParamsFormFunc func(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error)
|
|
|
+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, historyRequests []BuilderRequest, resultMap map[string]any) (any, error) {
|
|
|
+ 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
|
|
|
@@ -28,7 +28,7 @@ func FormJsonBodyWithTenantIDAndUserIDFunc(tenantIDFieldName string, userIDField
|
|
|
}
|
|
|
|
|
|
func FormQueryParamsWithTenantIDAndUserIDFunc(tenantIDFieldName string, userIDFieldName string) QueryParamsFormFunc {
|
|
|
- return func(c *api.Context, historyRequests []BuilderRequest, resultMap map[string]any) (map[string]string, error) {
|
|
|
+ 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
|
|
|
@@ -43,445 +43,382 @@ type BuilderRequest interface {
|
|
|
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
|
|
|
- }
|
|
|
+ Url string
|
|
|
+ Body any
|
|
|
+ UrlFormFunc UrlFormFunc
|
|
|
+ BodyFormFunc BodyFormFunc
|
|
|
+ HeadersFormFunc HeadersFormFunc
|
|
|
|
|
|
- return &PostRequest{
|
|
|
- url: url,
|
|
|
- options: options,
|
|
|
- }
|
|
|
+ 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) {
|
|
|
- url, err := req.options.urlTransferFunc(c, req.url, historyRequests, resultMap)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- headers, err := req.options.headersFormFunc(c, historyRequests, resultMap)
|
|
|
+ preparedRequest, err := req.prepare(c, historyRequests, resultMap)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- body, err := req.options.bodyFormFunc(c, historyRequests, resultMap)
|
|
|
+ response, err := request.Post(preparedRequest.Url, preparedRequest.Body,
|
|
|
+ http_client.WithRequestHeaders(preparedRequest.headers))
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- response, err := request.Post(url, body, http_client.WithRequestHeaders(headers))
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
+ preparedRequest.response = response
|
|
|
|
|
|
- return &PostRequest{
|
|
|
- url: url,
|
|
|
- response: response,
|
|
|
- options: &PostRequestOptions{
|
|
|
- urlTransferFunc: req.options.urlTransferFunc,
|
|
|
- headersFormFunc: req.options.headersFormFunc,
|
|
|
- bodyFormFunc: req.options.bodyFormFunc,
|
|
|
- },
|
|
|
- }, nil
|
|
|
+ return preparedRequest, 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 (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,
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-func DeleteRequestWithHeadersForm(headersFormFunc HeadersFormFunc) DeleteRequestOption {
|
|
|
- return func(options *DeleteRequestOptions) {
|
|
|
- options.headersFormFunc = headersFormFunc
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
-func DeleteRequestWithPathParamsForm(pathParamsFormFunc PathParamsFormFunc) DeleteRequestOption {
|
|
|
- return func(options *DeleteRequestOptions) {
|
|
|
- options.pathParamsFormFunc = pathParamsFormFunc
|
|
|
- }
|
|
|
-}
|
|
|
+ // 赋值默认body
|
|
|
+ if req.Body == nil {
|
|
|
+ cacheBody, err := c.GetBytesBody()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
-func DeleteRequestWithQueryParamsForm(queryParamsFormFunc QueryParamsFormFunc) DeleteRequestOption {
|
|
|
- return func(options *DeleteRequestOptions) {
|
|
|
- options.queryParamsFormFunc = queryParamsFormFunc
|
|
|
+ preparedRequest.Body = cacheBody.Bytes()
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-type DeleteRequest struct {
|
|
|
- url string
|
|
|
- response *http_client.Response
|
|
|
- options *DeleteRequestOptions
|
|
|
-}
|
|
|
+ // 赋值默认headers
|
|
|
+ preparedRequest.headers = c.GetHeader().Map()
|
|
|
|
|
|
-func NewDeleteRequest(url string, opts ...DeleteRequestOption) *DeleteRequest {
|
|
|
- options := new(DeleteRequestOptions)
|
|
|
+ // 构造url
|
|
|
+ if preparedRequest.UrlFormFunc != nil {
|
|
|
+ formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
- for _, opt := range opts {
|
|
|
- opt(options)
|
|
|
+ preparedRequest.Url = formedUrl
|
|
|
}
|
|
|
|
|
|
- if options.urlTransferFunc == nil {
|
|
|
- options.urlTransferFunc = defaultUrlTransferFunc
|
|
|
- }
|
|
|
+ // 构造body
|
|
|
+ if preparedRequest.BodyFormFunc != nil {
|
|
|
+ formedBody, err := preparedRequest.BodyFormFunc(c, preparedRequest.Body, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
- if options.headersFormFunc == nil {
|
|
|
- options.headersFormFunc = defaultHeadersFormFunc
|
|
|
+ preparedRequest.Body = formedBody
|
|
|
}
|
|
|
|
|
|
- if options.pathParamsFormFunc == nil {
|
|
|
- options.pathParamsFormFunc = defaultPathParamsFormFunc
|
|
|
- }
|
|
|
+ // 构造headers
|
|
|
+ if preparedRequest.HeadersFormFunc != nil {
|
|
|
+ formedHeaders, err := preparedRequest.HeadersFormFunc(c, preparedRequest.headers, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
- if options.queryParamsFormFunc == nil {
|
|
|
- options.queryParamsFormFunc = defaultQueryParamsFormFunc
|
|
|
+ preparedRequest.headers = formedHeaders
|
|
|
}
|
|
|
|
|
|
- return &DeleteRequest{
|
|
|
- url: url,
|
|
|
- options: options,
|
|
|
- }
|
|
|
+ return preparedRequest, nil
|
|
|
}
|
|
|
|
|
|
-func (req *DeleteRequest) Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
|
|
|
- url, err := req.options.urlTransferFunc(c, req.url, historyRequests, resultMap)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- headers, err := req.options.headersFormFunc(c, historyRequests, resultMap)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
+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
|
|
|
+}
|
|
|
|
|
|
- pathParams, err := req.options.pathParamsFormFunc(c, historyRequests, resultMap)
|
|
|
+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
|
|
|
}
|
|
|
|
|
|
- queryParams, err := req.options.queryParamsFormFunc(c, historyRequests, resultMap)
|
|
|
+ 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
|
|
|
}
|
|
|
|
|
|
- response, err := request.Delete(url,
|
|
|
- http_client.WithRequestHeaders(headers),
|
|
|
- http_client.WithRequestPathParams(pathParams),
|
|
|
- http_client.WithRequestQueryParams(queryParams))
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
+ preparedRequest.response = response
|
|
|
|
|
|
- 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
|
|
|
+ return preparedRequest, 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 (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,
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-func PutRequestWithHeadersForm(headersFormFunc HeadersFormFunc) PutRequestOption {
|
|
|
- return func(options *PutRequestOptions) {
|
|
|
- options.headersFormFunc = headersFormFunc
|
|
|
+ // 赋值默认查询参数
|
|
|
+ if req.QueryParams == nil || len(req.QueryParams) == 0 {
|
|
|
+ req.QueryParams = c.GetQueryParams().Map()
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-func PutRequestWithBodyForm(bodyFormFunc BodyFormFunc) PutRequestOption {
|
|
|
- return func(options *PutRequestOptions) {
|
|
|
- options.bodyFormFunc = bodyFormFunc
|
|
|
+ // 赋值默认路径参数
|
|
|
+ if req.PathParams == nil || len(req.PathParams) == 0 {
|
|
|
+ req.PathParams = c.GetPathParams().Map()
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-type PutRequest struct {
|
|
|
- url string
|
|
|
- response *http_client.Response
|
|
|
- options *PutRequestOptions
|
|
|
-}
|
|
|
+ // 赋值默认headers
|
|
|
+ preparedRequest.headers = c.GetHeader().Map()
|
|
|
|
|
|
-func NewPutRequest(url string, opts ...PutRequestOption) *PutRequest {
|
|
|
- options := new(PutRequestOptions)
|
|
|
+ // 构造url
|
|
|
+ if preparedRequest.UrlFormFunc != nil {
|
|
|
+ formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
- for _, opt := range opts {
|
|
|
- opt(options)
|
|
|
+ preparedRequest.Url = formedUrl
|
|
|
}
|
|
|
|
|
|
- if options.urlTransferFunc == nil {
|
|
|
- options.urlTransferFunc = defaultUrlTransferFunc
|
|
|
- }
|
|
|
+ // 构造查询参数
|
|
|
+ if preparedRequest.QueryParamsFormFunc != nil {
|
|
|
+ formedQueryParams, err := preparedRequest.QueryParamsFormFunc(c, preparedRequest.QueryParams, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
- if options.headersFormFunc == nil {
|
|
|
- options.headersFormFunc = defaultHeadersFormFunc
|
|
|
+ preparedRequest.QueryParams = formedQueryParams
|
|
|
}
|
|
|
|
|
|
- if options.bodyFormFunc == nil {
|
|
|
- options.bodyFormFunc = defaultBodyFormFunc
|
|
|
+ // 构造路径参数
|
|
|
+ if preparedRequest.PathParamsFormFunc != nil {
|
|
|
+ formedPathParams, err := preparedRequest.PathParamsFormFunc(c, preparedRequest.PathParams, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ preparedRequest.PathParams = formedPathParams
|
|
|
}
|
|
|
|
|
|
- return &PutRequest{
|
|
|
- url: url,
|
|
|
- options: options,
|
|
|
+ // 构造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
|
|
|
}
|
|
|
|
|
|
-func (req *PutRequest) Request(c *api.Context, request *http_client.Request, historyRequests []BuilderRequest, resultMap map[string]any) (BuilderRequest, error) {
|
|
|
- url, err := req.options.urlTransferFunc(c, req.url, historyRequests, resultMap)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
+type PutRequest struct {
|
|
|
+ Url string
|
|
|
+ Body any
|
|
|
+ UrlFormFunc UrlFormFunc
|
|
|
+ BodyFormFunc BodyFormFunc
|
|
|
+ HeadersFormFunc HeadersFormFunc
|
|
|
|
|
|
- headers, err := req.options.headersFormFunc(c, historyRequests, resultMap)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
+ headers map[string]string
|
|
|
+ response *http_client.Response
|
|
|
+}
|
|
|
|
|
|
- body, err := req.options.bodyFormFunc(c, historyRequests, resultMap)
|
|
|
+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(url, body, http_client.WithRequestHeaders(headers))
|
|
|
+ response, err := request.Put(preparedRequest.Url, preparedRequest.Body,
|
|
|
+ http_client.WithRequestHeaders(preparedRequest.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
|
|
|
+ preparedRequest.response = response
|
|
|
+
|
|
|
+ return preparedRequest, 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 (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,
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-func GetRequestWithHeadersForm(headersFormFunc HeadersFormFunc) GetRequestOption {
|
|
|
- return func(options *GetRequestOptions) {
|
|
|
- options.headersFormFunc = headersFormFunc
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
-func GetRequestWithPathParamsForm(pathParamsFormFunc PathParamsFormFunc) GetRequestOption {
|
|
|
- return func(options *GetRequestOptions) {
|
|
|
- options.pathParamsFormFunc = pathParamsFormFunc
|
|
|
- }
|
|
|
-}
|
|
|
+ // 赋值默认body
|
|
|
+ if req.Body == nil {
|
|
|
+ cacheBody, err := c.GetBytesBody()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
-func GetRequestWithQueryParamsForm(queryParamsFormFunc QueryParamsFormFunc) GetRequestOption {
|
|
|
- return func(options *GetRequestOptions) {
|
|
|
- options.queryParamsFormFunc = queryParamsFormFunc
|
|
|
+ preparedRequest.Body = cacheBody.Bytes()
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-type GetRequest struct {
|
|
|
- url string
|
|
|
- headers map[string]string
|
|
|
- pathParams map[string]string
|
|
|
- queryParams map[string]string
|
|
|
- response *http_client.Response
|
|
|
- options *GetRequestOptions
|
|
|
-}
|
|
|
+ // 赋值默认headers
|
|
|
+ preparedRequest.headers = c.GetHeader().Map()
|
|
|
|
|
|
-func NewGetRequest(url string, opts ...GetRequestOption) *GetRequest {
|
|
|
- options := new(GetRequestOptions)
|
|
|
+ // 构造url
|
|
|
+ if preparedRequest.UrlFormFunc != nil {
|
|
|
+ formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
- for _, opt := range opts {
|
|
|
- opt(options)
|
|
|
+ preparedRequest.Url = formedUrl
|
|
|
}
|
|
|
|
|
|
- if options.urlTransferFunc == nil {
|
|
|
- options.urlTransferFunc = defaultUrlTransferFunc
|
|
|
- }
|
|
|
+ // 构造body
|
|
|
+ if preparedRequest.BodyFormFunc != nil {
|
|
|
+ formedBody, err := preparedRequest.BodyFormFunc(c, preparedRequest.Body, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
- if options.headersFormFunc == nil {
|
|
|
- options.headersFormFunc = defaultHeadersFormFunc
|
|
|
+ preparedRequest.Body = formedBody
|
|
|
}
|
|
|
|
|
|
- if options.pathParamsFormFunc == nil {
|
|
|
- options.pathParamsFormFunc = defaultPathParamsFormFunc
|
|
|
- }
|
|
|
+ // 构造headers
|
|
|
+ if preparedRequest.HeadersFormFunc != nil {
|
|
|
+ formedHeaders, err := preparedRequest.HeadersFormFunc(c, preparedRequest.headers, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
- if options.queryParamsFormFunc == nil {
|
|
|
- options.queryParamsFormFunc = defaultQueryParamsFormFunc
|
|
|
+ preparedRequest.headers = formedHeaders
|
|
|
}
|
|
|
|
|
|
- return &GetRequest{
|
|
|
- url: url,
|
|
|
- options: options,
|
|
|
- }
|
|
|
+ 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) {
|
|
|
- url, err := req.options.urlTransferFunc(c, req.url, historyRequests, resultMap)
|
|
|
+ preparedRequest, err := req.prepare(c, historyRequests, resultMap)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- headers, err := req.options.headersFormFunc(c, historyRequests, resultMap)
|
|
|
+ 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
|
|
|
}
|
|
|
|
|
|
- pathParams, err := req.options.pathParamsFormFunc(c, historyRequests, resultMap)
|
|
|
- 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,
|
|
|
}
|
|
|
|
|
|
- queryParams, err := req.options.queryParamsFormFunc(c, historyRequests, resultMap)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
+ // 赋值默认查询参数
|
|
|
+ if req.QueryParams == nil || len(req.QueryParams) == 0 {
|
|
|
+ req.QueryParams = c.GetQueryParams().Map()
|
|
|
}
|
|
|
|
|
|
- response, err := request.Get(url,
|
|
|
- http_client.WithRequestHeaders(headers),
|
|
|
- http_client.WithRequestPathParams(pathParams),
|
|
|
- http_client.WithRequestQueryParams(queryParams))
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
+ // 赋值默认路径参数
|
|
|
+ if req.PathParams == nil || len(req.PathParams) == 0 {
|
|
|
+ req.PathParams = c.GetPathParams().Map()
|
|
|
}
|
|
|
|
|
|
- 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
|
|
|
-}
|
|
|
+ // 赋值默认headers
|
|
|
+ preparedRequest.headers = c.GetHeader().Map()
|
|
|
|
|
|
-func (req *GetRequest) Response() *http_client.Response {
|
|
|
- return req.response
|
|
|
-}
|
|
|
+ // 构造url
|
|
|
+ if preparedRequest.UrlFormFunc != nil {
|
|
|
+ formedUrl, err := preparedRequest.UrlFormFunc(c, preparedRequest.Url, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
-func defaultUrlTransferFunc(_ *api.Context, url string, _ []BuilderRequest, _ map[string]any) (string, error) {
|
|
|
- return url, nil
|
|
|
-}
|
|
|
+ preparedRequest.Url = formedUrl
|
|
|
+ }
|
|
|
|
|
|
-func defaultHeadersFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) {
|
|
|
- return c.GetHeaders(), nil
|
|
|
-}
|
|
|
+ // 构造查询参数
|
|
|
+ if preparedRequest.QueryParamsFormFunc != nil {
|
|
|
+ formedQueryParams, err := preparedRequest.QueryParamsFormFunc(c, preparedRequest.QueryParams, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
-func defaultBodyFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (any, error) {
|
|
|
- cachedBody, err := c.GetBytesBody()
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
+ preparedRequest.QueryParams = formedQueryParams
|
|
|
}
|
|
|
|
|
|
- return cachedBody.Bytes(), nil
|
|
|
-}
|
|
|
+ // 构造路径参数
|
|
|
+ if preparedRequest.PathParamsFormFunc != nil {
|
|
|
+ formedPathParams, err := preparedRequest.PathParamsFormFunc(c, preparedRequest.PathParams, historyRequests, resultMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
-func defaultQueryParamsFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) {
|
|
|
- return c.GetQueryParams().Map(), nil
|
|
|
-}
|
|
|
+ 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
|
|
|
+ }
|
|
|
|
|
|
-func defaultPathParamsFormFunc(c *api.Context, _ []BuilderRequest, _ map[string]any) (map[string]string, error) {
|
|
|
- return c.GetPathParams().Map(), nil
|
|
|
+ return preparedRequest, nil
|
|
|
}
|