feat: 升级HTTP请求, 支持自定义message location 和 解析整个响应body
This commit is contained in:
38
client.go
38
client.go
@ -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())
|
||||
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()
|
||||
businessData := gjson.Get(response.Data, hc.reqConfig.DataField)
|
||||
} 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{}
|
||||
|
||||
@ -21,9 +21,11 @@ type Request struct {
|
||||
FullUrl string `json:"full_url"` // 完整的请求URL
|
||||
ContentType string `json:"content_type"` // 请求类型
|
||||
Method string `json:"method"` // 请求方法
|
||||
DataField string `json:"data_field"` // 数据字段
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user