- 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

@@ -99,8 +99,13 @@ func (a *API) AccessAPI(apiStr string, params map[string]interface{}) (retVal Re
params2 := utils.MergeMaps(utils.Params2Map("key", a.key, "output", "json"), params) params2 := utils.MergeMaps(utils.Params2Map("key", a.key, "output", "json"), params)
params2[signKey] = a.signParams(params2) params2[signKey] = a.signParams(params2)
// request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params2), nil) err = platformapi.AccessPlatformAPIWithRetry(a.client,
err = platformapi.AccessPlatformAPIWithRetry(a.client, http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params2), "", nil, a.config, func(response *http.Response) (errLevel string, err error) { func() *http.Request {
request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params2), nil)
return request
},
a.config,
func(response *http.Response) (errLevel string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response) jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil { if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong

View File

@@ -1,6 +1,7 @@
package dadaapi package dadaapi
import ( import (
"bytes"
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"net/http" "net/http"
@@ -107,11 +108,15 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
} }
params2[signKey] = a.signParams(params2) params2[signKey] = a.signParams(params2)
params2Bytes := utils.MustMarshal(params2) params2Bytes := utils.MustMarshal(params2)
// 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, http.MethodPost, a.url+"/"+action, string(params2Bytes), header, a.config, func(response *http.Response) (result string, err error) { err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
request, _ := http.NewRequest(http.MethodPost, a.url+"/"+action, bytes.NewReader(params2Bytes))
request.Header.Set("Content-Type", "application/json")
return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response) jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil { if err != nil {
return platformapi.ErrLevelGeneralFail, err return platformapi.ErrLevelGeneralFail, err

View File

@@ -1,6 +1,7 @@
package elmapi package elmapi
import ( import (
"bytes"
"crypto/md5" "crypto/md5"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
@@ -145,14 +146,18 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
} }
pl.Signature = a.signParams(action, pl) pl.Signature = a.signParams(action, pl)
plBytes := utils.MustMarshal(pl)
bodyStr := string(utils.MustMarshal(pl)) err = platformapi.AccessPlatformAPIWithRetry(a.client,
// request, _ := http.NewRequest(http.MethodPost, a.fullURL, strings.NewReader(bodyStr)) func() *http.Request {
header := make(http.Header) request, _ := http.NewRequest(http.MethodPost, a.fullURL, bytes.NewReader(plBytes))
header.Set("Content-Type", "application/json; charset=utf-8") request.Header.Set("Content-Type", "application/json; charset=utf-8")
header.Set("Content-Encoding", "gzip, deflate") request.Header.Set("Content-Encoding", "gzip, deflate")
header.Set("User-Agent", "eleme-golang-api") request.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) { return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response) jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil { if err != nil {
return platformapi.ErrLevelGeneralFail, err return platformapi.ErrLevelGeneralFail, err
@@ -195,30 +200,24 @@ func (a *API) getAuthorizeURL() string {
} }
func (a *API) AcccessAPI2(baseURL string, params map[string]interface{}, method string) (retVal map[string]interface{}, err error) { func (a *API) AcccessAPI2(baseURL string, params map[string]interface{}, method string) (retVal map[string]interface{}, err error) {
// var request *http.Request err = platformapi.AccessPlatformAPIWithRetry(a.client,
body := "" func() *http.Request {
requestURL := baseURL var request *http.Request
var header http.Header
if method == http.MethodPost { if method == http.MethodPost {
params2 := make(url.Values) params2 := make(url.Values)
for k, v := range params { for k, v := range params {
params2[k] = []string{fmt.Sprint(v)} params2[k] = []string{fmt.Sprint(v)}
} }
body = params2.Encode() request, _ = http.NewRequest(method, baseURL, strings.NewReader(params2.Encode()))
// request, _ = http.NewRequest(http.MethodPost, baseURL, strings.NewReader(params2.Encode())) request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
header = make(http.Header) request.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(a.appKey+":"+a.secret)))
header.Set("Content-Type", "application/x-www-form-urlencoded")
header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(a.appKey+":"+a.secret)))
} else { } else {
requestURL = utils.GenerateGetURL(baseURL, "", params) request, _ = http.NewRequest(method, utils.GenerateGetURL(baseURL, "", params), nil)
// 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) { return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response) jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil { if err != nil {
return platformapi.ErrLevelGeneralFail, err return platformapi.ErrLevelGeneralFail, err

View File

@@ -23,7 +23,7 @@ func init() {
// sandbox // sandbox
elmapi = New("623c0904c0d2499e83df15b62902eb65", "RwT214gAsS", "56afff4b9ebd8a7eb532d18fa33f17be57f9b9db", false) elmapi = New("623c0904c0d2499e83df15b62902eb65", "RwT214gAsS", "56afff4b9ebd8a7eb532d18fa33f17be57f9b9db", false)
// prod // prod
// elmapi = New("b0168650ad84a44e49d7613e88d31572", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true) // elmapi = New("fee89f45d06f93037dd314ceb78fde7b", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true)
} }
func TestTest(t *testing.T) { func TestTest(t *testing.T) {

View File

@@ -143,9 +143,15 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal
params["timestamp"] = utils.GetCurTimeStr() params["timestamp"] = utils.GetCurTimeStr()
sign := a.signParams(params) sign := a.signParams(params)
params[signKey] = sign params[signKey] = sign
// request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params), nil)
//request.Close = true //todo 为了性能考虑 err = platformapi.AccessPlatformAPIWithRetry(a.client,
err = platformapi.AccessPlatformAPIWithRetry(a.client, http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params), "", nil, a.config, func(response *http.Response) (errLevel string, err error) { func() *http.Request {
request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, apiStr, params), nil)
// request.Close = true //todo 为了性能考虑还是不要关闭
return request
},
a.config,
func(response *http.Response) (errLevel string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response) jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil { if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong

View File

@@ -193,11 +193,15 @@ func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal *R
params2["version"] = []string{"1.0"} params2["version"] = []string{"1.0"}
params2[signKey] = []string{a.signParams(params2)} params2[signKey] = []string{a.signParams(params2)}
// baseapi.SugarLogger.Debug(params2.Encode()) // baseapi.SugarLogger.Debug(params2.Encode())
// 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, http.MethodPost, mtpsAPIURL+"/"+action, params2.Encode(), header, a.config, func(response *http.Response) (result string, err error) { err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
request, _ := http.NewRequest(http.MethodPost, mtpsAPIURL+"/"+action, strings.NewReader(params2.Encode()))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response) jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil { if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong

View File

@@ -4,7 +4,6 @@ import (
"errors" "errors"
"net" "net"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/fatih/structs" "github.com/fatih/structs"
@@ -73,19 +72,11 @@ func init() {
structs.DefaultTagName = "json" structs.DefaultTagName = "json"
} }
func AccessPlatformAPIWithRetry(client *http.Client, method, requestURL, body string, header http.Header, config *APIConfig, handleResponse func(response *http.Response) (string, error)) error { func AccessPlatformAPIWithRetry(client *http.Client, handleRequest func() *http.Request, config *APIConfig, handleResponse func(response *http.Response) (string, error)) error {
exceedLimitRetryCount := 0 exceedLimitRetryCount := 0
recoverableErrorRetryCount := 0 recoverableErrorRetryCount := 0
var request *http.Request
for { for {
if body != "" { request := handleRequest()
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) response, err := client.Do(request)
if err != nil { if err != nil {
baseapi.SugarLogger.Debugf("AccessPlatformAPIWithRetry client.Get return err:%v", err) baseapi.SugarLogger.Debugf("AccessPlatformAPIWithRetry client.Get return err:%v", err)

View File

@@ -2,6 +2,7 @@ package weixinapi
import ( import (
"net/http" "net/http"
"strings"
"sync" "sync"
"git.rosy.net.cn/baseapi/platformapi" "git.rosy.net.cn/baseapi/platformapi"
@@ -90,17 +91,20 @@ func (a *API) AccessAPI(action string, params map[string]interface{}, body strin
} }
fullURL := utils.GenerateGetURL(prodURL, action, params2) fullURL := utils.GenerateGetURL(prodURL, action, params2)
// baseapi.SugarLogger.Debug(fullURL) // baseapi.SugarLogger.Debug(fullURL)
// var request *http.Request
var method string err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
var request *http.Request
if body == "" { if body == "" {
method = http.MethodGet request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
// request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
} else { } else {
method = http.MethodPost request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(body))
// request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(body))
} }
// request.Close = true // todo try to fix EOF error when accessing weixin api. 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) { return request
},
a.config,
func(response *http.Response) (result string, err error) {
jsonResult1, err := utils.HTTPResponse2Json(response) jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil { if err != nil {
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong