- fk. Request.Close = true for weixin api.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package elmapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
@@ -145,38 +146,42 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
|
||||
}
|
||||
|
||||
pl.Signature = a.signParams(action, pl)
|
||||
plBytes := utils.MustMarshal(pl)
|
||||
|
||||
bodyStr := string(utils.MustMarshal(pl))
|
||||
// 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
|
||||
}
|
||||
resultError, _ := jsonResult1["error"].(map[string]interface{})
|
||||
retVal = &ResponseResult{
|
||||
ID: jsonResult1["id"].(string),
|
||||
Error: resultError,
|
||||
Result: jsonResult1["result"],
|
||||
}
|
||||
errinfoMap := retVal.Error
|
||||
if errinfoMap == nil {
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
}
|
||||
code := errinfoMap["code"].(string)
|
||||
newErr := utils.NewErrorCode(errinfoMap["message"].(string), code)
|
||||
if code == "EXCEED_LIMIT" {
|
||||
return platformapi.ErrLevelExceedLimit, newErr
|
||||
} else if code == "SERVER_ERROR" || code == "BIZ_SYSTEM_ERROR" || code == "BIZ_1006" || code == "BUSINESS_ERROR" {
|
||||
return platformapi.ErrLevelRecoverableErr, newErr
|
||||
} else {
|
||||
return platformapi.ErrLevelCodeIsNotOK, newErr
|
||||
}
|
||||
})
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
request, _ := http.NewRequest(http.MethodPost, a.fullURL, bytes.NewReader(plBytes))
|
||||
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")
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response) (result string, err error) {
|
||||
jsonResult1, err := utils.HTTPResponse2Json(response)
|
||||
if err != nil {
|
||||
return platformapi.ErrLevelGeneralFail, err
|
||||
}
|
||||
resultError, _ := jsonResult1["error"].(map[string]interface{})
|
||||
retVal = &ResponseResult{
|
||||
ID: jsonResult1["id"].(string),
|
||||
Error: resultError,
|
||||
Result: jsonResult1["result"],
|
||||
}
|
||||
errinfoMap := retVal.Error
|
||||
if errinfoMap == nil {
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
}
|
||||
code := errinfoMap["code"].(string)
|
||||
newErr := utils.NewErrorCode(errinfoMap["message"].(string), code)
|
||||
if code == "EXCEED_LIMIT" {
|
||||
return platformapi.ErrLevelExceedLimit, newErr
|
||||
} else if code == "SERVER_ERROR" || code == "BIZ_SYSTEM_ERROR" || code == "BIZ_1006" || code == "BUSINESS_ERROR" {
|
||||
return platformapi.ErrLevelRecoverableErr, newErr
|
||||
} else {
|
||||
return platformapi.ErrLevelCodeIsNotOK, newErr
|
||||
}
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
@@ -195,37 +200,31 @@ 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
|
||||
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)}
|
||||
}
|
||||
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 {
|
||||
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, 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
|
||||
}
|
||||
retVal = jsonResult1
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
})
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
var request *http.Request
|
||||
if method == http.MethodPost {
|
||||
params2 := make(url.Values)
|
||||
for k, v := range params {
|
||||
params2[k] = []string{fmt.Sprint(v)}
|
||||
}
|
||||
request, _ = http.NewRequest(method, 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)))
|
||||
} else {
|
||||
request, _ = http.NewRequest(method, utils.GenerateGetURL(baseURL, "", params), nil)
|
||||
}
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response) (result string, err error) {
|
||||
jsonResult1, err := utils.HTTPResponse2Json(response)
|
||||
if err != nil {
|
||||
return platformapi.ErrLevelGeneralFail, err
|
||||
}
|
||||
retVal = jsonResult1
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
})
|
||||
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func init() {
|
||||
// sandbox
|
||||
elmapi = New("623c0904c0d2499e83df15b62902eb65", "RwT214gAsS", "56afff4b9ebd8a7eb532d18fa33f17be57f9b9db", false)
|
||||
// prod
|
||||
// elmapi = New("b0168650ad84a44e49d7613e88d31572", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true)
|
||||
// elmapi = New("fee89f45d06f93037dd314ceb78fde7b", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true)
|
||||
}
|
||||
|
||||
func TestTest(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user