- 修改AccessPlatformAPIWithRetry接口,允许处理返回结果为非JSON请求
This commit is contained in:
@@ -137,7 +137,10 @@ func (a *API) AccessAPI(apiStr string, params map[string]interface{}) (retVal Re
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
status := jsonResult1["status"].(string)
|
||||
if status == StatusCodeSuccess {
|
||||
retVal = jsonResult1
|
||||
|
||||
@@ -118,7 +118,10 @@ func (a *API) AccessAPI(action string, params interface{}) (retVal *ResponseResu
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (result string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
code := int(utils.MustInterface2Int64(jsonResult1["code"]))
|
||||
retVal = &ResponseResult{
|
||||
Code: code,
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -147,7 +148,10 @@ func (a *API) AccessAPI(action string, params map[string]interface{}, bodyMap ma
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (result string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
errCode := int(utils.MustInterface2Int64(jsonResult1["errcode"]))
|
||||
if errCode == ResponseCodeSuccess {
|
||||
retVal = jsonResult1
|
||||
|
||||
@@ -115,7 +115,10 @@ func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *Respon
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (result string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
Body := jsonResult1["body"].(map[string]interface{})
|
||||
retVal = &ResponseResult{
|
||||
ErrNo: int(utils.MustInterface2Int64(Body["errno"])),
|
||||
|
||||
@@ -344,7 +344,10 @@ func (a *API) AccessStorePage2(subURL string, params map[string]interface{}, isP
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
retVal = jsonResult1
|
||||
code := int(utils.MustInterface2Int64(jsonResult1["errno"]))
|
||||
if code == ResponseCodeSuccess {
|
||||
@@ -591,7 +594,10 @@ func (a *API) AccessStorePageNoCookie(subURL string, params map[string]interface
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
code := int(utils.MustInterface2Int64(jsonResult1["error_no"]))
|
||||
if code == ResponseCodeSuccess {
|
||||
retVal = jsonResult1["result"].(map[string]interface{})
|
||||
|
||||
@@ -159,7 +159,10 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (result string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
resultError, _ := jsonResult1["error"].(map[string]interface{})
|
||||
retVal = &ResponseResult{
|
||||
ID: jsonResult1["id"].(string),
|
||||
@@ -216,7 +219,10 @@ func (a *API) AcccessAPI2(baseURL string, params map[string]interface{}, method
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (result string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
retVal = jsonResult1
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
})
|
||||
|
||||
@@ -121,7 +121,10 @@ func (a *API) AccessAPI(apiName string, apiParams map[string]interface{}) (retVa
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
code := int(utils.Interface2Int64WithDefault(jsonResult1["ret"], ResponseCodeSuccess))
|
||||
if code == ResponseCodeSuccess {
|
||||
retVal = jsonResult1["data"]
|
||||
|
||||
@@ -184,7 +184,10 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
code := jsonResult1["code"].(string)
|
||||
if code == ResponseCodeSuccess {
|
||||
retVal = jsonResult1
|
||||
|
||||
@@ -181,7 +181,10 @@ func (a *API) AccessStorePage(fullURL string, params map[string]interface{}, isP
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
retVal = jsonResult1
|
||||
code := jsonResult1["code"].(string)
|
||||
if code == ResponseCodeSuccess {
|
||||
|
||||
@@ -215,7 +215,10 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (result string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
code := int(utils.MustInterface2Int64(jsonResult1["code"]))
|
||||
retVal = &ResponseResult{
|
||||
Code: code,
|
||||
|
||||
@@ -152,7 +152,10 @@ func (a *API) AccessAPI2(cmd string, isGet bool, bizParams map[string]interface{
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
if errObj, ok := jsonResult1["error"]; ok {
|
||||
baseapi.SugarLogger.Debugf("mtwm AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
errorInfo := errObj.(map[string]interface{})
|
||||
|
||||
@@ -77,7 +77,10 @@ func (a *API) AccessUserPage(subURL string, params map[string]interface{}) (retV
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
retVal = jsonResult1
|
||||
code := int(utils.MustInterface2Int64(jsonResult1["code"]))
|
||||
if code == ResponseCodeSuccess {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -162,7 +162,10 @@ func (a *API) AccessAPI(apiStr string, apiParams map[string]interface{}) (retVal
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
// baseapi.SugarLogger.Debug(utils.Format4Output(jsonResult1, false))
|
||||
code := ResponseCodeSuccess
|
||||
errMsg := ""
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package weixinapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -100,7 +101,10 @@ func (a *API) AccessAPI(action string, params map[string]interface{}, body strin
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (result string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
var errInfo *ErrorInfo
|
||||
// 微信的返回值,在错误与正常情况下,结构是完全不一样的
|
||||
if errCode, ok := jsonResult1["errcode"]; ok {
|
||||
|
||||
@@ -2,6 +2,7 @@ package xiaowmapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
@@ -80,7 +81,10 @@ func (a *API) AccessAPI(apiName string, apiParams map[string]interface{}) (retVa
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
code := int(utils.Interface2Int64WithDefault(jsonResult1["errno"], ResponseCodeSuccess))
|
||||
if code == ResponseCodeSuccess {
|
||||
retVal = jsonResult1
|
||||
|
||||
@@ -100,7 +100,10 @@ func (a *API) AccessAPI(apiName string, apiParams map[string]interface{}, token
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
code := utils.Interface2String(jsonResult1["error"])
|
||||
if code == ResponseCodeSuccess {
|
||||
retVal, _ = jsonResult1["body"].(map[string]interface{})
|
||||
|
||||
@@ -88,7 +88,10 @@ func (a *API) AccessAPI(apiName string, apiParams map[string]interface{}, isGet
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
code := int(utils.MustInterface2Int64(jsonResult1["errNum"]))
|
||||
if code == ResponseCodeSuccess {
|
||||
retVal, _ = jsonResult1["retData"].(map[string]interface{})
|
||||
|
||||
Reference in New Issue
Block a user