diff --git a/request/curl.go b/request/curl.go index bbb913b..650ab23 100644 --- a/request/curl.go +++ b/request/curl.go @@ -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() - return apiResponse, false + 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<白茶清欢> diff --git a/request/define.go b/request/define.go index 4af5c94..54dc013 100644 --- a/request/define.go +++ b/request/define.go @@ -27,17 +27,18 @@ type APIMethod struct { // // Date : 8:32 下午 2021/8/1 type ResponseConfig struct { - RetryRule int `json:"retry_rule"` // 重试规则 0 - http error 1 - business error 2 - both 0 && 1 - RetryCnt int `json:"retry_cnt"` // 重试次数(重试次数 1, 最多请求两次) - RetryInterval int `json:"retry_interval"` // 重试的时间间隔, 单位ms, 为 0 立即重试, > 0 休眠指定ms后重试 - ISJson bool `json:"is_json"` // 响应数据是否为json - CodeKey string `json:"code_key"` // 代表业务Code的Key - MessageKey string `json:"message_key"` // 描述业务code的key - TraceKey string `json:"trace_key"` // 接口返回的trace追踪字段key - DataKey string `json:"data_key"` // 返回数据的key - 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"` // 哪些状态码被认为是请求成功 + RetryRule int `json:"retry_rule"` // 重试规则 0 - http error 1 - business error 2 - both 0 && 1 + RetryCnt int `json:"retry_cnt"` // 重试次数(重试次数 1, 最多请求两次) + RetryInterval int `json:"retry_interval"` // 重试的时间间隔, 单位ms, 为 0 立即重试, > 0 休眠指定ms后重试 + ISJson bool `json:"is_json"` // 响应数据是否为json + CodeKey string `json:"code_key"` // 代表业务Code的Key + MessageKey string `json:"message_key"` // 描述业务code的key + TraceKey string `json:"trace_key"` // 接口返回的trace追踪字段key + DataKey string `json:"data_key"` // 返回数据的key + 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响应数据