- autonavi api added.
- other refactor.
This commit is contained in:
148
platformapi/autonavi/autonavi.go
Normal file
148
platformapi/autonavi/autonavi.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package autonavi
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
signKey = "sig"
|
||||
prodURL = "https://restapi.amap.com/v3"
|
||||
)
|
||||
|
||||
const (
|
||||
StatusCodeFailed = "0"
|
||||
StatusCodeSuccess = "1"
|
||||
)
|
||||
|
||||
const (
|
||||
InfoCode_OK = "10000"
|
||||
InfoCode_ACCESS_TOO_FREQUENT = "10004"
|
||||
InfoCode_QPS_HAS_EXCEEDED_THE_LIMIT = "10014"
|
||||
InfoCode_SERVER_IS_BUSY = "10016"
|
||||
InfoCode_RESOURCE_UNAVAILABLE = "10017"
|
||||
InfoCode_CQPS_HAS_EXCEEDED_THE_LIMIT = "10019"
|
||||
InfoCode_CKQPS_HAS_EXCEEDED_THE_LIMIT = "10020"
|
||||
InfoCode_CIQPS_HAS_EXCEEDED_THE_LIMIT = "10021"
|
||||
InfoCode_CIKQPS_HAS_EXCEEDED_THE_LIMIT = "10022"
|
||||
InfoCode_KQPS_HAS_EXCEEDED_THE_LIMIT = "10023"
|
||||
)
|
||||
|
||||
const (
|
||||
CoordSysAutonavi = "autonavi"
|
||||
CoordSysGPS = "gps"
|
||||
CoordSysMapbar = "mapbar"
|
||||
CoordSysBaidu = "baidu"
|
||||
)
|
||||
|
||||
var (
|
||||
exceedLimitCodes = map[string]int{
|
||||
InfoCode_ACCESS_TOO_FREQUENT: 1,
|
||||
InfoCode_QPS_HAS_EXCEEDED_THE_LIMIT: 1,
|
||||
InfoCode_CQPS_HAS_EXCEEDED_THE_LIMIT: 1,
|
||||
InfoCode_CKQPS_HAS_EXCEEDED_THE_LIMIT: 1,
|
||||
InfoCode_CIQPS_HAS_EXCEEDED_THE_LIMIT: 1,
|
||||
InfoCode_CIKQPS_HAS_EXCEEDED_THE_LIMIT: 1,
|
||||
InfoCode_KQPS_HAS_EXCEEDED_THE_LIMIT: 1,
|
||||
}
|
||||
|
||||
canRetryCodes = map[string]int{
|
||||
InfoCode_SERVER_IS_BUSY: 1,
|
||||
}
|
||||
)
|
||||
|
||||
type ResponseResult map[string]interface{}
|
||||
|
||||
type API struct {
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
key string
|
||||
}
|
||||
|
||||
func New(key string, config ...*platformapi.APIConfig) *API {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
return &API{
|
||||
key: key,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) signParams(mapData map[string]interface{}) string {
|
||||
keys := make([]string, 0)
|
||||
for k := range mapData {
|
||||
if k != signKey {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
finalStr := ""
|
||||
for _, k := range keys {
|
||||
finalStr += k + "=" + fmt.Sprint(mapData[k])
|
||||
}
|
||||
|
||||
// baseapi.SugarLogger.Debugf("sign str:%v", finalStr)
|
||||
return fmt.Sprintf("%X", md5.Sum([]byte(finalStr)))
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(apiStr string, params map[string]interface{}) (retVal ResponseResult, err error) {
|
||||
params2 := utils.MergeMaps(utils.Params2Map("key", a.key, "output", "json"), params)
|
||||
params2[signKey] = a.signParams(params2)
|
||||
|
||||
finalURL, _ := url.Parse(utils.GenerateGetURL(prodURL, apiStr, params2))
|
||||
request := &http.Request{
|
||||
Method: "GET",
|
||||
URL: finalURL,
|
||||
}
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client, request, a.config, func(response *http.Response) (errLevel string, err error) {
|
||||
jsonResult1, err := utils.HTTPResponse2Json(response)
|
||||
if err != nil {
|
||||
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
|
||||
}
|
||||
status := jsonResult1["status"].(string)
|
||||
if status == StatusCodeSuccess {
|
||||
retVal = jsonResult1
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
}
|
||||
baseapi.SugarLogger.Warnf("response business code is not ok, data:%v, status:%v", jsonResult1, status)
|
||||
infoCode := jsonResult1["infocode"].(string)
|
||||
newErr := utils.NewErrorCode(jsonResult1["info"].(string), infoCode)
|
||||
if _, ok := exceedLimitCodes[infoCode]; ok {
|
||||
return platformapi.ErrLevelExceedLimit, newErr
|
||||
} else if _, ok := canRetryCodes[infoCode]; ok {
|
||||
return platformapi.ErrLevelRecoverableErr, newErr
|
||||
} else {
|
||||
return platformapi.ErrLevelCodeIsNotOK, newErr
|
||||
}
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func (a *API) CoordinateConvert(lng, lat float64, coordsys string) (retLng, retLat float64, err error) {
|
||||
if coordsys == "" || coordsys == CoordSysAutonavi {
|
||||
return lng, lat, nil
|
||||
}
|
||||
params := map[string]interface{}{
|
||||
"locations": fmt.Sprintf("%.6f,%.6f", lng, lat),
|
||||
"coordsys": coordsys,
|
||||
}
|
||||
result, err := a.AccessAPI("assistant/coordinate/convert", params)
|
||||
if err == nil {
|
||||
coordinate := result["locations"].(string)
|
||||
index := strings.Index(coordinate, ",")
|
||||
return utils.Str2Float64(coordinate[:index]), utils.Str2Float64(coordinate[index+1:]), nil
|
||||
}
|
||||
return 0.0, 0.0, err
|
||||
}
|
||||
37
platformapi/autonavi/autonavi_test.go
Normal file
37
platformapi/autonavi/autonavi_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package autonavi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
autonaviAPI *API
|
||||
sugarLogger *zap.SugaredLogger
|
||||
)
|
||||
|
||||
func init() {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
sugarLogger = logger.Sugar()
|
||||
baseapi.Init(sugarLogger)
|
||||
|
||||
autonaviAPI = New("4427170f870af2110becb8852d36ab08")
|
||||
}
|
||||
|
||||
func TestCoordinateConvert(t *testing.T) {
|
||||
gpsLng := 116.481499
|
||||
gpsLat := 39.990475
|
||||
desiredLng := 116.487585177952
|
||||
desiredLat := 39.991754014757
|
||||
lng, lat, err := autonaviAPI.CoordinateConvert(gpsLng, gpsLat, CoordSysGPS)
|
||||
if err != nil {
|
||||
t.Fatalf("TestCoordinateConvert failed with error:%v", err)
|
||||
} else {
|
||||
if lng != desiredLng || lat != desiredLat {
|
||||
t.Fatal("CoordinateConvert result is wrong")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,10 +142,10 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal
|
||||
sign := a.signParams(params)
|
||||
params[signKey] = sign
|
||||
|
||||
url, _ := url.Parse(utils.GenerateGetURL(prodURL, apiStr, params))
|
||||
finalURL, _ := url.Parse(utils.GenerateGetURL(prodURL, apiStr, params))
|
||||
request := &http.Request{
|
||||
Method: "GET",
|
||||
URL: url,
|
||||
URL: finalURL,
|
||||
}
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client, request, a.config, func(response *http.Response) (errLevel string, err error) {
|
||||
jsonResult1, err := utils.HTTPResponse2Json(response)
|
||||
|
||||
@@ -39,6 +39,16 @@ const (
|
||||
DeliveryStatusFinished = "40"
|
||||
)
|
||||
|
||||
const (
|
||||
PromotionTypeNormal = 1
|
||||
PromotionTypeSeckill = 2
|
||||
PromotionTypeDirectDown = 3
|
||||
PromotionTypeLimitedTime = 4
|
||||
PromotionTypeAddMoneyBuy = 1202
|
||||
PromotionTypeOverflowGiveGift = 1203
|
||||
PromotionTypeBuyGiveGift = 6
|
||||
)
|
||||
|
||||
func (a API) OrderQuery(jdParams map[string]interface{}) (retVal []interface{}, err error) {
|
||||
retVal, err = a.AccessAPIHavePage("order/es/query", jdParams, nil, nil, nil)
|
||||
return
|
||||
|
||||
@@ -129,6 +129,16 @@ type CreateOrderByShopParam struct {
|
||||
OrderType int `json:"order_type"`
|
||||
}
|
||||
|
||||
type GoodsItem struct {
|
||||
GoodCount int `json:"goodCount"`
|
||||
GoodName string `json:"goodName"`
|
||||
GoodPrice float64 `json:"goodPrice"`
|
||||
GoodUnit string `json:"goodUnit"`
|
||||
}
|
||||
type GoodsDetail struct {
|
||||
Goods []*GoodsItem `json:"goods"`
|
||||
}
|
||||
|
||||
type API struct {
|
||||
appKey string
|
||||
secret string
|
||||
|
||||
Reference in New Issue
Block a user