From 989cd7e618860e9b32069a23e946434f290d3579 Mon Sep 17 00:00:00 2001 From: gazebo Date: Thu, 11 Jul 2019 15:47:37 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BF=AE=E6=94=B9AccessPlatformAPIWithRetr?= =?UTF-8?q?y=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=85=81=E8=AE=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=E8=BF=94=E5=9B=9E=E7=BB=93=E6=9E=9C=E4=B8=BA=E9=9D=9E?= =?UTF-8?q?JSON=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platformapi/autonavi/autonavi.go | 5 +++- platformapi/dadaapi/dadaapi.go | 5 +++- platformapi/dingdingapi/dingdingapi.go | 6 ++++- platformapi/ebaiapi/ebaiapi.go | 5 +++- platformapi/ebaiapi/store_page.go | 10 ++++++-- platformapi/elmapi/elmapi.go | 10 ++++++-- platformapi/feieapi/feieapi.go | 5 +++- platformapi/jdapi/jdapi.go | 5 +++- platformapi/jdapi/store_page.go | 5 +++- platformapi/mtpsapi/mtpsapi.go | 5 +++- platformapi/mtwmapi/mtwmapi.go | 5 +++- platformapi/mtwmapi/user_page.go | 5 +++- platformapi/platformapi.go | 31 +++++++++++++++--------- platformapi/weimobapi/weimobapi.go | 5 +++- platformapi/weixinapi/weixinapi.go | 6 ++++- platformapi/xiaowmapi/xiaowmapi.go | 6 ++++- platformapi/yilianyunapi/yilianyunapi.go | 5 +++- platformapi/zhongwuapi/zhongwuapi.go | 5 +++- 18 files changed, 98 insertions(+), 31 deletions(-) diff --git a/platformapi/autonavi/autonavi.go b/platformapi/autonavi/autonavi.go index 62e73d1d..a81ddaaa 100644 --- a/platformapi/autonavi/autonavi.go +++ b/platformapi/autonavi/autonavi.go @@ -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 diff --git a/platformapi/dadaapi/dadaapi.go b/platformapi/dadaapi/dadaapi.go index 28db1c6a..f50a77b9 100644 --- a/platformapi/dadaapi/dadaapi.go +++ b/platformapi/dadaapi/dadaapi.go @@ -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, diff --git a/platformapi/dingdingapi/dingdingapi.go b/platformapi/dingdingapi/dingdingapi.go index c20baf19..a99c62aa 100644 --- a/platformapi/dingdingapi/dingdingapi.go +++ b/platformapi/dingdingapi/dingdingapi.go @@ -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 diff --git a/platformapi/ebaiapi/ebaiapi.go b/platformapi/ebaiapi/ebaiapi.go index 139aa5fd..1f7d1b6e 100644 --- a/platformapi/ebaiapi/ebaiapi.go +++ b/platformapi/ebaiapi/ebaiapi.go @@ -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"])), diff --git a/platformapi/ebaiapi/store_page.go b/platformapi/ebaiapi/store_page.go index 788a1775..59ade957 100644 --- a/platformapi/ebaiapi/store_page.go +++ b/platformapi/ebaiapi/store_page.go @@ -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{}) diff --git a/platformapi/elmapi/elmapi.go b/platformapi/elmapi/elmapi.go index 08866a8e..d7a2a4e1 100644 --- a/platformapi/elmapi/elmapi.go +++ b/platformapi/elmapi/elmapi.go @@ -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 }) diff --git a/platformapi/feieapi/feieapi.go b/platformapi/feieapi/feieapi.go index 394fb759..4c1e2d99 100644 --- a/platformapi/feieapi/feieapi.go +++ b/platformapi/feieapi/feieapi.go @@ -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"] diff --git a/platformapi/jdapi/jdapi.go b/platformapi/jdapi/jdapi.go index 103c2862..1ed4c26d 100644 --- a/platformapi/jdapi/jdapi.go +++ b/platformapi/jdapi/jdapi.go @@ -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 diff --git a/platformapi/jdapi/store_page.go b/platformapi/jdapi/store_page.go index f0ff257a..3ac2ac50 100644 --- a/platformapi/jdapi/store_page.go +++ b/platformapi/jdapi/store_page.go @@ -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 { diff --git a/platformapi/mtpsapi/mtpsapi.go b/platformapi/mtpsapi/mtpsapi.go index 1b0f7389..3266602b 100644 --- a/platformapi/mtpsapi/mtpsapi.go +++ b/platformapi/mtpsapi/mtpsapi.go @@ -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, diff --git a/platformapi/mtwmapi/mtwmapi.go b/platformapi/mtwmapi/mtwmapi.go index fcb252ff..db6ddb72 100644 --- a/platformapi/mtwmapi/mtwmapi.go +++ b/platformapi/mtwmapi/mtwmapi.go @@ -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{}) diff --git a/platformapi/mtwmapi/user_page.go b/platformapi/mtwmapi/user_page.go index 43c687fe..cd6264fd 100644 --- a/platformapi/mtwmapi/user_page.go +++ b/platformapi/mtwmapi/user_page.go @@ -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 { diff --git a/platformapi/platformapi.go b/platformapi/platformapi.go index 2d86cfe2..eed36404 100644 --- a/platformapi/platformapi.go +++ b/platformapi/platformapi.go @@ -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 { diff --git a/platformapi/weimobapi/weimobapi.go b/platformapi/weimobapi/weimobapi.go index ae4ae685..db293bf4 100644 --- a/platformapi/weimobapi/weimobapi.go +++ b/platformapi/weimobapi/weimobapi.go @@ -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 := "" diff --git a/platformapi/weixinapi/weixinapi.go b/platformapi/weixinapi/weixinapi.go index efb9b9eb..174bc202 100644 --- a/platformapi/weixinapi/weixinapi.go +++ b/platformapi/weixinapi/weixinapi.go @@ -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 { diff --git a/platformapi/xiaowmapi/xiaowmapi.go b/platformapi/xiaowmapi/xiaowmapi.go index 11c0383d..81b5c3de 100644 --- a/platformapi/xiaowmapi/xiaowmapi.go +++ b/platformapi/xiaowmapi/xiaowmapi.go @@ -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 diff --git a/platformapi/yilianyunapi/yilianyunapi.go b/platformapi/yilianyunapi/yilianyunapi.go index 31b6525c..02c4d186 100644 --- a/platformapi/yilianyunapi/yilianyunapi.go +++ b/platformapi/yilianyunapi/yilianyunapi.go @@ -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{}) diff --git a/platformapi/zhongwuapi/zhongwuapi.go b/platformapi/zhongwuapi/zhongwuapi.go index e5f1b590..685fc83f 100644 --- a/platformapi/zhongwuapi/zhongwuapi.go +++ b/platformapi/zhongwuapi/zhongwuapi.go @@ -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{})