- 修改AccessPlatformAPIWithRetry接口,允许处理返回结果为非JSON请求

This commit is contained in:
gazebo
2019-07-11 15:47:37 +08:00
parent 91a789d1d1
commit 989cd7e618
18 changed files with 98 additions and 31 deletions

View File

@@ -88,7 +88,7 @@ func getClonedData(requestURL *url.URL, r *bytes.Buffer) string {
return retVal
}
func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.Request, config *APIConfig, handleResponse func(response *http.Response, bodyMap map[string]interface{}) (string, error)) error {
func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.Request, config *APIConfig, handleResponse func(response *http.Response, bodyStr string, bodyMap map[string]interface{}) (string, error)) error {
exceedLimitRetryCount := 0
recoverableErrorRetryCount := 0
for {
@@ -134,28 +134,35 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
return ErrHTTPCodeIsNot200
}
var (
errLevel string
bodyMap map[string]interface{}
errLevel string
bodyMap map[string]interface{}
parseJSONErr error
)
bodyData, err := ioutil.ReadAll(response.Body)
if err != nil {
baseapi.SugarLogger.Errorf("AccessPlatformAPIWithRetry:%s ioutil.ReadAll failed, url:%v, request:%v, error:%v", trackID, request.URL, getClonedData(request.URL, savedBuf), err)
} else if err = utils.TryUnmarshalUseNumber(bodyData, &bodyMap); err != nil {
const maxOutputLen = 200
bodyDataLen := len(bodyData)
bodyData2 := bodyData
if bodyDataLen > maxOutputLen {
bodyData2 = bodyData2[:maxOutputLen]
}
baseapi.SugarLogger.Infof("AccessPlatformAPIWithRetry:%s TryUnmarshalUseNumber failed, url:%v, request:%v, error:%v, bodyData:%s", trackID, request.URL, getClonedData(request.URL, savedBuf), err, string(bodyData2))
parseJSONErr = err
err = nil // 尝试忽略解析成json错
} else {
baseapi.SugarLogger.Debugf("AccessPlatformAPIWithRetry:%s url:%v, response:%s", trackID, request.URL, utils.Format4Output(bodyMap, true))
}
errLevel, err = handleResponse(response, string(bodyData), bodyMap)
baseapi.SugarLogger.Debugf("AccessPlatformAPIWithRetry:%s url:%v, response:%s", trackID, request.URL, utils.Format4Output(bodyMap, true))
if err != nil {
errLevel = ErrLevelRecoverableErr // 读取数据错误,或数据格式错误认为是偶发情况,重试
} else {
errLevel, err = handleResponse(response, bodyMap)
if errLevel, err = handleResponse(response, string(bodyData), bodyMap); err != nil && parseJSONErr != nil {
const maxOutputLen = 200
bodyDataLen := len(bodyData)
bodyData2 := bodyData
if bodyDataLen > maxOutputLen {
bodyData2 = bodyData2[:maxOutputLen]
}
baseapi.SugarLogger.Infof("AccessPlatformAPIWithRetry:%s TryUnmarshalUseNumber failed, url:%v, request:%v, error:%v, bodyData:%s", trackID, request.URL, getClonedData(request.URL, savedBuf), parseJSONErr, string(bodyData2))
}
}
if err == nil {
return nil
} else if errLevel == ErrLevelExceedLimit {