- 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 return request
}, },
a.config, a.config,
func(response *http.Response) (errLevel string, err error) { func(jsonResult1 map[string]interface{}) (errLevel string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
}
status := jsonResult1["status"].(string) status := jsonResult1["status"].(string)
if status == StatusCodeSuccess { if status == StatusCodeSuccess {
retVal = jsonResult1 retVal = jsonResult1

View File

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

View File

@@ -98,11 +98,7 @@ func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *Respon
return request return request
}, },
a.config, a.config,
func(response *http.Response) (result string, err error) { func(jsonResult1 map[string]interface{}) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, err
}
Body := jsonResult1["body"].(map[string]interface{}) Body := jsonResult1["body"].(map[string]interface{})
retVal = &ResponseResult{ retVal = &ResponseResult{
ErrNo: int(utils.MustInterface2Int64(Body["errno"])), 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 return request
}, },
a.config, a.config,
func(response *http.Response) (result string, err error) { func(jsonResult1 map[string]interface{}) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, err
}
resultError, _ := jsonResult1["error"].(map[string]interface{}) resultError, _ := jsonResult1["error"].(map[string]interface{})
retVal = &ResponseResult{ retVal = &ResponseResult{
ID: jsonResult1["id"].(string), ID: jsonResult1["id"].(string),
@@ -220,11 +216,7 @@ func (a *API) AcccessAPI2(baseURL string, params map[string]interface{}, method
return request return request
}, },
a.config, a.config,
func(response *http.Response) (result string, err error) { func(jsonResult1 map[string]interface{}) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, err
}
retVal = jsonResult1 retVal = jsonResult1
return platformapi.ErrLevelSuccess, nil return platformapi.ErrLevelSuccess, nil
}) })

View File

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

View File

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

View File

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

View File

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

View File

@@ -10,6 +10,7 @@ import (
"time" "time"
"git.rosy.net.cn/baseapi" "git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
"github.com/fatih/structs" "github.com/fatih/structs"
) )
@@ -79,7 +80,7 @@ func getClonedData(r *bytes.Buffer) string {
return string(r.Bytes()) 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 exceedLimitRetryCount := 0
recoverableErrorRetryCount := 0 recoverableErrorRetryCount := 0
for { for {
@@ -116,7 +117,13 @@ func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.
} }
return ErrHTTPCodeIsNot200 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 { if err == nil {
return nil return nil
} else if errLevel == ErrLevelExceedLimit { } else if errLevel == ErrLevelExceedLimit {

View File

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