From 47e1b3989d1f91114cad4d9d37e0bd37ae73038b Mon Sep 17 00:00:00 2001 From: gazebo Date: Wed, 15 Aug 2018 22:06:57 +0800 Subject: [PATCH] - fix retry bug (request can not be reused). --- platformapi/autonavi/autonavi.go | 4 +-- platformapi/dadaapi/dadaapi.go | 8 +++--- platformapi/dadaapi/dadaapi_test.go | 3 +- platformapi/elmapi/elmapi.go | 43 +++++++++++++++++------------ platformapi/elmapi/elmapi_test.go | 5 ++-- platformapi/jdapi/jdapi.go | 4 +-- platformapi/jdapi/jdapi_test.go | 2 +- platformapi/mtpsapi/mtpsapi.go | 7 +++-- platformapi/mtpsapi/mtpsapi_test.go | 3 ++ platformapi/platformapi.go | 12 +++++++- platformapi/weixinapi/weixinapi.go | 14 ++++++---- 11 files changed, 65 insertions(+), 40 deletions(-) diff --git a/platformapi/autonavi/autonavi.go b/platformapi/autonavi/autonavi.go index 0a26dd7b..39959c64 100644 --- a/platformapi/autonavi/autonavi.go +++ b/platformapi/autonavi/autonavi.go @@ -99,8 +99,8 @@ func (a *API) AccessAPI(apiStr string, params map[string]interface{}) (retVal Re params2 := utils.MergeMaps(utils.Params2Map("key", a.key, "output", "json"), params) params2[signKey] = a.signParams(params2) - request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params2), nil) - err = platformapi.AccessPlatformAPIWithRetry(a.client, request, a.config, func(response *http.Response) (errLevel string, err error) { + // request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params2), nil) + err = platformapi.AccessPlatformAPIWithRetry(a.client, http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params2), "", nil, a.config, func(response *http.Response) (errLevel string, err error) { jsonResult1, err := utils.HTTPResponse2Json(response) if err != nil { return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong diff --git a/platformapi/dadaapi/dadaapi.go b/platformapi/dadaapi/dadaapi.go index f6f2f872..91fb8372 100644 --- a/platformapi/dadaapi/dadaapi.go +++ b/platformapi/dadaapi/dadaapi.go @@ -1,7 +1,6 @@ package dadaapi import ( - "bytes" "crypto/md5" "fmt" "net/http" @@ -108,10 +107,11 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R } params2[signKey] = a.signParams(params2) params2Bytes := utils.MustMarshal(params2) - request, _ := http.NewRequest("POST", a.url+"/"+action, bytes.NewReader(params2Bytes)) - request.Header.Set("Content-Type", "application/json") + // request, _ := http.NewRequest(http.MethodPost, a.url+"/"+action, bytes.NewReader(params2Bytes)) + header := make(http.Header) + header.Set("Content-Type", "application/json") - err = platformapi.AccessPlatformAPIWithRetry(a.client, request, a.config, func(response *http.Response) (result string, err error) { + err = platformapi.AccessPlatformAPIWithRetry(a.client, http.MethodPost, a.url+"/"+action, string(params2Bytes), header, a.config, func(response *http.Response) (result string, err error) { jsonResult1, err := utils.HTTPResponse2Json(response) if err != nil { return platformapi.ErrLevelGeneralFail, err diff --git a/platformapi/dadaapi/dadaapi_test.go b/platformapi/dadaapi/dadaapi_test.go index 6f684c8d..979f7eb6 100644 --- a/platformapi/dadaapi/dadaapi_test.go +++ b/platformapi/dadaapi/dadaapi_test.go @@ -1,6 +1,7 @@ package dadaapi import ( + "net/http" "testing" "git.rosy.net.cn/baseapi" @@ -161,7 +162,7 @@ func TestCallbackMsgPlayback(t *testing.T) { }, } for _, v := range playbackData { - _, err := utils.SendFakeRequest("POST", v[1], v[0], "application/json; charset=utf-8") + _, err := utils.SendFakeRequest(http.MethodPost, v[1], v[0], "application/json; charset=utf-8") if err != nil { t.Fatal(err) } diff --git a/platformapi/elmapi/elmapi.go b/platformapi/elmapi/elmapi.go index a60220b0..261dc97c 100644 --- a/platformapi/elmapi/elmapi.go +++ b/platformapi/elmapi/elmapi.go @@ -147,11 +147,12 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R pl.Signature = a.signParams(action, pl) bodyStr := string(utils.MustMarshal(pl)) - request, _ := http.NewRequest(http.MethodPost, a.fullURL, strings.NewReader(bodyStr)) - request.Header.Set("Content-Type", "application/json; charset=utf-8") - request.Header.Set("Content-Encoding", "gzip, deflate") - request.Header.Set("User-Agent", "eleme-golang-api") - err = platformapi.AccessPlatformAPIWithRetry(a.client, request, a.config, func(response *http.Response) (result string, err error) { + // request, _ := http.NewRequest(http.MethodPost, a.fullURL, strings.NewReader(bodyStr)) + header := make(http.Header) + header.Set("Content-Type", "application/json; charset=utf-8") + header.Set("Content-Encoding", "gzip, deflate") + header.Set("User-Agent", "eleme-golang-api") + err = platformapi.AccessPlatformAPIWithRetry(a.client, http.MethodPost, a.fullURL, bodyStr, header, a.config, func(response *http.Response) (result string, err error) { jsonResult1, err := utils.HTTPResponse2Json(response) if err != nil { return platformapi.ErrLevelGeneralFail, err @@ -194,24 +195,30 @@ func (a *API) getAuthorizeURL() string { } func (a *API) AcccessAPI2(baseURL string, params map[string]interface{}, method string) (retVal map[string]interface{}, err error) { - var request *http.Request - if method == "POST" { + // var request *http.Request + body := "" + requestURL := baseURL + var header http.Header + if method == http.MethodPost { params2 := make(url.Values) for k, v := range params { params2[k] = []string{fmt.Sprint(v)} } - request, _ = http.NewRequest("POST", baseURL, strings.NewReader(params2.Encode())) - request.Header.Set("Content-Type", "application/x-www-form-urlencoded") - request.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(a.appKey+":"+a.secret))) + body = params2.Encode() + // request, _ = http.NewRequest(http.MethodPost, baseURL, strings.NewReader(params2.Encode())) + header = make(http.Header) + header.Set("Content-Type", "application/x-www-form-urlencoded") + header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(a.appKey+":"+a.secret))) } else { - fullURL, _ := url.Parse(utils.GenerateGetURL(baseURL, "", params)) - // baseapi.SugarLogger.Debug(url.String()) - request = &http.Request{ - Method: "GET", - URL: fullURL, - } + requestURL = utils.GenerateGetURL(baseURL, "", params) + // fullURL, _ := url.Parse(utils.GenerateGetURL(baseURL, "", params)) + // // baseapi.SugarLogger.Debug(url.String()) + // request = &http.Request{ + // Method: http.MethodGet, + // URL: fullURL, + // } } - err = platformapi.AccessPlatformAPIWithRetry(a.client, request, a.config, func(response *http.Response) (result string, err error) { + err = platformapi.AccessPlatformAPIWithRetry(a.client, method, requestURL, body, header, a.config, func(response *http.Response) (result string, err error) { jsonResult1, err := utils.HTTPResponse2Json(response) if err != nil { return platformapi.ErrLevelGeneralFail, err @@ -224,7 +231,7 @@ func (a *API) AcccessAPI2(baseURL string, params map[string]interface{}, method } func (a *API) RefreshTokenIndividual() (retVal *TokenInfo, err error) { - result, err := a.AcccessAPI2(a.getTokenURL(), utils.Params2Map("grant_type", "client_credentials", "scope", "all"), "POST") + result, err := a.AcccessAPI2(a.getTokenURL(), utils.Params2Map("grant_type", "client_credentials", "scope", "all"), http.MethodPost) if err != nil { return nil, err } diff --git a/platformapi/elmapi/elmapi_test.go b/platformapi/elmapi/elmapi_test.go index 90ad0420..888de393 100644 --- a/platformapi/elmapi/elmapi_test.go +++ b/platformapi/elmapi/elmapi_test.go @@ -1,6 +1,7 @@ package elmapi import ( + "net/http" "testing" "git.rosy.net.cn/baseapi" @@ -22,7 +23,7 @@ func init() { // sandbox elmapi = New("623c0904c0d2499e83df15b62902eb65", "RwT214gAsS", "56afff4b9ebd8a7eb532d18fa33f17be57f9b9db", false) // prod - // elmapi = New("d2cbacee0359c1932e3e3827950cbbb1", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true) + // elmapi = New("b0168650ad84a44e49d7613e88d31572", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true) } func TestTest(t *testing.T) { @@ -115,7 +116,7 @@ func TestCallbackMsgPlayback(t *testing.T) { }, } for _, v := range playbackData { - _, err := utils.SendFakeRequest("POST", v[1], v[0], v[2]) + _, err := utils.SendFakeRequest(http.MethodPost, v[1], v[0], v[2]) if err != nil { t.Fatal(err) } diff --git a/platformapi/jdapi/jdapi.go b/platformapi/jdapi/jdapi.go index 58b54fd5..b0c4f2c4 100644 --- a/platformapi/jdapi/jdapi.go +++ b/platformapi/jdapi/jdapi.go @@ -143,9 +143,9 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal params["timestamp"] = utils.GetCurTimeStr() sign := a.signParams(params) params[signKey] = sign - request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params), nil) + // request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params), nil) //request.Close = true //todo 为了性能考虑 - err = platformapi.AccessPlatformAPIWithRetry(a.client, request, a.config, func(response *http.Response) (errLevel string, err error) { + err = platformapi.AccessPlatformAPIWithRetry(a.client, http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params), "", nil, a.config, func(response *http.Response) (errLevel string, err error) { jsonResult1, err := utils.HTTPResponse2Json(response) if err != nil { return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong diff --git a/platformapi/jdapi/jdapi_test.go b/platformapi/jdapi/jdapi_test.go index eab4dfd9..a12f29a1 100644 --- a/platformapi/jdapi/jdapi_test.go +++ b/platformapi/jdapi/jdapi_test.go @@ -225,7 +225,7 @@ func TestCallbackMsgPlayback(t *testing.T) { }, } for _, v := range playbackData { - _, err := utils.SendFakeRequest("POST", v[1], v[0], "") + _, err := utils.SendFakeRequest(http.MethodPost, v[1], v[0], "") if err != nil { t.Fatal(err) } diff --git a/platformapi/mtpsapi/mtpsapi.go b/platformapi/mtpsapi/mtpsapi.go index 82d4bdb2..68417e80 100644 --- a/platformapi/mtpsapi/mtpsapi.go +++ b/platformapi/mtpsapi/mtpsapi.go @@ -193,10 +193,11 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R params2["version"] = []string{"1.0"} params2[signKey] = []string{a.signParams(params2)} // baseapi.SugarLogger.Debug(params2.Encode()) - request, _ := http.NewRequest("POST", mtpsAPIURL+"/"+action, strings.NewReader(params2.Encode())) - request.Header.Set("Content-Type", "application/x-www-form-urlencoded") + // request, _ := http.NewRequest(http.MethodPost, mtpsAPIURL+"/"+action, strings.NewReader(params2.Encode())) + header := make(http.Header) + header.Set("Content-Type", "application/x-www-form-urlencoded") - err = platformapi.AccessPlatformAPIWithRetry(a.client, request, a.config, func(response *http.Response) (result string, err error) { + err = platformapi.AccessPlatformAPIWithRetry(a.client, http.MethodPost, mtpsAPIURL+"/"+action, params2.Encode(), header, a.config, func(response *http.Response) (result string, err error) { jsonResult1, err := utils.HTTPResponse2Json(response) if err != nil { return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong diff --git a/platformapi/mtpsapi/mtpsapi_test.go b/platformapi/mtpsapi/mtpsapi_test.go index 786a778e..f00acc36 100644 --- a/platformapi/mtpsapi/mtpsapi_test.go +++ b/platformapi/mtpsapi/mtpsapi_test.go @@ -1,6 +1,7 @@ package mtpsapi import ( + "strings" "testing" "git.rosy.net.cn/baseapi" @@ -33,6 +34,8 @@ func handleError(t *testing.T, err error) { } func TestTest(t *testing.T) { sugarLogger.Debug(utils.GetCurTimeStr()) + tmpStr := "this is a\u00A0" + sugarLogger.Debug(strings.Trim(tmpStr, " \u00A0")) } func TestAccessAPI(t *testing.T) { diff --git a/platformapi/platformapi.go b/platformapi/platformapi.go index 6f2a10eb..a06b1021 100644 --- a/platformapi/platformapi.go +++ b/platformapi/platformapi.go @@ -4,6 +4,7 @@ import ( "errors" "net" "net/http" + "strings" "time" "github.com/fatih/structs" @@ -72,10 +73,19 @@ func init() { structs.DefaultTagName = "json" } -func AccessPlatformAPIWithRetry(client *http.Client, request *http.Request, config *APIConfig, handleResponse func(response *http.Response) (string, error)) error { +func AccessPlatformAPIWithRetry(client *http.Client, method, requestURL, body string, header http.Header, config *APIConfig, handleResponse func(response *http.Response) (string, error)) error { exceedLimitRetryCount := 0 recoverableErrorRetryCount := 0 + var request *http.Request for { + if body != "" { + request, _ = http.NewRequest(method, requestURL, strings.NewReader(body)) + } else { + request, _ = http.NewRequest(method, requestURL, nil) + } + if header != nil { + request.Header = header + } response, err := client.Do(request) if err != nil { baseapi.SugarLogger.Debugf("AccessPlatformAPIWithRetry client.Get return err:%v", err) diff --git a/platformapi/weixinapi/weixinapi.go b/platformapi/weixinapi/weixinapi.go index abea8548..729e9c94 100644 --- a/platformapi/weixinapi/weixinapi.go +++ b/platformapi/weixinapi/weixinapi.go @@ -2,7 +2,6 @@ package weixinapi import ( "net/http" - "strings" "sync" "git.rosy.net.cn/baseapi/platformapi" @@ -91,14 +90,17 @@ 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 request *http.Request + var method string if body == "" { - request, _ = http.NewRequest(http.MethodGet, fullURL, nil) + method = http.MethodGet + // request, _ = http.NewRequest(http.MethodGet, fullURL, nil) } else { - request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(body)) + 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, request, a.config, func(response *http.Response) (result string, err error) { + // 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