- refactor platformapi.AccessPlatformAPIWithRetry

This commit is contained in:
gazebo
2019-01-12 14:04:37 +08:00
parent fc34194835
commit e124038aa2
11 changed files with 20 additions and 56 deletions

View File

@@ -137,11 +137,7 @@ func (a *API) AccessAPI(apiStr string, params map[string]interface{}) (retVal Re
return request
},
a.config,
func(response *http.Response) (errLevel string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
}
func(jsonResult1 map[string]interface{}) (errLevel string, err error) {
status := jsonResult1["status"].(string)
if status == StatusCodeSuccess {
retVal = jsonResult1

View File

@@ -116,11 +116,7 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, err
}
func(jsonResult1 map[string]interface{}) (result string, err error) {
code := int(utils.MustInterface2Int64(jsonResult1["code"]))
retVal = &ResponseResult{
Code: code,

View File

@@ -98,11 +98,7 @@ func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *Respon
return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, err
}
func(jsonResult1 map[string]interface{}) (result string, err error) {
Body := jsonResult1["body"].(map[string]interface{})
retVal = &ResponseResult{
ErrNo: int(utils.MustInterface2Int64(Body["errno"])),

View File

@@ -159,11 +159,7 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, err
}
func(jsonResult1 map[string]interface{}) (result string, err error) {
resultError, _ := jsonResult1["error"].(map[string]interface{})
retVal = &ResponseResult{
ID: jsonResult1["id"].(string),
@@ -220,11 +216,7 @@ func (a *API) AcccessAPI2(baseURL string, params map[string]interface{}, method
return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, err
}
func(jsonResult1 map[string]interface{}) (result string, err error) {
retVal = jsonResult1
return platformapi.ErrLevelSuccess, nil
})

View File

@@ -179,12 +179,7 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal
return request
},
a.config,
func(response *http.Response) (errLevel string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
// baseapi.SugarLogger.Debug(utils.Format4Output(jsonResult1, false))
if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
}
func(jsonResult1 map[string]interface{}) (errLevel string, err error) {
code := jsonResult1["code"].(string)
if code == ResponseCodeSuccess {
retVal = jsonResult1

View File

@@ -46,12 +46,7 @@ func (a *API) AccessStorePage(subURL string) (retVal map[string]interface{}, err
return request
},
a.config,
func(response *http.Response) (errLevel string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
// baseapi.SugarLogger.Debug(utils.Format4Output(jsonResult1, false))
if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
}
func(jsonResult1 map[string]interface{}) (errLevel string, err error) {
retVal = jsonResult1
code := jsonResult1["code"].(string)
if code == ResponseCodeSuccess {

View File

@@ -217,11 +217,7 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
}
func(jsonResult1 map[string]interface{}) (result string, err error) {
code := int(utils.MustInterface2Int64(jsonResult1["code"]))
retVal = &ResponseResult{
Code: code,

View File

@@ -129,12 +129,7 @@ func (a *API) AccessAPI(cmd string, isGet bool, bizParams map[string]interface{}
return request
},
a.config,
func(response *http.Response) (errLevel string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
// baseapi.SugarLogger.Debug(utils.Format4Output(jsonResult1, false))
if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
}
func(jsonResult1 map[string]interface{}) (errLevel string, err error) {
if _, ok := jsonResult1["error"]; ok {
baseapi.SugarLogger.Debugf("mtwm AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
errorInfo := jsonResult1["error"].(map[string]interface{})

View File

@@ -10,6 +10,7 @@ import (
"time"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
"github.com/fatih/structs"
)
@@ -79,7 +80,7 @@ func getClonedData(r *bytes.Buffer) string {
return string(r.Bytes())
}
func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.Request, config *APIConfig, handleResponse func(response *http.Response) (string, error)) error {
func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.Request, config *APIConfig, handleResponse func(bodyMap map[string]interface{}) (string, error)) error {
exceedLimitRetryCount := 0
recoverableErrorRetryCount := 0
for {
@@ -116,7 +117,13 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
}
return ErrHTTPCodeIsNot200
}
errLevel, err := handleResponse(response)
var errLevel string
bodyMap, err := utils.HTTPResponse2Json(response)
if err != nil {
errLevel = ErrLevelRecoverableErr
} else {
errLevel, err = handleResponse(bodyMap)
}
if err == nil {
return nil
} else if errLevel == ErrLevelExceedLimit {

View File

@@ -146,11 +146,7 @@ func (a *API) AccessAPI(action string, params map[string]interface{}, body strin
return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
}
func(jsonResult1 map[string]interface{}) (result string, err error) {
var errInfo *ErrorInfo
// 微信的返回值,在错误与正常情况下,结构是完全不一样的
if errCode, ok := jsonResult1["errcode"]; ok {

View File

@@ -277,7 +277,7 @@ func HTTPResponse2Json(response *http.Response) (map[string]interface{}, error)
var jsonResult map[string]interface{}
bodyData, err := ioutil.ReadAll(response.Body)
if err != nil {
baseapi.SugarLogger.Errorf("ioutil.ReadAll error:%v, response:%v", err, response)
// baseapi.SugarLogger.Errorf("ioutil.ReadAll error:%v, response:%v", err, response)
return nil, err
}