- basic ebaiapi added.
This commit is contained in:
105
platformapi/ebaiapi/ebaiapi.go
Normal file
105
platformapi/ebaiapi/ebaiapi.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package ebaiapi
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
prodURL = "https://api-be.ele.me/"
|
||||
|
||||
signKey = "sign"
|
||||
)
|
||||
|
||||
type ResponseResult struct {
|
||||
ErrNo int `json:"errno"`
|
||||
Error string `json:"error"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type API struct {
|
||||
source string
|
||||
secret string
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
}
|
||||
|
||||
func New(source, secret string, config ...*platformapi.APIConfig) *API {
|
||||
// baseapi.SugarLogger.Debugf("token=%v, appKey=%v, secret=%v", token, appKey, secret)
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
api := &API{
|
||||
source: source,
|
||||
secret: secret,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
return api
|
||||
}
|
||||
|
||||
func (a *API) signParams(params url.Values) string {
|
||||
keyValues := make([]string, 0)
|
||||
for k, v := range params {
|
||||
if k != signKey {
|
||||
keyValues = append(keyValues, k+"="+v[0])
|
||||
}
|
||||
}
|
||||
|
||||
sort.Strings(keyValues)
|
||||
finalStr := strings.Join(keyValues, "&")
|
||||
// baseapi.SugarLogger.Debugf("sign str:%v", finalStr)
|
||||
return fmt.Sprintf("%X", md5.Sum([]byte(finalStr)))
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *ResponseResult, err error) {
|
||||
if body == nil {
|
||||
body = make(map[string]interface{}, 0)
|
||||
}
|
||||
params := url.Values{
|
||||
"cmd": []string{cmd},
|
||||
"version": []string{"3"},
|
||||
"timestamp": []string{utils.Int64ToStr(utils.GetCurTimestamp())},
|
||||
"ticket": []string{utils.GetUpperUUID()},
|
||||
"source": []string{a.source},
|
||||
"body": []string{string(utils.MustMarshal(body))},
|
||||
"secret": []string{a.secret},
|
||||
}
|
||||
params[signKey] = []string{a.signParams(params)}
|
||||
encodedParams := params.Encode()
|
||||
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
request, _ := http.NewRequest(http.MethodPost, prodURL, strings.NewReader(encodedParams))
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
request.Header.Set("User-Agent", "ebai-golang-api")
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
func(response *http.Response) (result string, err error) {
|
||||
jsonResult1, err := utils.HTTPResponse2Json(response)
|
||||
if err != nil {
|
||||
return platformapi.ErrLevelGeneralFail, err
|
||||
}
|
||||
Body := jsonResult1["body"].(map[string]interface{})
|
||||
retVal = &ResponseResult{
|
||||
ErrNo: int(utils.MustInterface2Int64(Body["errno"])),
|
||||
Error: utils.Interface2String(Body["error"]),
|
||||
Data: Body["data"],
|
||||
}
|
||||
if retVal.ErrNo == 0 {
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
}
|
||||
newErr := utils.NewErrorIntCode(retVal.Error, retVal.ErrNo)
|
||||
return platformapi.ErrLevelCodeIsNotOK, newErr
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
Reference in New Issue
Block a user