- mtwmapi
This commit is contained in:
143
platformapi/mtwmapi/mtwmapi.go
Normal file
143
platformapi/mtwmapi/mtwmapi.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package mtwmapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
apiURL = "https://waimaiopen.meituan.com/api/v1"
|
||||
sandboxAPIURL = "http://openapi.b.waimai.test.sankuai.com/api/v1"
|
||||
signKey = "sig"
|
||||
)
|
||||
|
||||
const (
|
||||
KeyAppPoiCode = "app_poi_code"
|
||||
KeyAppPoiCodes = "app_poi_codes"
|
||||
KeyAppFoodCode = "app_food_code"
|
||||
KeyImgName = "img_name"
|
||||
KeyImgData = "img_data"
|
||||
KeyOrderID = "order_id"
|
||||
)
|
||||
|
||||
const (
|
||||
GeneralMaxLimit = 200 // 大多数的API的批处理最大条数
|
||||
)
|
||||
|
||||
type API struct {
|
||||
appID string
|
||||
secret string
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
}
|
||||
|
||||
func New(appID, secret string, config ...*platformapi.APIConfig) *API {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
return &API{
|
||||
appID: appID,
|
||||
secret: secret,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) genURL(cmd string) string {
|
||||
return apiURL + "/" + cmd
|
||||
}
|
||||
|
||||
func (a *API) signParams(cmd string, params map[string]interface{}) string {
|
||||
keys := make([]string, 0)
|
||||
for k := range params {
|
||||
if k != signKey {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
finalStr := a.genURL(cmd) + "?"
|
||||
kvPaires := make([]string, len(keys))
|
||||
for k, key := range keys {
|
||||
if params[key] != nil {
|
||||
kvPaires[k] = key + "=" + fmt.Sprintf("%v", params[key])
|
||||
}
|
||||
}
|
||||
finalStr += strings.Join(kvPaires, "&") + a.secret
|
||||
// baseapi.SugarLogger.Debug(finalStr)
|
||||
return fmt.Sprintf("%x", md5.Sum([]byte(finalStr)))
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(cmd string, isGet bool, bizParams map[string]interface{}) (retVal interface{}, err error) {
|
||||
params := make(map[string]interface{})
|
||||
params["timestamp"] = time.Now().Unix()
|
||||
params["app_id"] = a.appID
|
||||
params = utils.MergeMaps(params, bizParams)
|
||||
imgData := params[KeyImgData]
|
||||
if imgData != nil {
|
||||
delete(params, KeyImgData)
|
||||
}
|
||||
params[signKey] = a.signParams(cmd, params)
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
var request *http.Request
|
||||
if isGet {
|
||||
fullURL := utils.GenerateGetURL(apiURL, cmd, params)
|
||||
// baseapi.SugarLogger.Debug(fullURL)
|
||||
request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
|
||||
} else {
|
||||
fullURL := a.genURL(cmd)
|
||||
// baseapi.SugarLogger.Debug(utils.Map2URLValues(params).Encode())
|
||||
if imgData == nil {
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
} else {
|
||||
var b bytes.Buffer
|
||||
w := multipart.NewWriter(&b)
|
||||
if fw, err := w.CreateFormFile("file", params[KeyImgName].(string)); err != nil {
|
||||
panic(err.Error())
|
||||
} else {
|
||||
fw.Write(imgData.([]byte))
|
||||
}
|
||||
for k, v := range params {
|
||||
fmt.Println(k, " ", v)
|
||||
w.WriteField(k, url.QueryEscape(fmt.Sprint(v)))
|
||||
}
|
||||
w.Close()
|
||||
// b.WriteString(utils.Map2URLValues(params).Encode())
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, &b)
|
||||
request.Header.Set("Content-Type", w.FormDataContentType())
|
||||
}
|
||||
request.Header.Set("charset", "UTF-8")
|
||||
}
|
||||
// request.Close = true //todo 为了性能考虑还是不要关闭
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response) (errLevel string, err error) {
|
||||
jsonResult1, err := utils.HTTPResponse2Json(response)
|
||||
// baseapi.SugarLogger.Debug(utils.Format4Output(jsonResult1, false))
|
||||
if err != nil {
|
||||
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
|
||||
}
|
||||
if _, ok := jsonResult1["error"]; ok {
|
||||
errorInfo := jsonResult1["error"].(map[string]interface{})
|
||||
newErr := utils.NewErrorIntCode(errorInfo["msg"].(string), int(utils.MustInterface2Int64(errorInfo["code"])))
|
||||
return platformapi.ErrLevelCodeIsNotOK, newErr
|
||||
}
|
||||
retVal = jsonResult1["data"]
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
Reference in New Issue
Block a user