完成部分重试&异常结果类型判断逻辑
This commit is contained in:
parent
187b476fa6
commit
bc8268c3f3
@ -25,6 +25,12 @@ import (
|
||||
//
|
||||
// Date : 9:38 下午 2021/8/11
|
||||
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
|
||||
}
|
||||
|
||||
@ -33,7 +39,7 @@ func Send(apiMethod *APIMethod) *APIResponse {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// 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{
|
||||
Code: "",
|
||||
Message: "",
|
||||
@ -52,26 +58,23 @@ func request(method string, fullURL string, parameter map[string]interface{}, he
|
||||
apiResponse.FinishRequestTime = time.Now().Unix()
|
||||
apiResponse.RealCost = apiResponse.FinishRequestTime - apiResponse.StartRequestTime
|
||||
}(apiResponse)
|
||||
if nil == parameter {
|
||||
parameter = make(map[string]interface{})
|
||||
}
|
||||
if nil == header {
|
||||
header = make(map[string]string)
|
||||
}
|
||||
var (
|
||||
client *httpclient.HttpClient
|
||||
response *httpclient.Response
|
||||
err error
|
||||
responseData []byte
|
||||
)
|
||||
client = httpclient.NewHttpClient().WithHeaders(header)
|
||||
inputByteData, _ := json.Marshal(parameter)
|
||||
client = httpclient.NewHttpClient()
|
||||
inputByteData, _ := json.Marshal(apiMethod.Parameter)
|
||||
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)
|
||||
return apiResponse, false
|
||||
}
|
||||
if responseData, err = ioutil.ReadAll(response.Body); nil != err {
|
||||
apiResponse.Success = false
|
||||
apiResponse.ErrorList = append(apiResponse.ErrorList, err)
|
||||
return apiResponse, false
|
||||
}
|
||||
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.Message = gjson.Get(orgData, responseConfig.MessageKey).String()
|
||||
apiResponse.Cost = gjson.Get(orgData, responseConfig.CostKey).Int()
|
||||
apiResponse.Success = isSuccessCode(apiResponse.Code, responseConfig.SuccessCode)
|
||||
|
||||
if apiResponse.Success {
|
||||
return apiResponse, false
|
||||
}
|
||||
|
||||
// 非成功, 判断是否需要重试
|
||||
return apiResponse, isNeedRetry(apiMethod, ResponseErrorTypeBusinessCode)
|
||||
}
|
||||
|
||||
// isNeedRetry 是否需要重试
|
||||
@ -91,6 +101,22 @@ func isNeedRetry(apiMethod *APIMethod, errType string) bool {
|
||||
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 构建响应结果
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
|
@ -38,6 +38,7 @@ type ResponseConfig struct {
|
||||
CostKey string `json:"cost_key"` // 代表接口耗时的key
|
||||
SuccessRule string `json:"success_rule"` // 请求成功的规则, http_code / business_code http状态码或者业务状态码, 如果是通过http code 判断是否为请求成功 code key / message key 配置无效
|
||||
SuccessCode []string `json:"success_code"` // 哪些状态码被认为是请求成功
|
||||
SuccessHttpCode []string `json:"success_http_code"` // 被认为是成功的http code
|
||||
}
|
||||
|
||||
// APIResponse API响应数据
|
||||
|
Loading…
Reference in New Issue
Block a user