99 lines
2.7 KiB
Go
99 lines
2.7 KiB
Go
package tibiotapi
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"fmt"
|
|
"git.rosy.net.cn/baseapi"
|
|
"git.rosy.net.cn/baseapi/platformapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
prodURL = "https://api.tibiot.cn/api/v1"
|
|
signKey = "password"
|
|
)
|
|
|
|
const (
|
|
// ResponseCodeSuccess 操作成功
|
|
ResponseCodeSuccess = 0
|
|
)
|
|
|
|
var (
|
|
exceedLimitCodes = map[int]int{}
|
|
|
|
canRetryCodes = map[int]int{}
|
|
)
|
|
|
|
type API struct {
|
|
platformapi.APICookie
|
|
|
|
username string
|
|
password string
|
|
client *http.Client
|
|
config *platformapi.APIConfig
|
|
}
|
|
|
|
func New(username, password string, config ...*platformapi.APIConfig) *API {
|
|
curConfig := platformapi.DefAPIConfig
|
|
if len(config) > 0 {
|
|
curConfig = *config[0]
|
|
}
|
|
return &API{
|
|
username: username,
|
|
password: password,
|
|
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
|
config: &curConfig,
|
|
}
|
|
}
|
|
|
|
func (a *API) signParams(apiParams map[string]interface{}) string {
|
|
return fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%x", md5.Sum([]byte(a.username+apiParams["tkey"].(string)))))))
|
|
}
|
|
|
|
func (a *API) AccessAPI(url string, apiParams map[string]interface{}) (retVal interface{}, err error) {
|
|
params := utils.MergeMaps(map[string]interface{}{
|
|
"username": a.username,
|
|
}, apiParams)
|
|
fullURL := utils.GenerateGetURL(prodURL, url, nil)
|
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
|
func() *http.Request {
|
|
params["tkey"] = time.Now().Format("20060102150405")
|
|
sign := a.signParams(params)
|
|
params[signKey] = sign
|
|
request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
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")
|
|
}
|
|
code := int(utils.Interface2Int64WithDefault(jsonResult1["ret"], ResponseCodeSuccess))
|
|
if code == ResponseCodeSuccess {
|
|
retVal = jsonResult1["data"]
|
|
return platformapi.ErrLevelSuccess, nil
|
|
}
|
|
newErr := utils.NewErrorIntCode(jsonResult1["msg"].(string), code)
|
|
if _, ok := exceedLimitCodes[code]; ok {
|
|
return platformapi.ErrLevelExceedLimit, newErr
|
|
} else if _, ok := canRetryCodes[code]; ok {
|
|
return platformapi.ErrLevelRecoverableErr, newErr
|
|
} else {
|
|
baseapi.SugarLogger.Debugf("feie AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
|
return platformapi.ErrLevelCodeIsNotOK, newErr
|
|
}
|
|
})
|
|
return retVal, err
|
|
}
|
|
|
|
func (a *API) GetCardInfo(iccid string) (err error) {
|
|
a.AccessAPI("card/getCardInfo", map[string]interface{}{
|
|
"iccid": iccid,
|
|
})
|
|
return err
|
|
}
|