- change all request to NewRequest, don't use bare Request{}.

- MessageTemplateSend added.
- refactor weixin api.
This commit is contained in:
gazebo
2018-07-20 13:39:11 +08:00
parent dd45e6b31a
commit fda5f8e0ed
6 changed files with 82 additions and 62 deletions

View File

@@ -4,7 +4,6 @@ import (
"crypto/md5"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sort"
@@ -41,14 +40,14 @@ type TokenInfo struct {
}
type API struct {
token string
appKey string
secret string
isProd bool
url *url.URL
client *http.Client
config *platformapi.APIConfig
locker sync.RWMutex
token string
appKey string
secret string
isProd bool
fullURL string
client *http.Client
config *platformapi.APIConfig
locker sync.RWMutex
}
type payload struct {
@@ -77,9 +76,9 @@ func New(token, appKey, secret string, isProd bool, config ...*platformapi.APICo
}
if isProd {
api.url, _ = url.Parse(prodURL + "/")
api.fullURL = prodURL + "/"
} else {
api.url, _ = url.Parse(sandboxURL + "/")
api.fullURL = sandboxURL + "/"
}
return api
}
@@ -149,18 +148,10 @@ 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.Request{
Method: "POST",
URL: a.url,
Header: http.Header{
"Content-Type": []string{"application/json; charset=utf-8"},
"Content-Encoding": []string{"gzip, deflate"},
"User-Agent": []string{"eleme-golang-api"},
// "x-eleme-requestid": []string{payload.Id},
},
Body: ioutil.NopCloser(strings.NewReader(bodyStr)),
}
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) {
jsonResult1, err := utils.HTTPResponse2Json(response)
if err != nil {

View File

@@ -20,7 +20,7 @@ func init() {
baseapi.Init(sugarLogger)
// sandbox
elmapi = New("7b642f1767b6ad5c074238f1bf4720f8", "RwT214gAsS", "56afff4b9ebd8a7eb532d18fa33f17be57f9b9db", false)
elmapi = New("623c0904c0d2499e83df15b62902eb65", "RwT214gAsS", "56afff4b9ebd8a7eb532d18fa33f17be57f9b9db", false)
// prod
// elmapi = New("a530ade4a8e0b99542df0592b06575b5", "KLRDcOZGrk", "1fc221f8265506531da36fb613d5f5ad673f2e9a", true)