完成部分重试&异常结果类型判断逻辑

This commit is contained in:
白茶清欢 2021-08-13 19:13:01 +08:00
parent 187b476fa6
commit bc8268c3f3
2 changed files with 49 additions and 22 deletions

View File

@ -25,6 +25,12 @@ import (
// //
// Date : 9:38 下午 2021/8/11 // Date : 9:38 下午 2021/8/11
func Send(apiMethod *APIMethod) *APIResponse { func Send(apiMethod *APIMethod) *APIResponse {
if apiMethod.Parameter == nil {
apiMethod.Parameter = make(map[string]interface{})
}
if apiMethod.Header == nil {
apiMethod.Header = make(map[string]string)
}
return nil return nil
} }
@ -33,7 +39,7 @@ func Send(apiMethod *APIMethod) *APIResponse {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 2:27 下午 2021/8/12 // Date : 2:27 下午 2021/8/12
func request(method string, fullURL string, parameter map[string]interface{}, header map[string]string, responseConfig *ResponseConfig) (*APIResponse, bool) { func request(apiMethod *APIMethod, responseConfig *ResponseConfig) (*APIResponse, bool) {
apiResponse := &APIResponse{ apiResponse := &APIResponse{
Code: "", Code: "",
Message: "", Message: "",
@ -52,26 +58,23 @@ func request(method string, fullURL string, parameter map[string]interface{}, he
apiResponse.FinishRequestTime = time.Now().Unix() apiResponse.FinishRequestTime = time.Now().Unix()
apiResponse.RealCost = apiResponse.FinishRequestTime - apiResponse.StartRequestTime apiResponse.RealCost = apiResponse.FinishRequestTime - apiResponse.StartRequestTime
}(apiResponse) }(apiResponse)
if nil == parameter {
parameter = make(map[string]interface{})
}
if nil == header {
header = make(map[string]string)
}
var ( var (
client *httpclient.HttpClient client *httpclient.HttpClient
response *httpclient.Response response *httpclient.Response
err error err error
responseData []byte responseData []byte
) )
client = httpclient.NewHttpClient().WithHeaders(header) client = httpclient.NewHttpClient()
inputByteData, _ := json.Marshal(parameter) inputByteData, _ := json.Marshal(apiMethod.Parameter)
requestData := bytes.NewReader(inputByteData) requestData := bytes.NewReader(inputByteData)
if response, err = client.Do(method, fullURL, header, requestData); nil != err { if response, err = client.Do(apiMethod.Method, apiMethod.ServiceDomain+apiMethod.URI, apiMethod.Header, requestData); nil != err {
apiResponse.Success = false
apiResponse.ErrorList = append(apiResponse.ErrorList, err) apiResponse.ErrorList = append(apiResponse.ErrorList, err)
return apiResponse, false return apiResponse, false
} }
if responseData, err = ioutil.ReadAll(response.Body); nil != err { if responseData, err = ioutil.ReadAll(response.Body); nil != err {
apiResponse.Success = false
apiResponse.ErrorList = append(apiResponse.ErrorList, err)
return apiResponse, false return apiResponse, false
} }
orgData := string(responseData) orgData := string(responseData)
@ -79,7 +82,14 @@ func request(method string, fullURL string, parameter map[string]interface{}, he
apiResponse.Code = fmt.Sprintf("%v", gjson.Get(orgData, responseConfig.CodeKey).Value()) apiResponse.Code = fmt.Sprintf("%v", gjson.Get(orgData, responseConfig.CodeKey).Value())
apiResponse.Message = gjson.Get(orgData, responseConfig.MessageKey).String() apiResponse.Message = gjson.Get(orgData, responseConfig.MessageKey).String()
apiResponse.Cost = gjson.Get(orgData, responseConfig.CostKey).Int() apiResponse.Cost = gjson.Get(orgData, responseConfig.CostKey).Int()
apiResponse.Success = isSuccessCode(apiResponse.Code, responseConfig.SuccessCode)
if apiResponse.Success {
return apiResponse, false return apiResponse, false
}
// 非成功, 判断是否需要重试
return apiResponse, isNeedRetry(apiMethod, ResponseErrorTypeBusinessCode)
} }
// isNeedRetry 是否需要重试 // isNeedRetry 是否需要重试
@ -91,6 +101,22 @@ func isNeedRetry(apiMethod *APIMethod, errType string) bool {
return false return false
} }
// isSuccessCode 是否为业务处理成功状态码
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 3:43 下午 2021/8/13
func isSuccessCode(responseCode string, successResponseCodeList []string) bool {
isSuccess := false
for _, itemCode := range successResponseCodeList {
if responseCode == itemCode {
isSuccess = true
break
}
}
return isSuccess
}
// buildResponseData 构建响应结果 // buildResponseData 构建响应结果
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>

View File

@ -38,6 +38,7 @@ type ResponseConfig struct {
CostKey string `json:"cost_key"` // 代表接口耗时的key CostKey string `json:"cost_key"` // 代表接口耗时的key
SuccessRule string `json:"success_rule"` // 请求成功的规则, http_code / business_code http状态码或者业务状态码, 如果是通过http code 判断是否为请求成功 code key / message key 配置无效 SuccessRule string `json:"success_rule"` // 请求成功的规则, http_code / business_code http状态码或者业务状态码, 如果是通过http code 判断是否为请求成功 code key / message key 配置无效
SuccessCode []string `json:"success_code"` // 哪些状态码被认为是请求成功 SuccessCode []string `json:"success_code"` // 哪些状态码被认为是请求成功
SuccessHttpCode []string `json:"success_http_code"` // 被认为是成功的http code
} }
// APIResponse API响应数据 // APIResponse API响应数据