From bc8268c3f3e25087d98a36fef0f4af5c4de061eb 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, 13 Aug 2021 19:13:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=83=A8=E5=88=86=E9=87=8D?= =?UTF-8?q?=E8=AF=95&=E5=BC=82=E5=B8=B8=E7=BB=93=E6=9E=9C=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request/curl.go | 48 ++++++++++++++++++++++++++++++++++++----------- request/define.go | 23 ++++++++++++----------- 2 files changed, 49 insertions(+), 22 deletions(-) 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响应数据