From e124038aa2ddb52c5ceaa960c92ad212590b6ca9 Mon Sep 17 00:00:00 2001 From: gazebo Date: Sat, 12 Jan 2019 14:04:37 +0800 Subject: [PATCH] - refactor platformapi.AccessPlatformAPIWithRetry --- platformapi/autonavi/autonavi.go | 6 +----- platformapi/dadaapi/dadaapi.go | 6 +----- platformapi/ebaiapi/ebaiapi.go | 6 +----- platformapi/elmapi/elmapi.go | 12 ++---------- platformapi/jdapi/jdapi.go | 7 +------ platformapi/jdapi/store_page.go | 7 +------ platformapi/mtpsapi/mtpsapi.go | 6 +----- platformapi/mtwmapi/mtwmapi.go | 7 +------ platformapi/platformapi.go | 11 +++++++++-- platformapi/weixinapi/weixinapi.go | 6 +----- utils/typeconv.go | 2 +- 11 files changed, 20 insertions(+), 56 deletions(-) diff --git a/platformapi/autonavi/autonavi.go b/platformapi/autonavi/autonavi.go index 3b42ff75..9a27860b 100644 --- a/platformapi/autonavi/autonavi.go +++ b/platformapi/autonavi/autonavi.go @@ -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 diff --git a/platformapi/dadaapi/dadaapi.go b/platformapi/dadaapi/dadaapi.go index 790c8c8e..13bda249 100644 --- a/platformapi/dadaapi/dadaapi.go +++ b/platformapi/dadaapi/dadaapi.go @@ -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, diff --git a/platformapi/ebaiapi/ebaiapi.go b/platformapi/ebaiapi/ebaiapi.go index c6fb4aea..66d1845c 100644 --- a/platformapi/ebaiapi/ebaiapi.go +++ b/platformapi/ebaiapi/ebaiapi.go @@ -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"])), diff --git a/platformapi/elmapi/elmapi.go b/platformapi/elmapi/elmapi.go index 903156ce..dfbf8c16 100644 --- a/platformapi/elmapi/elmapi.go +++ b/platformapi/elmapi/elmapi.go @@ -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 }) diff --git a/platformapi/jdapi/jdapi.go b/platformapi/jdapi/jdapi.go index 4007a6d2..604294f4 100644 --- a/platformapi/jdapi/jdapi.go +++ b/platformapi/jdapi/jdapi.go @@ -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 diff --git a/platformapi/jdapi/store_page.go b/platformapi/jdapi/store_page.go index 5b123102..bb110675 100644 --- a/platformapi/jdapi/store_page.go +++ b/platformapi/jdapi/store_page.go @@ -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 { diff --git a/platformapi/mtpsapi/mtpsapi.go b/platformapi/mtpsapi/mtpsapi.go index 97ee294b..3d584eb5 100644 --- a/platformapi/mtpsapi/mtpsapi.go +++ b/platformapi/mtpsapi/mtpsapi.go @@ -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, diff --git a/platformapi/mtwmapi/mtwmapi.go b/platformapi/mtwmapi/mtwmapi.go index 89866359..2cd99388 100644 --- a/platformapi/mtwmapi/mtwmapi.go +++ b/platformapi/mtwmapi/mtwmapi.go @@ -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{}) diff --git a/platformapi/platformapi.go b/platformapi/platformapi.go index a519ce81..6f827a63 100644 --- a/platformapi/platformapi.go +++ b/platformapi/platformapi.go @@ -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 { diff --git a/platformapi/weixinapi/weixinapi.go b/platformapi/weixinapi/weixinapi.go index 299a6c2e..a26c13f0 100644 --- a/platformapi/weixinapi/weixinapi.go +++ b/platformapi/weixinapi/weixinapi.go @@ -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 { diff --git a/utils/typeconv.go b/utils/typeconv.go index 4a65bb79..d4a2c99d 100644 --- a/utils/typeconv.go +++ b/utils/typeconv.go @@ -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 }