aa
This commit is contained in:
95
platformapi/ejyapi/ejyapi.go
Normal file
95
platformapi/ejyapi/ejyapi.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package ejyapi
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
sigKey = "sign"
|
||||
TestUrl = "https://dev.ejiayou.com"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
platformName string
|
||||
timeStamp int64
|
||||
beforeKey string
|
||||
afterKey string
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
}
|
||||
|
||||
func New(platformName string, timeStamp int64, beforeKey, afterKey string, config ...*platformapi.APIConfig) *API {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
return &API{
|
||||
platformName: platformName,
|
||||
timeStamp: timeStamp,
|
||||
beforeKey: beforeKey,
|
||||
afterKey: afterKey,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) signParam(params map[string]interface{}) (sig string) {
|
||||
var valueList []string
|
||||
for k, v := range params {
|
||||
if k != sigKey {
|
||||
if str := fmt.Sprint(v); str != "" {
|
||||
valueList = append(valueList, fmt.Sprintf("%s=%s", k, str))
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Sort(sort.StringSlice(valueList))
|
||||
valueList = append(valueList, fmt.Sprintf("timestamp=%d", a.timeStamp))
|
||||
valueList = append(valueList, fmt.Sprintf("beforeKey=%s", a.beforeKey))
|
||||
valueList = append(valueList, fmt.Sprintf("afterKey=%s", a.afterKey))
|
||||
sig = strings.Join(valueList, "&")
|
||||
fmt.Println(sig)
|
||||
binSig := md5.Sum([]byte(sig))
|
||||
sig = fmt.Sprintf("%X", binSig)
|
||||
return sig
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(action string, url string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, nil), nil)
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
if err == nil {
|
||||
if jsonResult1["error_response"] != nil {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
err = utils.NewErrorCode(jsonResult1["error_response"].(map[string]interface{})["zh_desc"].(string), jsonResult1["error_response"].(map[string]interface{})["code"].(string))
|
||||
baseapi.SugarLogger.Debugf("jdeclp AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
}
|
||||
retVal = jsonResult1
|
||||
}
|
||||
return errLevel, err
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func (a *API) GetStationList() (err error) {
|
||||
params := make(map[string]interface{})
|
||||
params["platformName"] = a.platformName
|
||||
sign := a.signParam(params)
|
||||
_, err = a.AccessAPI("oreo/ejiayou_open_api/stations/v2/"+a.platformName+"/"+sign+"/"+utils.Int64ToStr(a.timeStamp), TestUrl, nil)
|
||||
return err
|
||||
}
|
||||
25
platformapi/ejyapi/ejyapi_test.go
Normal file
25
platformapi/ejyapi/ejyapi_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package ejyapi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
api *API
|
||||
sugarLogger *zap.SugaredLogger
|
||||
)
|
||||
|
||||
func init() {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
sugarLogger = logger.Sugar()
|
||||
baseapi.Init(sugarLogger)
|
||||
api = New("1Zbve", time.Now().Unix(), "ymsrrxlZXlmglK6Q", "MYsFZGgwwprIahzQ")
|
||||
}
|
||||
|
||||
func TestGetStationList(t *testing.T) {
|
||||
api.GetStationList()
|
||||
}
|
||||
Reference in New Issue
Block a user