fnps
This commit is contained in:
96
platformapi/fnpsapi/fnpsapi.go
Normal file
96
platformapi/fnpsapi/fnpsapi.go
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
package fnpsapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.rosy.net.cn/baseapi"
|
||||||
|
"git.rosy.net.cn/baseapi/platformapi"
|
||||||
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sigKey = "signature"
|
||||||
|
|
||||||
|
TestURL = "https://exam-anubis.ele.me/anubis-webapi/v2"
|
||||||
|
URL = "https://open-anubis.ele.me/anubis-webapi/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type API struct {
|
||||||
|
accessToken string
|
||||||
|
appID string
|
||||||
|
appSecret string
|
||||||
|
client *http.Client
|
||||||
|
config *platformapi.APIConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(accessToken, appID, appSecret string, config ...*platformapi.APIConfig) *API {
|
||||||
|
curConfig := platformapi.DefAPIConfig
|
||||||
|
if len(config) > 0 {
|
||||||
|
curConfig = *config[0]
|
||||||
|
}
|
||||||
|
return &API{
|
||||||
|
accessToken: accessToken,
|
||||||
|
appID: appID,
|
||||||
|
appSecret: appSecret,
|
||||||
|
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("%s", a.appSecret))
|
||||||
|
var valueList2 = make([]string, len(valueList)+1)
|
||||||
|
at := copy(valueList2, valueList[:0])
|
||||||
|
at += copy(valueList2[at:], []string{a.appSecret})
|
||||||
|
copy(valueList2[at:], valueList[0:])
|
||||||
|
sig = strings.Join(valueList2, "")
|
||||||
|
binSig := md5.Sum([]byte(sig))
|
||||||
|
sig = fmt.Sprintf("%X", binSig)
|
||||||
|
return sig
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) AccessAPI(action string, url string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
|
||||||
|
data, _ := json.Marshal(bizParams)
|
||||||
|
fullURL := utils.GenerateGetURL(url, action, nil)
|
||||||
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||||
|
func() *http.Request {
|
||||||
|
var request *http.Request
|
||||||
|
if isPost {
|
||||||
|
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
||||||
|
request.Header.Set("Content-Type", "application/json")
|
||||||
|
} else {
|
||||||
|
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, bizParams), 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 utils.MustInterface2Int64(jsonResult1["code"]) != 200 {
|
||||||
|
errLevel = platformapi.ErrLevelGeneralFail
|
||||||
|
err = utils.NewErrorCode(jsonResult1["msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["code"])))
|
||||||
|
baseapi.SugarLogger.Debugf("ejiay AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||||
|
}
|
||||||
|
retVal = jsonResult1
|
||||||
|
}
|
||||||
|
return errLevel, err
|
||||||
|
})
|
||||||
|
return retVal, err
|
||||||
|
}
|
||||||
18
platformapi/fnpsapi/fnpsapi_test.go
Normal file
18
platformapi/fnpsapi/fnpsapi_test.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package fnpsapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"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("9ad9cf5fc8b140b19a1dbbb700b47676kown", "D5E8352BE0786ED16F77B4548F62F09A", "71e1061ac2f246f6ac27efb900edba12")
|
||||||
|
}
|
||||||
@@ -32,7 +32,7 @@ func TestCreateEntityStore(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDeleteStoresByID(t *testing.T) {
|
func TestDeleteStoresByID(t *testing.T) {
|
||||||
err := api.DeleteStoresByID(1000063844)
|
err := api.DeleteStoresByID(24563933)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user