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

View File

@ -15,23 +15,24 @@ import "context"
//
// Date : 17:10 2024/5/24
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]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"` // 请求方法
DataField string `json:"data_field"` // 数据字段
CodeField string `json:"code_field"` // 业务状态码字段
MessageField string `json:"message_field"` // code描述字段
DataReceiver any `json:"-"` // 响应data部分数据解析
SuccessCodeList []string `json:"success_code_list"` // 哪些业务状态码视为成功
ConnectTimeout int64 `json:"connect_timeout"` // 连接超时时间: ms
ReadTimeout int64 `json:"read_timeout"` // 读取超时时间
RetryRule *RequestRetryRule `json:"retry_rule"` // 重试规则
Ctx context.Context `json:"-"` // 请求上下文
PathParam map[string]string `json:"path_param"` // 替换url中的占位符
Body map[string]any `json:"body"` // 请求Body
Header map[string]any `json:"header"` // 请求Header
Cookie map[string]any `json:"cookie"` // 请求Cookie
Query map[string]any `json:"query"` // 请求query
Static map[string]map[string]any `json:"static"` // 静态参数: location => valName => val
FullUrl string `json:"full_url"` // 完整的请求URL
ContentType string `json:"content_type"` // 请求类型
Method string `json:"method"` // 请求方法
DataField string `json:"data_field"` // 数据字段
CodeField string `json:"code_field"` // 业务状态码字段
MessageField string `json:"message_field"` // code描述字段
DataReceiver any `json:"-"` // 响应data部分数据解析
SuccessCodeList []string `json:"success_code_list"` // 哪些业务状态码视为成功
ConnectTimeout int64 `json:"connect_timeout"` // 连接超时时间: ms
ReadTimeout int64 `json:"read_timeout"` // 读取超时时间
RetryRule *RequestRetryRule `json:"retry_rule"` // 重试规则
}
// RequestRetryRule 重试规则

View File

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

View File

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

View File

@ -36,6 +36,28 @@ func Request(req *RequestConfig, receiver any) *Response {
reqCfg: req,
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()
}
@ -152,9 +174,10 @@ func (c *client) buildRequestParams(apiCfg *RequestConfigGroupItem) (map[string]
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,
strings.ToLower(consts.ResponseDataLocationBody.String()): itemRes.Body[apiCfg.RequestCfg.DataField],
strings.ToLower(consts.ResponseDataLocationHeader.String()): itemRes.Header,
strings.ToLower(consts.ResponseDataLocationCookie.String()): itemRes.Cookie,
strings.ToLower(consts.RequestDataLocationQuery.String()): itemRes.Query,
}
}
sourceDataByte := serialize.JSON.MarshalForByteIgnoreError(sourceData)
@ -162,7 +185,8 @@ func (c *client) buildRequestParams(apiCfg *RequestConfigGroupItem) (map[string]
for _, itemParam := range apiCfg.ParamRuleList {
arr := strings.Split(itemParam.Path, ".")
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{
JsonTag: arr[len(arr)-1],
Type: itemParam.Type,
@ -170,10 +194,11 @@ func (c *client) buildRequestParams(apiCfg *RequestConfigGroupItem) (map[string]
RuleList: nil,
DefaultValue: itemParam.DefaultValue,
SourcePath: sourcePath,
TargetPath: fmt.Sprintf("%v.%v", itemParam.Location, itemParam.Path),
TargetPath: targetPath,
Errmsg: "",
})
}
buildRes, err := validate.Run(sourceDataByte, validateRuleList)
if nil != err {
return nil, err