增加接口请求参数的构建逻辑

This commit is contained in:
2025-03-28 18:39:04 +08:00
parent 94cbccc047
commit b9bd189ade
5 changed files with 127 additions and 12 deletions

View File

@ -18,9 +18,9 @@ type Request struct {
Ctx context.Context `json:"-"` // 请求上下文
PathParam map[string]string `json:"path_param"` // 替换url中的占位符
Body map[string]any `json:"body"` // 请求Body
Header map[string]string `json:"header"` // 请求Header
Cookie map[string]string `json:"cookie"` // 请求Cookie
Query map[string]string `json:"query"` // 请求query
Header map[string]any `json:"header"` // 请求Header
Cookie map[string]any `json:"cookie"` // 请求Cookie
Query map[string]any `json:"query"` // 请求query
FullUrl string `json:"full_url"` // 完整的请求URL
ContentType string `json:"content_type"` // 请求类型
Method string `json:"method"` // 请求方法

View File

@ -58,11 +58,13 @@ type RequestConfigGroupItemFailBehavior struct {
//
// Date : 14:23 2025/3/28
type RequestConfigGroupItemParamRule struct {
Location consts.RequestDataLocation `json:"location"` // 参数位置
Path string `json:"path"` // 参数设置的路径
Type consts.DataType `json:"type"` // 参数类型
SourceAlias string `json:"source_alias"` // 数据源请求别名 __COMMON__ 代表从公共参数读取数据
SourceResultPath string `json:"source_result_path"` // 数据源数据, 从结果中获取的路径
Location consts.RequestDataLocation `json:"location"` // 参数位置
Path string `json:"path"` // 参数设置的路径
Type consts.DataType `json:"type"` // 参数类型
SourceAlias string `json:"source_alias"` // 数据源请求别名 __COMMON__ 代表从公共参数读取数据
SourceResultLocation string `json:"source_result_location"` // 数据源请求结果位置
SourceResultPath string `json:"source_result_path"` // 数据源数据, 从结果中获取的路径
DefaultValue string `json:"default_value"` // 默认值
}
// RequestConfigResultRule 返回值构建规则

View File

@ -9,8 +9,13 @@ package mesh
import (
"context"
"fmt"
"git.zhangdeman.cn/gateway/validate"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/network/httpclient"
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
"git.zhangdeman.cn/zhangdeman/serialize"
"strings"
"sync"
)
@ -47,6 +52,11 @@ func (c *client) Request() *Response {
break
}
}
if c.resp.FinalFailure {
// 请求失败
return c.resp
}
// 请求成功, 构建返回结果
return c.resp
}
@ -65,10 +75,21 @@ func (c *client) doRequest(apiList []*RequestConfigGroupItem) bool {
var (
err error
httpClient *httpclient.HttpClient
param map[string]map[string]any
)
// 初始化一下请求
c.initApiCfg(apiCfg)
// TODO: 构造生成请求参数
// 构造生成请求参数
if param, err = c.buildRequestParams(apiCfg); nil != err {
// 构造请求参数失败, 直接返回
c.resp.ErrorCode = "-500"
c.resp.ErrorMessage = err.Error()
return false
}
apiCfg.RequestCfg.Body = param[consts.RequestDataLocationBody.String()] // body
apiCfg.RequestCfg.Header = param[consts.RequestDataLocationHeader.String()] // header
apiCfg.RequestCfg.Cookie = param[consts.RequestDataLocationCookie.String()] // cookie
apiCfg.RequestCfg.Query = param[consts.RequestDataLocationQuery.String()] // query
if httpClient, err = httpclient.NewHttpClient(apiCfg.RequestCfg, apiCfg.CacheInstance); nil != err {
// 此处获取客户端实例即发生异常, 忽略一切配置, 直接作为全局失败, 后续也不请求了
c.resp.ErrorCode = "-500"
@ -125,3 +146,39 @@ func (c *client) initApiCfg(apiCfg *RequestConfigGroupItem) {
// 每一个请求有独立的context
apiCfg.RequestCfg.Ctx = context.WithValue(c.reqCfg.Ctx, "alias", apiCfg.Alias)
}
// buildRequestParams 构建请求参数 location => path: value
func (c *client) buildRequestParams(apiCfg *RequestConfigGroupItem) (map[string]map[string]any, error) {
sourceData := map[string]any{}
for aliasName, itemRes := range c.resp.AliasResultTable {
sourceData[aliasName] = map[string]any{
consts.ResponseDataLocationBody.String(): itemRes.Body[apiCfg.RequestCfg.DataField],
consts.ResponseDataLocationHeader.String(): itemRes.Header,
consts.ResponseDataLocationCookie.String(): itemRes.Cookie,
}
}
sourceDataByte := serialize.JSON.MarshalForByteIgnoreError(sourceData)
validateRuleList := make([]validate.StructField, 0)
for _, itemParam := range apiCfg.ParamRuleList {
arr := strings.Split(itemParam.Path, ".")
validateRuleList = append(validateRuleList, validate.StructField{
JsonTag: arr[len(arr)-1],
Type: itemParam.Type,
Required: false,
RuleList: nil,
DefaultValue: itemParam.DefaultValue,
SourcePath: fmt.Sprintf("%v.%v.%v", itemParam.SourceAlias, itemParam.SourceResultLocation, itemParam.SourceResultPath), // TODO : 公共参数特殊处理
TargetPath: fmt.Sprintf("%v.%v", itemParam.Location, itemParam.Path),
Errmsg: "",
})
}
buildRes, err := validate.Run(sourceDataByte, validateRuleList)
if nil != err {
return nil, err
}
var d map[string]map[string]any
if err = serialize.JSON.UnmarshalWithNumber(buildRes, &d); nil != err {
return nil, err
}
return d, err
}