支持响应数据类型自适应解析
This commit is contained in:
@ -8,21 +8,23 @@
|
||||
package implement
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/network/httpclient/define"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/gjson"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Response 响应结果解析
|
||||
type Response struct {
|
||||
}
|
||||
|
||||
func (r *Response) Parse(reqConfig *define.Request, response *define.Response) {
|
||||
func (r *Response) Parse(reqConfig *define.Request, response *define.Response) error {
|
||||
r.fillResponseHeader(response)
|
||||
r.fillResponseCookie(response)
|
||||
r.fillResponseBody(reqConfig, response)
|
||||
return
|
||||
return r.fillResponseBody(reqConfig, response)
|
||||
}
|
||||
|
||||
// fillResponseHeader 填充响应header
|
||||
@ -60,8 +62,38 @@ func (r *Response) fillResponseCookie(response *define.Response) {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:38 2024/6/5
|
||||
func (r *Response) fillResponseBody(reqCfg *define.Request, response *define.Response) {
|
||||
response.Data = string(response.RestyResponse.Body())
|
||||
func (r *Response) fillResponseBody(reqCfg *define.Request, response *define.Response) error {
|
||||
// 解析响应数据类型
|
||||
responseContentType := response.RestyResponse.Header().Get(consts.HeaderKeyContentType.String())
|
||||
if responseContentType == "" {
|
||||
// 返回数据未说明 Content-Type
|
||||
return errors.New("response content type is empty")
|
||||
}
|
||||
typeArr := strings.Split(strings.Split(responseContentType, ";")[0], "/")
|
||||
responseType := "json"
|
||||
if len(typeArr) > 0 {
|
||||
responseType = typeArr[len(typeArr)-1]
|
||||
}
|
||||
parser := ResponseParserTable[responseType]
|
||||
if parser == nil {
|
||||
// 未读取到的, 不支持解析对应的响应数据
|
||||
return errors.New("response type " + responseType + " is not support")
|
||||
|
||||
}
|
||||
|
||||
var (
|
||||
err error
|
||||
res map[string]any
|
||||
jsonByte []byte
|
||||
)
|
||||
|
||||
if err = parser.Unmarshal(response.RestyResponse.Body(), &res); nil != err {
|
||||
return errors.New("response parse body error :" + err.Error())
|
||||
}
|
||||
if jsonByte, err = parser.MarshalForByte(res); nil != err {
|
||||
return errors.New("response body Marshal error :" + err.Error())
|
||||
}
|
||||
response.Data = string(jsonByte)
|
||||
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)
|
||||
@ -96,6 +128,7 @@ func (r *Response) fillResponseBody(reqCfg *define.Request, response *define.Res
|
||||
response.ExtendData[key.String()] = value.String()
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// BusinessSuccess ...
|
||||
|
@ -13,6 +13,10 @@ import (
|
||||
)
|
||||
|
||||
// ResponseParserTable 响应数据解析器
|
||||
// 此处为内置解析实现, 未加锁, 未做并发安全处理
|
||||
// 原因: 无论 Add 还是 Delete, 均应在服务初始化阶段调用, 而在服务完成初始化, 完全启动之后, 应该只有读操作
|
||||
// 如果强要运行时, 动态操作此配置表, 需在外部调用处自行加锁
|
||||
// 也可完全自行实现相关解析实例, 在请求时通过 RequestOption 传入, 则控制权完全贵开发者所有
|
||||
var ResponseParserTable = map[string]abstract.IResponseParser{
|
||||
"json": serialize.JSON,
|
||||
"xml": serialize.Xml,
|
||||
@ -21,3 +25,13 @@ var ResponseParserTable = map[string]abstract.IResponseParser{
|
||||
"yaml": serialize.Yml,
|
||||
"text": serialize.JSON,
|
||||
}
|
||||
|
||||
// AddResponseParser 新增一个解析实现
|
||||
func AddResponseParser(t string, parser abstract.IResponseParser) {
|
||||
ResponseParserTable[t] = parser
|
||||
}
|
||||
|
||||
// RemoveResponseParser 移除一个解析实现
|
||||
func RemoveResponseParser(t string) {
|
||||
delete(ResponseParserTable, t)
|
||||
}
|
||||
|
Reference in New Issue
Block a user