97 lines
2.8 KiB
Go
97 lines
2.8 KiB
Go
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
|
|
}
|