完成响应数据填充

This commit is contained in:
白茶清欢 2024-06-05 21:54:06 +08:00
parent 81d01affc9
commit ee1dce2b5e
3 changed files with 91 additions and 10 deletions

View File

@ -13,6 +13,7 @@ import (
"git.zhangdeman.cn/gateway/httpclient/validate"
"git.zhangdeman.cn/zhangdeman/serialize"
"github.com/go-resty/resty/v2"
"github.com/tidwall/gjson"
"strings"
"time"
)
@ -154,12 +155,38 @@ func (hc *HttpClient) Request() *define.Response {
err error
)
response := hc.newResponse()
for i := 0; i < hc.reqConfig.RetryRule.RetryCount+1; i++ {
response := hc.newResponse(i+1, hc.reqConfig.RetryRule.RetryCount+1)
response.Seq++
response.RequestCount++
response.Header = map[string]string{}
response.Cookie = map[string]string{}
response.Body = map[string]any{}
if response.RestyResponse, err = hc.request.Send(); nil != err {
response.FailInfo = &define.ResponseFailInfo{
Type: define.RequestFailTypeSend,
Message: "response instance is nil",
}
for _, itemAfterResponse := range hc.requestFinishHandler {
itemAfterResponse(hc.reqConfig, response)
}
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
continue
}
if nil == response.RestyResponse {
response.FailInfo = &define.ResponseFailInfo{
Type: define.RequestFailTypeSend,
Message: "response instance is nil",
}
for _, itemAfterResponse := range hc.requestFinishHandler {
itemAfterResponse(hc.reqConfig, response)
}
time.Sleep(time.Duration(hc.reqConfig.RetryRule.RetryTimeInterval) * time.Millisecond)
continue
}
// 解析返回信息
// 设置缓存
response.IsCache, response.CacheError = hc.setCacheResult(response)
}
@ -172,7 +199,7 @@ func (hc *HttpClient) Request() *define.Response {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:44 2024/6/1
func (hc *HttpClient) newResponse(seq int, requestCount int) *define.Response {
func (hc *HttpClient) newResponse() *define.Response {
return &define.Response{
Header: map[string]string{},
Cookie: map[string]string{},
@ -183,17 +210,69 @@ func (hc *HttpClient) newResponse(seq int, requestCount int) *define.Response {
HttpCode: 0,
HttpCodeStatus: "",
ResponseDataRule: nil,
Seq: seq,
Seq: 0,
RequestStartTime: 0,
RequestFinishTime: 0,
UsedTime: 0,
RestyResponse: nil,
IsSuccess: false,
RequestCount: requestCount,
RequestCount: 0,
FailInfo: nil,
}
}
// fillResponseHeader 填充响应header
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:30 2024/6/5
func (hc *HttpClient) fillResponseHeader(response *define.Response) {
response.Header = map[string]string{} // 清空已有数据
response.HttpCode = response.RestyResponse.StatusCode() // http状态码
response.HttpCodeStatus = response.RestyResponse.Status() // http状态码描述
for headerName, headerValue := range response.RestyResponse.Header() {
if len(headerValue) > 0 {
response.Header[headerName] = headerValue[0]
} else {
response.Header[headerName] = ""
}
}
}
// fillResponseCookie 填充cookie
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:32 2024/6/5
func (hc *HttpClient) fillResponseCookie(response *define.Response) {
response.Cookie = map[string]string{} // 清空已有数据
for _, cookieValue := range response.RestyResponse.Cookies() {
response.Cookie[cookieValue.Name] = cookieValue.Value
}
}
// fillResponseBody 填充响应body
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:38 2024/6/5
func (hc *HttpClient) fillResponseBody(response *define.Response) {
response.Body = string(response.RestyResponse.Body())
response.Code = gjson.Get(response.Body, hc.reqConfig.CodeField).String()
response.Message = gjson.Get(response.Body, hc.reqConfig.MessageField).String()
response.Data = gjson.Get(response.Body, hc.reqConfig.DataField).String()
response.ExtendData = map[string]string{}
gjson.Parse(response.Body).ForEach(func(key, value gjson.Result) bool {
if key.String() == hc.reqConfig.CodeField ||
key.String() == hc.reqConfig.MessageField ||
key.String() == hc.reqConfig.DataField {
return true
}
response.ExtendData[key.String()] = value.String()
return true
})
}
// getCacheResult 获取缓存结果
//
// Author : go_developer@163.com<白茶清欢>
@ -209,7 +288,7 @@ func (hc *HttpClient) getCacheResult() *define.Response {
if len(cacheValue) == 0 {
return nil
}
response := hc.newResponse(-1, -1)
response := hc.newResponse()
if err := serialize.JSON.UnmarshalWithNumber([]byte(cacheValue), response); nil != err {
return nil
}

View File

@ -24,6 +24,7 @@ type Request struct {
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"` // 读取超时时间

View File

@ -19,10 +19,11 @@ import (
type Response struct {
Header map[string]string `json:"header"` // 响应header
Cookie map[string]string `json:"cookie"` // 响应cookie
Body map[string]any `json:"body"` // 响应body
Data string `json:"data"` // 响应body
Code string `json:"code"` // 业务状态码
Message string `json:"message"` // 业务状态码描述
Data string `json:"data"` // 响应数据
Body string `json:"body"` // 响应数据
ExtendData map[string]string `json:"extend_data"` // 除去 code / message / data 之外的其他数据
HttpCode int `json:"http_code"` // http状态码
HttpCodeStatus string `json:"http_code_status"` // http状态码描述
ResponseDataRule map[string]any `json:"response_data_rule"` // 返回数据的验证规则
@ -33,7 +34,7 @@ type Response struct {
RestyResponse *resty.Response `json:"-"` // 请求返回
IsSuccess bool `json:"is_success"` // 是否请求成功
RequestCount int `json:"request_count"` // 请求次数
FailInfo *ResponseFileInfo `json:"fail_info"` // 请求失败信息记录
FailInfo *ResponseFailInfo `json:"fail_info"` // 请求失败信息记录
IsCache bool `json:"-"` // 是否命中缓存
CacheKey string `json:"-"` // 缓存key
CacheValue string `json:"-"` // 缓存值
@ -41,12 +42,12 @@ type Response struct {
CacheError error `json:"-"` // 缓存是否异常
}
// ResponseFileInfo 失败信息
// ResponseFailInfo 失败信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:48 2024/6/1
type ResponseFileInfo struct {
type ResponseFailInfo struct {
Type string `json:"type"` // 失败类型
Message string `json:"message"` // 失败信息
}