Compare commits
5 Commits
7b1a36a7c3
...
master
Author | SHA1 | Date | |
---|---|---|---|
0e6bd9e669 | |||
0b2546217c | |||
42b9f26a9a | |||
5ddab034f3 | |||
a56528f2ee |
@ -455,7 +455,7 @@ func (hc *HttpClient) newResponse() *define.Response {
|
||||
//
|
||||
// Date : 16:04 2024/6/3
|
||||
func (hc *HttpClient) getCacheResult() *define.Response {
|
||||
if nil == hc.reqOption.CacheInstance {
|
||||
if nil == hc.reqOption || nil == hc.reqOption.CacheInstance {
|
||||
log.RecordDebug("接口请求前缓存检测, 未设置缓存实例", map[string]any{}, hc.reqCfg)
|
||||
return nil
|
||||
}
|
||||
|
@ -30,6 +30,7 @@ type Request struct {
|
||||
Method string `json:"method"` // 请求方法
|
||||
DataField string `json:"data_field"` // 数据字段
|
||||
CodeField string `json:"code_field"` // 业务状态码字段
|
||||
CodeLocation string `json:"code_location"` // 业务状态码位置
|
||||
MessageField string `json:"message_field"` // code描述字段
|
||||
DataReceiver any `json:"-"` // 响应data部分数据解析
|
||||
SuccessHttpCodeList []int `json:"success_http_code_list"` // 哪些http状态码视为成功, 不配置, 默认2xx
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/gjson"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -67,6 +68,9 @@ func (r *Response) fillResponseBody(reqCfg *define.Request, response *define.Res
|
||||
responseContentType := response.RestyResponse.Header().Get(consts.HeaderKeyContentType.String())
|
||||
if responseContentType == "" {
|
||||
// 返回数据未说明 Content-Type
|
||||
if response.RestyResponse.StatusCode() != http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
return errors.New("response content type is empty")
|
||||
}
|
||||
typeArr := strings.Split(strings.Split(responseContentType, ";")[0], "/")
|
||||
@ -94,8 +98,20 @@ func (r *Response) fillResponseBody(reqCfg *define.Request, response *define.Res
|
||||
return errors.New("response body Marshal error :" + err.Error())
|
||||
}
|
||||
response.Data = string(jsonByte)
|
||||
if strings.ToLower(reqCfg.CodeLocation) == "header" {
|
||||
if reqCfg.CodeField == "code" {
|
||||
response.Code = fmt.Sprintf("%v", response.HttpCode)
|
||||
response.Message = response.RestyResponse.Status()
|
||||
} else {
|
||||
response.Code = response.RestyResponse.Header().Get(reqCfg.CodeField)
|
||||
response.Message = response.RestyResponse.Header().Get(reqCfg.MessageField)
|
||||
}
|
||||
} else {
|
||||
// 统一认为Body
|
||||
response.Code = gjson.Get(response.Data, reqCfg.CodeField).String()
|
||||
response.Message = gjson.Get(response.Data, reqCfg.MessageField).String()
|
||||
}
|
||||
|
||||
businessData := gjson.Get(response.Data, reqCfg.DataField)
|
||||
if businessData.Value() == nil {
|
||||
// data为空指针, 归一化成空对象
|
||||
|
@ -83,7 +83,7 @@ func NewRestyClient(reqConfig *define.Request, reqOption *RequestOption) (*resty
|
||||
}
|
||||
// 限流处理, 增加限流中间件
|
||||
client.AddRequestMiddleware(func(client *resty.Client, request *resty.Request) error {
|
||||
if nil != reqOption && nil != reqOption.RateLimiter {
|
||||
if nil == reqOption || nil == reqOption.RateLimiter {
|
||||
// 未配置流控
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user