fnapi
This commit is contained in:
@@ -4,9 +4,13 @@ import (
|
|||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi"
|
"git.rosy.net.cn/baseapi"
|
||||||
"git.rosy.net.cn/baseapi/platformapi"
|
"git.rosy.net.cn/baseapi/platformapi"
|
||||||
@@ -16,18 +20,26 @@ import (
|
|||||||
const (
|
const (
|
||||||
sigKey = "signature"
|
sigKey = "signature"
|
||||||
|
|
||||||
TestURL = "https://exam-anubis.ele.me/anubis-webapi/v2"
|
TestURL = "https://exam-anubis.ele.me/anubis-webapi"
|
||||||
URL = "https://open-anubis.ele.me/anubis-webapi/v2"
|
URL = "https://open-anubis.ele.me/anubis-webapi"
|
||||||
|
tokenAction = "get_access_token"
|
||||||
)
|
)
|
||||||
|
|
||||||
type API struct {
|
type API struct {
|
||||||
accessToken string
|
accessToken string
|
||||||
appID string
|
appID string
|
||||||
appSecret string
|
appSecret string
|
||||||
|
locker sync.RWMutex
|
||||||
client *http.Client
|
client *http.Client
|
||||||
config *platformapi.APIConfig
|
config *platformapi.APIConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *API) SetToken(token string) {
|
||||||
|
a.locker.Lock()
|
||||||
|
defer a.locker.Unlock()
|
||||||
|
a.accessToken = token
|
||||||
|
}
|
||||||
|
|
||||||
func New(accessToken, appID, appSecret string, config ...*platformapi.APIConfig) *API {
|
func New(accessToken, appID, appSecret string, config ...*platformapi.APIConfig) *API {
|
||||||
curConfig := platformapi.DefAPIConfig
|
curConfig := platformapi.DefAPIConfig
|
||||||
if len(config) > 0 {
|
if len(config) > 0 {
|
||||||
@@ -47,34 +59,42 @@ func (a *API) signParam(params map[string]interface{}) (sig string) {
|
|||||||
for k, v := range params {
|
for k, v := range params {
|
||||||
if k != sigKey {
|
if k != sigKey {
|
||||||
if str := fmt.Sprint(v); str != "" {
|
if str := fmt.Sprint(v); str != "" {
|
||||||
valueList = append(valueList, fmt.Sprintf("%s%s", k, str))
|
valueList = append(valueList, fmt.Sprintf("%s=%s", k, str))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort.Sort(sort.StringSlice(valueList))
|
sort.Sort(sort.StringSlice(valueList))
|
||||||
valueList = append(valueList, fmt.Sprintf("%s", a.appSecret))
|
valueList = append(valueList, fmt.Sprintf("secret_key=%s", a.appSecret))
|
||||||
var valueList2 = make([]string, len(valueList)+1)
|
sig = strings.Join(valueList, "&")
|
||||||
at := copy(valueList2, valueList[:0])
|
binSig := md5.Sum([]byte(url.QueryEscape(sig)))
|
||||||
at += copy(valueList2[at:], []string{a.appSecret})
|
sig = fmt.Sprintf("%x", binSig)
|
||||||
copy(valueList2[at:], valueList[0:])
|
|
||||||
sig = strings.Join(valueList2, "")
|
|
||||||
binSig := md5.Sum([]byte(sig))
|
|
||||||
sig = fmt.Sprintf("%X", binSig)
|
|
||||||
return sig
|
return sig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *API) AccessAPI(action string, url string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
|
func (a *API) AccessAPI(action string, url string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
|
||||||
data, _ := json.Marshal(bizParams)
|
params := make(map[string]interface{})
|
||||||
|
params["salt"] = GetSalt()
|
||||||
|
params["app_id"] = a.appID
|
||||||
|
if action != tokenAction {
|
||||||
|
if a.accessToken == "" {
|
||||||
|
params["access_token"] = a.accessToken
|
||||||
|
}
|
||||||
|
data, _ := json.Marshal(bizParams)
|
||||||
|
params["data"] = string(data)
|
||||||
|
}
|
||||||
|
signStr := a.signParam(params)
|
||||||
|
params[sigKey] = signStr
|
||||||
|
data, _ := json.Marshal(params)
|
||||||
fullURL := utils.GenerateGetURL(url, action, nil)
|
fullURL := utils.GenerateGetURL(url, action, nil)
|
||||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||||
func() *http.Request {
|
func() *http.Request {
|
||||||
var request *http.Request
|
var request *http.Request
|
||||||
if isPost {
|
if isPost {
|
||||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
||||||
request.Header.Set("Content-Type", "application/json")
|
|
||||||
} else {
|
} else {
|
||||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, bizParams), nil)
|
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, params), nil)
|
||||||
}
|
}
|
||||||
|
request.Header.Set("Content-Type", "application/json")
|
||||||
return request
|
return request
|
||||||
},
|
},
|
||||||
a.config,
|
a.config,
|
||||||
@@ -86,7 +106,7 @@ func (a *API) AccessAPI(action string, url string, bizParams map[string]interfac
|
|||||||
if utils.MustInterface2Int64(jsonResult1["code"]) != 200 {
|
if utils.MustInterface2Int64(jsonResult1["code"]) != 200 {
|
||||||
errLevel = platformapi.ErrLevelGeneralFail
|
errLevel = platformapi.ErrLevelGeneralFail
|
||||||
err = utils.NewErrorCode(jsonResult1["msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["code"])))
|
err = utils.NewErrorCode(jsonResult1["msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["code"])))
|
||||||
baseapi.SugarLogger.Debugf("ejiay AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
baseapi.SugarLogger.Debugf("fnps AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||||
}
|
}
|
||||||
retVal = jsonResult1
|
retVal = jsonResult1
|
||||||
}
|
}
|
||||||
@@ -94,3 +114,22 @@ func (a *API) AccessAPI(action string, url string, bizParams map[string]interfac
|
|||||||
})
|
})
|
||||||
return retVal, err
|
return retVal, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetSalt() (salt int) {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
return rand.Intn(8999) + 1000
|
||||||
|
}
|
||||||
|
|
||||||
|
type TokenInfo struct {
|
||||||
|
AccessToken string `json:"access_token"`
|
||||||
|
AppID string `json:"app_id"`
|
||||||
|
ExpireTime int64 `json:"expire_time"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) {
|
||||||
|
result, err := a.AccessAPI(tokenAction, TestURL, nil, false)
|
||||||
|
if err == nil {
|
||||||
|
utils.Map2StructByJson(result["data"], &tokenInfo, false)
|
||||||
|
}
|
||||||
|
return tokenInfo, err
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package fnpsapi
|
package fnpsapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi"
|
"git.rosy.net.cn/baseapi"
|
||||||
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,5 +17,13 @@ func init() {
|
|||||||
logger, _ := zap.NewDevelopment()
|
logger, _ := zap.NewDevelopment()
|
||||||
sugarLogger = logger.Sugar()
|
sugarLogger = logger.Sugar()
|
||||||
baseapi.Init(sugarLogger)
|
baseapi.Init(sugarLogger)
|
||||||
// api = New("9ad9cf5fc8b140b19a1dbbb700b47676kown", "D5E8352BE0786ED16F77B4548F62F09A", "71e1061ac2f246f6ac27efb900edba12")
|
api = New("", "6a3e2073-1850-413b-9eb7-6c342ec36e1c", "a8248088-a742-4c33-a0db-03aeae00ca7d")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetAccessToken(t *testing.T) {
|
||||||
|
result, err := api.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(utils.Format4Output(result, false))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user