优化返回值字段提取 + 支持不验证业务状态码字段
This commit is contained in:
parent
049f9cca4f
commit
0057825613
11
define.go
11
define.go
@ -68,6 +68,17 @@ type ApiRetry struct {
|
|||||||
Frequency int `json:"frequency" yaml:"frequency"` // 重试频率, 单位 : ms
|
Frequency int `json:"frequency" yaml:"frequency"` // 重试频率, 单位 : ms
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// responseDaFieldConfig 响应数据的字段配置
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 17:07 2022/7/1
|
||||||
|
type responseDaFieldConfig struct {
|
||||||
|
Code string
|
||||||
|
Message string
|
||||||
|
Data string
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// DefaultConnectTimeout 默认连接超时
|
// DefaultConnectTimeout 默认连接超时
|
||||||
DefaultConnectTimeout = 100
|
DefaultConnectTimeout = 100
|
||||||
|
82
rpc.go
82
rpc.go
@ -180,7 +180,6 @@ func (r *request) GetServiceApi(serviceFlag string, apiFlag string) (*Service, *
|
|||||||
if apiInfo, exist = serviceInfo.ApiTable[apiFlag]; !exist {
|
if apiInfo, exist = serviceInfo.ApiTable[apiFlag]; !exist {
|
||||||
return nil, nil, errors.New(serviceFlag + " : " + apiFlag + " -> api")
|
return nil, nil, errors.New(serviceFlag + " : " + apiFlag + " -> api")
|
||||||
}
|
}
|
||||||
|
|
||||||
return serviceInfo, apiInfo, nil
|
return serviceInfo, apiInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,12 +248,14 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !r.httpCodeIsSuccess(response.StatusCode, apiConfig.SuccessHttpCodeList) {
|
responseFieldCfg := r.getCodeAndMessageAndDataField(serviceConfig, apiConfig)
|
||||||
|
successHttpCodeList, successBusinessCodeList := r.getSuccessHttpCodeAndSuccessBusinessCode(serviceConfig, apiConfig)
|
||||||
|
if !r.httpCodeIsSuccess(response.StatusCode, successHttpCodeList) {
|
||||||
return fmt.Errorf("HTTP状态码异常 : %v -> %v", response.StatusCode, response.Status)
|
return fmt.Errorf("HTTP状态码异常 : %v -> %v", response.StatusCode, response.Status)
|
||||||
}
|
}
|
||||||
// 解析响应的业务数据
|
// 解析响应的业务数据
|
||||||
code, message, data = r.getCodeAndMessageAndData(apiConfig, responseBody)
|
code, message, data = r.getCodeAndMessageAndData(responseBody, responseFieldCfg)
|
||||||
if !r.codeIsSuccess(code, apiConfig.SuccessCodeList) {
|
if !r.codeIsSuccess(code, successBusinessCodeList) {
|
||||||
return fmt.Errorf("业务状态码异常 : %v -> %v", code, message)
|
return fmt.Errorf("业务状态码异常 : %v -> %v", code, message)
|
||||||
}
|
}
|
||||||
return parseResponseBody(response.Header.Get("Content-type"), []byte(data), receiver)
|
return parseResponseBody(response.Header.Get("Content-type"), []byte(data), receiver)
|
||||||
@ -348,22 +349,22 @@ func (r *request) validateResponseHttpCode(apiConfig *Api, response *httpclient.
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 18:20 2022/6/30
|
// Date : 18:20 2022/6/30
|
||||||
func (r *request) getCodeAndMessageAndData(apiConfig *Api, responseBody []byte) (string, string, string) {
|
func (r *request) getCodeAndMessageAndData(responseBody []byte, responseFieldCfg responseDaFieldConfig) (string, string, string) {
|
||||||
var (
|
var (
|
||||||
code = SpecialFiledVal
|
code = SpecialFiledVal
|
||||||
message = SpecialFiledVal
|
message = SpecialFiledVal
|
||||||
data string
|
data string
|
||||||
)
|
)
|
||||||
if apiConfig.CodeField != SpecialFiledVal {
|
if responseFieldCfg.Code != SpecialFiledVal {
|
||||||
code = gjson.Get(string(responseBody), apiConfig.CodeField).String()
|
code = gjson.Get(string(responseBody), responseFieldCfg.Code).String()
|
||||||
}
|
}
|
||||||
if apiConfig.MessageField != SpecialFiledVal {
|
if responseFieldCfg.Message != SpecialFiledVal {
|
||||||
message = gjson.Get(string(responseBody), apiConfig.MessageField).String()
|
message = gjson.Get(string(responseBody), responseFieldCfg.Message).String()
|
||||||
}
|
}
|
||||||
if apiConfig.DataField == SpecialFiledVal {
|
if responseFieldCfg.Data == SpecialFiledVal {
|
||||||
data = string(responseBody)
|
data = string(responseBody)
|
||||||
} else {
|
} else {
|
||||||
data = gjson.Get(string(responseBody), apiConfig.DataField).String()
|
data = gjson.Get(string(responseBody), responseFieldCfg.Data).String()
|
||||||
}
|
}
|
||||||
return code, message, data
|
return code, message, data
|
||||||
}
|
}
|
||||||
@ -374,6 +375,10 @@ func (r *request) getCodeAndMessageAndData(apiConfig *Api, responseBody []byte)
|
|||||||
//
|
//
|
||||||
// Date : 18:27 2022/6/30
|
// Date : 18:27 2022/6/30
|
||||||
func (r *request) codeIsSuccess(input string, successCodeList []string) bool {
|
func (r *request) codeIsSuccess(input string, successCodeList []string) bool {
|
||||||
|
if input == SpecialFiledVal {
|
||||||
|
// 不需要验证状态码,直接返回成功
|
||||||
|
return true
|
||||||
|
}
|
||||||
for _, item := range successCodeList {
|
for _, item := range successCodeList {
|
||||||
if item == input {
|
if item == input {
|
||||||
return true
|
return true
|
||||||
@ -395,3 +400,58 @@ func (r *request) httpCodeIsSuccess(input int, successCodeList []int) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getSuccessHttpCodeAndSuccessBusinessCode 获取成功的http 和 业务状态码列表
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 16:48 2022/7/1
|
||||||
|
func (r *request) getSuccessHttpCodeAndSuccessBusinessCode(serviceInfo *Service, apiInfo *Api) ([]int, []string) {
|
||||||
|
var (
|
||||||
|
successHttpCodeList = apiInfo.SuccessHttpCodeList
|
||||||
|
successBusinessCodeList = apiInfo.SuccessCodeList
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(apiInfo.SuccessHttpCodeList) == 0 {
|
||||||
|
successHttpCodeList = serviceInfo.SuccessHttpCodeList
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(apiInfo.SuccessHttpCodeList) == 0 {
|
||||||
|
successBusinessCodeList = serviceInfo.SuccessCodeList
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(successHttpCodeList) == 0 {
|
||||||
|
successHttpCodeList = []int{http.StatusOK}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(successBusinessCodeList) == 0 {
|
||||||
|
successBusinessCodeList = []string{"0"}
|
||||||
|
}
|
||||||
|
return successHttpCodeList, successBusinessCodeList
|
||||||
|
}
|
||||||
|
|
||||||
|
// getCodeAndMessageAndDataField 获取三个字段信息
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 16:54 2022/7/1
|
||||||
|
func (r *request) getCodeAndMessageAndDataField(serviceInfo *Service, apiInfo *Api) responseDaFieldConfig {
|
||||||
|
cfg := responseDaFieldConfig{
|
||||||
|
Code: apiInfo.CodeField,
|
||||||
|
Message: apiInfo.MessageField,
|
||||||
|
Data: apiInfo.DataField,
|
||||||
|
}
|
||||||
|
if len(cfg.Code) == 0 {
|
||||||
|
cfg.Code = serviceInfo.CodeField
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cfg.Message) == 0 {
|
||||||
|
cfg.Message = serviceInfo.MessageField
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cfg.Data) == 0 {
|
||||||
|
cfg.Data = serviceInfo.DataField
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user