From 00578256138cb35d69ea7f2048cb8fac3c80152f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 1 Jul 2022 17:19:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=BF=94=E5=9B=9E=E5=80=BC?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E6=8F=90=E5=8F=96=20+=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=B8=8D=E9=AA=8C=E8=AF=81=E4=B8=9A=E5=8A=A1=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E7=A0=81=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 11 ++++++++ rpc.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 82 insertions(+), 11 deletions(-) diff --git a/define.go b/define.go index f881c5b..680dca3 100644 --- a/define.go +++ b/define.go @@ -68,6 +68,17 @@ type ApiRetry struct { 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 ( // DefaultConnectTimeout 默认连接超时 DefaultConnectTimeout = 100 diff --git a/rpc.go b/rpc.go index 8ff7ba1..a53d5a7 100644 --- a/rpc.go +++ b/rpc.go @@ -180,7 +180,6 @@ func (r *request) GetServiceApi(serviceFlag string, apiFlag string) (*Service, * if apiInfo, exist = serviceInfo.ApiTable[apiFlag]; !exist { return nil, nil, errors.New(serviceFlag + " : " + apiFlag + " -> api") } - return serviceInfo, apiInfo, nil } @@ -249,12 +248,14 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par 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) } // 解析响应的业务数据 - code, message, data = r.getCodeAndMessageAndData(apiConfig, responseBody) - if !r.codeIsSuccess(code, apiConfig.SuccessCodeList) { + code, message, data = r.getCodeAndMessageAndData(responseBody, responseFieldCfg) + if !r.codeIsSuccess(code, successBusinessCodeList) { return fmt.Errorf("业务状态码异常 : %v -> %v", code, message) } 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<白茶清欢> // // 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 ( code = SpecialFiledVal message = SpecialFiledVal data string ) - if apiConfig.CodeField != SpecialFiledVal { - code = gjson.Get(string(responseBody), apiConfig.CodeField).String() + if responseFieldCfg.Code != SpecialFiledVal { + code = gjson.Get(string(responseBody), responseFieldCfg.Code).String() } - if apiConfig.MessageField != SpecialFiledVal { - message = gjson.Get(string(responseBody), apiConfig.MessageField).String() + if responseFieldCfg.Message != SpecialFiledVal { + message = gjson.Get(string(responseBody), responseFieldCfg.Message).String() } - if apiConfig.DataField == SpecialFiledVal { + if responseFieldCfg.Data == SpecialFiledVal { data = string(responseBody) } else { - data = gjson.Get(string(responseBody), apiConfig.DataField).String() + data = gjson.Get(string(responseBody), responseFieldCfg.Data).String() } return code, message, data } @@ -374,6 +375,10 @@ func (r *request) getCodeAndMessageAndData(apiConfig *Api, responseBody []byte) // // Date : 18:27 2022/6/30 func (r *request) codeIsSuccess(input string, successCodeList []string) bool { + if input == SpecialFiledVal { + // 不需要验证状态码,直接返回成功 + return true + } for _, item := range successCodeList { if item == input { return true @@ -395,3 +400,58 @@ func (r *request) httpCodeIsSuccess(input int, successCodeList []int) bool { } 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 +}