- fix retry bug (request can not be reused).
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user