- fix retry bug (request can not be reused).

This commit is contained in:
gazebo
2018-08-15 22:06:57 +08:00
parent fa83668d82
commit 47e1b3989d
11 changed files with 65 additions and 40 deletions

View File

@@ -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
}