- autonavi api added.

- other refactor.
This commit is contained in:
gazebo
2018-07-14 13:13:58 +08:00
parent 1ec75cafd0
commit 90f21ffb8d
6 changed files with 267 additions and 42 deletions

View 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
}

View 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")
}
}
}