- basic ebaiapi added.
This commit is contained in:
105
platformapi/ebaiapi/ebaiapi.go
Normal file
105
platformapi/ebaiapi/ebaiapi.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package ebaiapi
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
prodURL = "https://api-be.ele.me/"
|
||||
|
||||
signKey = "sign"
|
||||
)
|
||||
|
||||
type ResponseResult struct {
|
||||
ErrNo int `json:"errno"`
|
||||
Error string `json:"error"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type API struct {
|
||||
source string
|
||||
secret string
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
}
|
||||
|
||||
func New(source, secret string, config ...*platformapi.APIConfig) *API {
|
||||
// baseapi.SugarLogger.Debugf("token=%v, appKey=%v, secret=%v", token, appKey, secret)
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
api := &API{
|
||||
source: source,
|
||||
secret: secret,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
return api
|
||||
}
|
||||
|
||||
func (a *API) signParams(params url.Values) string {
|
||||
keyValues := make([]string, 0)
|
||||
for k, v := range params {
|
||||
if k != signKey {
|
||||
keyValues = append(keyValues, k+"="+v[0])
|
||||
}
|
||||
}
|
||||
|
||||
sort.Strings(keyValues)
|
||||
finalStr := strings.Join(keyValues, "&")
|
||||
// baseapi.SugarLogger.Debugf("sign str:%v", finalStr)
|
||||
return fmt.Sprintf("%X", md5.Sum([]byte(finalStr)))
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *ResponseResult, err error) {
|
||||
if body == nil {
|
||||
body = make(map[string]interface{}, 0)
|
||||
}
|
||||
params := url.Values{
|
||||
"cmd": []string{cmd},
|
||||
"version": []string{"3"},
|
||||
"timestamp": []string{utils.Int64ToStr(utils.GetCurTimestamp())},
|
||||
"ticket": []string{utils.GetUpperUUID()},
|
||||
"source": []string{a.source},
|
||||
"body": []string{string(utils.MustMarshal(body))},
|
||||
"secret": []string{a.secret},
|
||||
}
|
||||
params[signKey] = []string{a.signParams(params)}
|
||||
encodedParams := params.Encode()
|
||||
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
request, _ := http.NewRequest(http.MethodPost, prodURL, strings.NewReader(encodedParams))
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
request.Header.Set("User-Agent", "ebai-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
|
||||
}
|
||||
Body := jsonResult1["body"].(map[string]interface{})
|
||||
retVal = &ResponseResult{
|
||||
ErrNo: int(utils.MustInterface2Int64(Body["errno"])),
|
||||
Error: utils.Interface2String(Body["error"]),
|
||||
Data: Body["data"],
|
||||
}
|
||||
if retVal.ErrNo == 0 {
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
}
|
||||
newErr := utils.NewErrorIntCode(retVal.Error, retVal.ErrNo)
|
||||
return platformapi.ErrLevelCodeIsNotOK, newErr
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
42
platformapi/ebaiapi/ebaiapi_test.go
Normal file
42
platformapi/ebaiapi/ebaiapi_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package ebaiapi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
api *API
|
||||
sugarLogger *zap.SugaredLogger
|
||||
)
|
||||
|
||||
func init() {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
sugarLogger = logger.Sugar()
|
||||
baseapi.Init(sugarLogger)
|
||||
|
||||
// sandbox
|
||||
api = New("source", "secret")
|
||||
// prod
|
||||
// api = New("source", "secret")
|
||||
}
|
||||
|
||||
func TestTest(t *testing.T) {
|
||||
sugarLogger.Debug(utils.GetCurTimeStr())
|
||||
}
|
||||
|
||||
func TestAccessAPI(t *testing.T) {
|
||||
result, err := api.AccessAPI("shop.get", utils.Params2Map("baidu_shop_id", "2232527731"))
|
||||
if err != nil {
|
||||
t.Fatalf("Error when accessing AccessAPI result:%v, error:%v", result, err)
|
||||
} else {
|
||||
shopInfo := result.Data.(map[string]interface{})
|
||||
if len(shopInfo) > 0 {
|
||||
t.Fatalf("data is not correct:%v", shopInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
45
platformapi/ebaiapi/shop.go
Normal file
45
platformapi/ebaiapi/shop.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package ebaiapi
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
type ShopInfo struct {
|
||||
ShopID int `json:"shop_id"`
|
||||
BaiduShopID int `json:"baidu_shop_id"`
|
||||
Name string `json:"name"`
|
||||
Status int `json:"status"`
|
||||
SysStatus int `json:"sys_status"`
|
||||
OrderPush int `json:"order_push"`
|
||||
OrderStatusPush int `json:"order_status_push"`
|
||||
}
|
||||
|
||||
func (a *API) ShopList(orderPush, orderStatusPush, status, sysStatus int) (shopList []*ShopInfo, err error) {
|
||||
body := map[string]interface{}{}
|
||||
if orderPush >= 0 {
|
||||
body["order_push"] = orderPush
|
||||
}
|
||||
if orderStatusPush >= 0 {
|
||||
body["order_status_push"] = orderStatusPush
|
||||
}
|
||||
if status >= 0 {
|
||||
body["status"] = status
|
||||
}
|
||||
if sysStatus >= 0 {
|
||||
body["sys_status"] = sysStatus
|
||||
}
|
||||
result, err := a.AccessAPI("shop.list", body)
|
||||
if err == nil {
|
||||
list := result.Data.([]interface{})
|
||||
shopList = make([]*ShopInfo, len(list))
|
||||
for k, v := range list {
|
||||
mapData := v.(map[string]interface{})
|
||||
shopList[k] = &ShopInfo{
|
||||
ShopID: int(utils.MustInterface2Int64(mapData["shop_id"])),
|
||||
BaiduShopID: int(utils.MustInterface2Int64(mapData["baidu_shop_id"])),
|
||||
}
|
||||
}
|
||||
return shopList, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -77,7 +77,11 @@ func DictKeysMan(data interface{}, keysToRemove []string, keysToKeep []string) i
|
||||
|
||||
// 去除-号,全部大写,比如:929ADB626EB911E893E452540009DAB3
|
||||
func GetUUID() string {
|
||||
return strings.ToUpper(strings.Replace(uuid.Must(uuid.NewV1()).String(), "-", "", -1))
|
||||
return strings.Replace(GetUpperUUID(), "-", "", -1)
|
||||
}
|
||||
|
||||
func GetUpperUUID() string {
|
||||
return strings.ToUpper(uuid.Must(uuid.NewV1()).String())
|
||||
}
|
||||
|
||||
func GetCurTimeStr() string {
|
||||
|
||||
Reference in New Issue
Block a user