httpclient支持mesh请求 #2

Merged
zhangdeman merged 26 commits from feature/upgrade_httpclient into master 2025-04-01 12:16:20 +08:00
5 changed files with 56 additions and 29 deletions
Showing only changes of commit 5d790ff1e7 - Show all commits

View File

@ -264,8 +264,8 @@ func (hc *HttpClient) requestBackendApi() *define.Response {
// Date : 17:44 2024/6/1 // Date : 17:44 2024/6/1
func (hc *HttpClient) newResponse() *define.Response { func (hc *HttpClient) newResponse() *define.Response {
return &define.Response{ return &define.Response{
Header: map[string]string{}, Header: map[string]any{},
Cookie: map[string]string{}, Cookie: map[string]any{},
Body: map[string]any{}, Body: map[string]any{},
Code: "", Code: "",
Message: "", Message: "",
@ -298,7 +298,7 @@ func (hc *HttpClient) newResponse() *define.Response {
// //
// Date : 21:30 2024/6/5 // Date : 21:30 2024/6/5
func (hc *HttpClient) fillResponseHeader(response *define.Response) { func (hc *HttpClient) fillResponseHeader(response *define.Response) {
response.Header = map[string]string{} // 清空已有数据 response.Header = map[string]any{} // 清空已有数据
response.HttpCode = response.RestyResponse.StatusCode() // http状态码 response.HttpCode = response.RestyResponse.StatusCode() // http状态码
response.HttpCodeStatus = response.RestyResponse.Status() // http状态码描述 response.HttpCodeStatus = response.RestyResponse.Status() // http状态码描述
for headerName, headerValue := range response.RestyResponse.Header() { for headerName, headerValue := range response.RestyResponse.Header() {
@ -316,7 +316,7 @@ func (hc *HttpClient) fillResponseHeader(response *define.Response) {
// //
// Date : 21:32 2024/6/5 // Date : 21:32 2024/6/5
func (hc *HttpClient) fillResponseCookie(response *define.Response) { func (hc *HttpClient) fillResponseCookie(response *define.Response) {
response.Cookie = map[string]string{} // 清空已有数据 response.Cookie = map[string]any{} // 清空已有数据
for _, cookieValue := range response.RestyResponse.Cookies() { for _, cookieValue := range response.RestyResponse.Cookies() {
response.Cookie[cookieValue.Name] = cookieValue.Value response.Cookie[cookieValue.Name] = cookieValue.Value
} }

View File

@ -21,6 +21,7 @@ type Request struct {
Header map[string]any `json:"header"` // 请求Header Header map[string]any `json:"header"` // 请求Header
Cookie map[string]any `json:"cookie"` // 请求Cookie Cookie map[string]any `json:"cookie"` // 请求Cookie
Query map[string]any `json:"query"` // 请求query Query map[string]any `json:"query"` // 请求query
Static map[string]map[string]any `json:"static"` // 静态参数: location => valName => val
FullUrl string `json:"full_url"` // 完整的请求URL FullUrl string `json:"full_url"` // 完整的请求URL
ContentType string `json:"content_type"` // 请求类型 ContentType string `json:"content_type"` // 请求类型
Method string `json:"method"` // 请求方法 Method string `json:"method"` // 请求方法

View File

@ -17,8 +17,9 @@ import (
// //
// Date : 12:34 2024/5/31 // Date : 12:34 2024/5/31
type Response struct { type Response struct {
Header map[string]string `json:"header"` // 响应header Query map[string]any `json:"query"` // 请求query, 模拟自循环的请求
Cookie map[string]string `json:"cookie"` // 响应cookie Header map[string]any `json:"header"` // 响应header
Cookie map[string]any `json:"cookie"` // 响应cookie
Data string `json:"data"` // 响应body Data string `json:"data"` // 响应body
Code string `json:"code"` // 业务状态码 Code string `json:"code"` // 业务状态码
Message string `json:"message"` // 业务状态码描述 Message string `json:"message"` // 业务状态码描述

View File

@ -22,7 +22,7 @@ import (
// Date : 14:14 2025/3/28 // Date : 14:14 2025/3/28
type RequestConfig struct { type RequestConfig struct {
Ctx context.Context `json:"-"` // 上下文 Ctx context.Context `json:"-"` // 上下文
CommonParam map[string]any `json:"common_param"` // 公共参数 CommonParam map[string]map[string]any `json:"common_param"` // 公共参数
Group [][]*RequestConfigGroupItem `json:"group"` // 请求分组 Group [][]*RequestConfigGroupItem `json:"group"` // 请求分组
ResultRule []*RequestConfigResultRule `json:"result_rule"` // 结果提取规则, 多个接口的结果构造成一个返回结果 ResultRule []*RequestConfigResultRule `json:"result_rule"` // 结果提取规则, 多个接口的结果构造成一个返回结果
} }

View File

@ -36,6 +36,28 @@ func Request(req *RequestConfig, receiver any) *Response {
reqCfg: req, reqCfg: req,
receiver: receiver, receiver: receiver,
} }
c.resp.AliasResultTable[CommonServiceAlias] = &define.Response{
Query: c.reqCfg.CommonParam[consts.RequestDataLocationQuery.String()],
Header: c.reqCfg.CommonParam[consts.RequestDataLocationHeader.String()],
Cookie: c.reqCfg.CommonParam[consts.RequestDataLocationCookie.String()],
Data: "",
Code: "",
Message: "",
Body: c.reqCfg.CommonParam[consts.RequestDataLocationBody.String()],
ExtendData: nil,
HttpCode: 0,
HttpCodeStatus: "",
ResponseDataRule: nil,
Seq: 0,
RequestStartTime: 0,
RequestFinishTime: 0,
UsedTime: 0,
RestyResponse: nil,
IsSuccess: false,
RequestCount: 0,
FailInfo: nil,
CacheInfo: nil,
}
return c.Request() return c.Request()
} }
@ -152,9 +174,10 @@ func (c *client) buildRequestParams(apiCfg *RequestConfigGroupItem) (map[string]
sourceData := map[string]any{} sourceData := map[string]any{}
for aliasName, itemRes := range c.resp.AliasResultTable { for aliasName, itemRes := range c.resp.AliasResultTable {
sourceData[aliasName] = map[string]any{ sourceData[aliasName] = map[string]any{
consts.ResponseDataLocationBody.String(): itemRes.Body[apiCfg.RequestCfg.DataField], strings.ToLower(consts.ResponseDataLocationBody.String()): itemRes.Body[apiCfg.RequestCfg.DataField],
consts.ResponseDataLocationHeader.String(): itemRes.Header, strings.ToLower(consts.ResponseDataLocationHeader.String()): itemRes.Header,
consts.ResponseDataLocationCookie.String(): itemRes.Cookie, strings.ToLower(consts.ResponseDataLocationCookie.String()): itemRes.Cookie,
strings.ToLower(consts.RequestDataLocationQuery.String()): itemRes.Query,
} }
} }
sourceDataByte := serialize.JSON.MarshalForByteIgnoreError(sourceData) sourceDataByte := serialize.JSON.MarshalForByteIgnoreError(sourceData)
@ -162,7 +185,8 @@ func (c *client) buildRequestParams(apiCfg *RequestConfigGroupItem) (map[string]
for _, itemParam := range apiCfg.ParamRuleList { for _, itemParam := range apiCfg.ParamRuleList {
arr := strings.Split(itemParam.Path, ".") arr := strings.Split(itemParam.Path, ".")
sourcePath := itemParam.SourceResultPath sourcePath := itemParam.SourceResultPath
sourcePath = fmt.Sprintf("%v.%v.%v", itemParam.SourceAlias, itemParam.SourceResultLocation, itemParam.SourceResultPath) sourcePath = fmt.Sprintf("%v.%v.%v", itemParam.SourceAlias, strings.ToLower(itemParam.SourceResultLocation), itemParam.SourceResultPath)
targetPath := fmt.Sprintf("%v.%v", strings.ToLower(itemParam.Location.String()), itemParam.Path)
validateRuleList = append(validateRuleList, validate.StructField{ validateRuleList = append(validateRuleList, validate.StructField{
JsonTag: arr[len(arr)-1], JsonTag: arr[len(arr)-1],
Type: itemParam.Type, Type: itemParam.Type,
@ -170,10 +194,11 @@ func (c *client) buildRequestParams(apiCfg *RequestConfigGroupItem) (map[string]
RuleList: nil, RuleList: nil,
DefaultValue: itemParam.DefaultValue, DefaultValue: itemParam.DefaultValue,
SourcePath: sourcePath, SourcePath: sourcePath,
TargetPath: fmt.Sprintf("%v.%v", itemParam.Location, itemParam.Path), TargetPath: targetPath,
Errmsg: "", Errmsg: "",
}) })
} }
buildRes, err := validate.Run(sourceDataByte, validateRuleList) buildRes, err := validate.Run(sourceDataByte, validateRuleList)
if nil != err { if nil != err {
return nil, err return nil, err