完成响应数据填充

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
}