Merge pull request 'feat: 升级HTTP请求, 支持自定义message location 和 解析整个响应body' (#2) from feature/upgrade_response_parse into master

Reviewed-on: #2
This commit is contained in:
2025-12-13 22:16:30 +08:00
2 changed files with 51 additions and 25 deletions

View File

@ -9,15 +9,17 @@ package httpclient
import (
"fmt"
"git.zhangdeman.cn/gateway/httpclient/cache"
"git.zhangdeman.cn/gateway/httpclient/define"
"git.zhangdeman.cn/gateway/httpclient/validate"
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
"net/http"
"strings"
"time"
"git.zhangdeman.cn/gateway/httpclient/cache"
"git.zhangdeman.cn/gateway/httpclient/define"
"git.zhangdeman.cn/gateway/httpclient/validate"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
)
// NewHttpClient 获取http client
@ -329,9 +331,31 @@ func (hc *HttpClient) fillResponseCookie(response *define.Response) {
// Date : 21:38 2024/6/5
func (hc *HttpClient) fillResponseBody(response *define.Response) {
response.Data = string(response.RestyResponse.Body())
response.Code = gjson.Get(response.Data, hc.reqConfig.CodeField).String()
response.Message = gjson.Get(response.Data, hc.reqConfig.MessageField).String()
businessData := gjson.Get(response.Data, hc.reqConfig.DataField)
if hc.reqConfig.CodeFieldLocation == consts.ResponseDataLocationBody.String() {
// 状态码位置在body
response.Code = gjson.Get(response.Data, hc.reqConfig.CodeField).String()
} else {
// 非 Body 认为是 header
response.Code = fmt.Sprintf("%v", response.HttpCode)
}
if hc.reqConfig.MessageFieldLocation == "" {
// 未单独指定 message 字段位置, 则和code 同位置
hc.reqConfig.MessageFieldLocation = hc.reqConfig.CodeFieldLocation
}
if hc.reqConfig.CodeFieldLocation == consts.ResponseDataLocationBody.String() {
// 状态码位置在body
response.Message = gjson.Get(response.Data, hc.reqConfig.MessageField).String()
} else {
// 非 Body 认为是 header
response.Message = response.RestyResponse.Status()
}
var businessData gjson.Result
if hc.reqConfig.DataField == "" || hc.reqConfig.DataField == consts.ResponseDataLocationBodyRoot.String() {
// 整个 Body 均是数据
businessData = gjson.Parse(response.Data)
} else {
businessData = gjson.Get(response.Data, hc.reqConfig.DataField)
}
if businessData.Value() == nil {
// data为空指针, 归一化成空对象
response.Body = map[string]any{}

View File

@ -13,22 +13,24 @@ package define
//
// Date : 17:10 2024/5/24
type Request struct {
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
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 interface{} `json:"-"` // 响应data部分数据解析
SuccessCodeList []string `json:"success_code_list"` // 哪些业务状态码视为成功
ConnectTimeout int64 `json:"connect_timeout"` // 连接超时时间: ms
ReadTimeout int64 `json:"read_timeout"` // 读取超时时间
RetryRule *RequestRetryRule `json:"retry_rule"` // 重试规则
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
FullUrl string `json:"full_url"` // 完整的请求URL
ContentType string `json:"content_type"` // 请求类型
Method string `json:"method"` // 请求方法
DataField string `json:"data_field"` // 数据字段 BODY_ROOT 代表整个BODY 都是带解析数据
CodeField string `json:"code_field"` // 业务状态码字段
CodeFieldLocation string `json:"code_field_location"` // 业务状态码字段位置
MessageField string `json:"message_field"` // code描述字段
MessageFieldLocation string `json:"message_field_location"` // code 描述字段位置
DataReceiver interface{} `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 重试规则