- fk. Request.Close = true for weixin api.

This commit is contained in:
gazebo
2018-08-16 11:03:06 +08:00
parent 47e1b3989d
commit fa6ac348e3
8 changed files with 210 additions and 196 deletions

View File

@@ -2,6 +2,7 @@ package weixinapi
import (
"net/http"
"strings"
"sync"
"git.rosy.net.cn/baseapi/platformapi"
@@ -90,43 +91,46 @@ func (a *API) AccessAPI(action string, params map[string]interface{}, body strin
}
fullURL := utils.GenerateGetURL(prodURL, action, params2)
// baseapi.SugarLogger.Debug(fullURL)
// var request *http.Request
var method string
if body == "" {
method = http.MethodGet
// request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
} else {
method = http.MethodPost
// request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(body))
}
// request.Close = true // todo try to fix EOF error when accessing weixin api.
err = platformapi.AccessPlatformAPIWithRetry(a.client, method, fullURL, body, nil, a.config, func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
}
var errInfo *ErrorInfo
// 微信的返回值,在错误与正常情况下,结构是完全不一样的
if errCode, ok := jsonResult1["errcode"]; ok {
errInfo = &ErrorInfo{
ErrCode: int(utils.MustInterface2Int64(errCode)),
ErrMsg: jsonResult1["errmsg"].(string),
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
var request *http.Request
if body == "" {
request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
} else {
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(body))
}
if errInfo.ErrCode == 0 {
request.Close = true // todo try to fix EOF error when accessing weixin api.
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
}
var errInfo *ErrorInfo
// 微信的返回值,在错误与正常情况下,结构是完全不一样的
if errCode, ok := jsonResult1["errcode"]; ok {
errInfo = &ErrorInfo{
ErrCode: int(utils.MustInterface2Int64(errCode)),
ErrMsg: jsonResult1["errmsg"].(string),
}
if errInfo.ErrCode == 0 {
retVal = jsonResult1
}
} else {
retVal = jsonResult1
}
} else {
retVal = jsonResult1
}
if retVal != nil {
return platformapi.ErrLevelSuccess, nil
}
newErr := utils.NewErrorIntCode(errInfo.ErrMsg, errInfo.ErrCode)
if errInfo.ErrCode == ResponseCodeBusy {
return platformapi.ErrLevelRecoverableErr, newErr
}
return platformapi.ErrLevelCodeIsNotOK, newErr
})
if retVal != nil {
return platformapi.ErrLevelSuccess, nil
}
newErr := utils.NewErrorIntCode(errInfo.ErrMsg, errInfo.ErrCode)
if errInfo.ErrCode == ResponseCodeBusy {
return platformapi.ErrLevelRecoverableErr, newErr
}
return platformapi.ErrLevelCodeIsNotOK, newErr
})
return retVal, err
}