- make sugarLogger baseapi globals.
This commit is contained in:
@@ -6,8 +6,8 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AccessPlatformAPIWithRetryParams struct {
|
||||
@@ -16,7 +16,6 @@ type AccessPlatformAPIWithRetryParams struct {
|
||||
SleepSecondWhenExceedLimit time.Duration
|
||||
Client *http.Client
|
||||
Request *http.Request
|
||||
SugarLogger *zap.SugaredLogger
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -47,7 +46,7 @@ func AccessPlatformAPIWithRetry(params *AccessPlatformAPIWithRetryParams, handle
|
||||
for {
|
||||
response, err := params.Client.Do(params.Request)
|
||||
if err != nil {
|
||||
params.SugarLogger.Debugf("client.Get return err:%v", err)
|
||||
baseapi.SugarLogger.Debugf("client.Get return err:%v", err)
|
||||
err, ok := err.(net.Error)
|
||||
recoverableErrorRetryCount++
|
||||
if ok && err.Timeout() && recoverableErrorRetryCount <= params.MaxRecoverableRetryCount {
|
||||
@@ -58,7 +57,7 @@ func AccessPlatformAPIWithRetry(params *AccessPlatformAPIWithRetryParams, handle
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != 200 {
|
||||
params.SugarLogger.Debugf("http code is:%d", response.StatusCode)
|
||||
baseapi.SugarLogger.Debugf("http code is:%d", response.StatusCode)
|
||||
recoverableErrorRetryCount++
|
||||
if recoverableErrorRetryCount <= params.MaxRecoverableRetryCount {
|
||||
continue
|
||||
|
||||
@@ -3,6 +3,7 @@ package elmapi
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platform/common"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"github.com/fatih/structs"
|
||||
@@ -49,7 +50,7 @@ func (e *ELMAPI) unmarshalData(data []byte, msg interface{}) (callbackResponse *
|
||||
func (e *ELMAPI) CheckRequestValidation(mapData map[string]interface{}) (callbackResponse *ELMCallbackResponse) {
|
||||
sign := e.signParamsMap(mapData, "")
|
||||
if remoteSign, ok := mapData[signKey].(string); ok && sign != remoteSign {
|
||||
e.sugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, remoteSign)
|
||||
baseapi.SugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, remoteSign)
|
||||
return &ELMCallbackResponse{Message: "signature is invalid"}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -10,9 +10,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platform/common"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -35,12 +35,11 @@ type ELMResult struct {
|
||||
}
|
||||
|
||||
type ELMAPI struct {
|
||||
token string
|
||||
appKey string
|
||||
secret string
|
||||
sugarLogger *zap.SugaredLogger
|
||||
url *url.URL
|
||||
client *http.Client
|
||||
token string
|
||||
appKey string
|
||||
secret string
|
||||
url *url.URL
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
type ELMPayload struct {
|
||||
@@ -53,13 +52,12 @@ type ELMPayload struct {
|
||||
Signature string `json:"signature"`
|
||||
}
|
||||
|
||||
func NewELMAPI(token, appKey, secret string, sugarLogger *zap.SugaredLogger, isProd bool) *ELMAPI {
|
||||
func NewELMAPI(token, appKey, secret string, isProd bool) *ELMAPI {
|
||||
api := &ELMAPI{
|
||||
token: token,
|
||||
appKey: appKey,
|
||||
secret: secret,
|
||||
sugarLogger: sugarLogger,
|
||||
client: &http.Client{Timeout: clientTimeout},
|
||||
token: token,
|
||||
appKey: appKey,
|
||||
secret: secret,
|
||||
client: &http.Client{Timeout: clientTimeout},
|
||||
}
|
||||
|
||||
if isProd {
|
||||
@@ -87,7 +85,7 @@ func (e *ELMAPI) signParamsMap(mapData map[string]interface{}, prefix string) st
|
||||
|
||||
sort.Strings(keyValues)
|
||||
finalStr := prefix + strings.Join(keyValues, "") + e.secret
|
||||
// e.sugarLogger.Debugf("sign str:%v", finalStr)
|
||||
// baseapi.SugarLogger.Debugf("sign str:%v", finalStr)
|
||||
return fmt.Sprintf("%X", md5.Sum([]byte(finalStr)))
|
||||
}
|
||||
|
||||
@@ -133,13 +131,12 @@ func (e *ELMAPI) AccessELM(action string, params map[string]interface{}) (retVal
|
||||
|
||||
Body: ioutil.NopCloser(strings.NewReader(string(utils.MustMarshal(payload)))),
|
||||
},
|
||||
SugarLogger: e.sugarLogger,
|
||||
}
|
||||
|
||||
err = common.AccessPlatformAPIWithRetry(apiAccess, func(response *http.Response) (result string, err error) {
|
||||
jsonResult1, err := utils.HttpResponse2Json(response)
|
||||
if err != nil {
|
||||
e.sugarLogger.Warnf("HttpResponse2Json return:%v", err)
|
||||
baseapi.SugarLogger.Warnf("HttpResponse2Json return:%v", err)
|
||||
return common.PAErrorLevelGeneralFail, err
|
||||
}
|
||||
resultError, _ := jsonResult1["error"].(map[string]interface{})
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platform/common"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
@@ -66,7 +67,7 @@ func (j *JDAPI) CheckRequestValidation(request *http.Request) (callbackResponse
|
||||
|
||||
sign := j.signParams(mapData)
|
||||
if sign != request.FormValue(signKey) {
|
||||
j.sugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, request.FormValue(signKey))
|
||||
baseapi.SugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, request.FormValue(signKey))
|
||||
return &JDCallbackResponse{
|
||||
Code: JDerrorCodeInvalidSign,
|
||||
Msg: "signature is invalid",
|
||||
|
||||
@@ -10,10 +10,10 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platform/common"
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -56,11 +56,10 @@ const (
|
||||
)
|
||||
|
||||
type JDAPI struct {
|
||||
token string
|
||||
appKey string
|
||||
appSecret string
|
||||
sugarLogger *zap.SugaredLogger
|
||||
client *http.Client
|
||||
token string
|
||||
appKey string
|
||||
appSecret string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -132,8 +131,8 @@ func genGetURL(baseURL, apiStr string, params map[string]string) string {
|
||||
return fmt.Sprintf(baseURL, apiStr) + fullURL
|
||||
}
|
||||
|
||||
func NewJDAPI(token, appKey, appSecret string, sugarLogger *zap.SugaredLogger) *JDAPI {
|
||||
return &JDAPI{token, appKey, appSecret, sugarLogger, &http.Client{Timeout: clientTimeout}}
|
||||
func NewJDAPI(token, appKey, appSecret string) *JDAPI {
|
||||
return &JDAPI{token, appKey, appSecret, &http.Client{Timeout: clientTimeout}}
|
||||
}
|
||||
|
||||
func (j *JDAPI) AccessJDQuery(apiStr string, jdParams map[string]string) (retVal map[string]interface{}, err error) {
|
||||
@@ -149,7 +148,7 @@ func (j *JDAPI) AccessJDQuery(apiStr string, jdParams map[string]string) (retVal
|
||||
}
|
||||
jdParamStr, err := json.Marshal(jdParams)
|
||||
if err != nil {
|
||||
j.sugarLogger.Errorf("Error when marshal %v, error:%v", jdParams, err)
|
||||
baseapi.SugarLogger.Errorf("Error when marshal %v, error:%v", jdParams, err)
|
||||
return nil, err
|
||||
}
|
||||
params["jd_param_json"] = string(jdParamStr)
|
||||
@@ -168,14 +167,13 @@ func (j *JDAPI) AccessJDQuery(apiStr string, jdParams map[string]string) (retVal
|
||||
Method: "GET",
|
||||
URL: url,
|
||||
},
|
||||
SugarLogger: j.sugarLogger,
|
||||
}
|
||||
|
||||
err = common.AccessPlatformAPIWithRetry(apiAccess, func(response *http.Response) (errLevel string, err error) {
|
||||
jsonResult1, err := utils.HttpResponse2Json(response)
|
||||
|
||||
if err != nil {
|
||||
j.sugarLogger.Warnf("HttpResponse2Json return:%v", err)
|
||||
baseapi.SugarLogger.Warnf("HttpResponse2Json return:%v", err)
|
||||
return common.PAErrorLevelGeneralFail, err
|
||||
}
|
||||
|
||||
@@ -184,7 +182,7 @@ func (j *JDAPI) AccessJDQuery(apiStr string, jdParams map[string]string) (retVal
|
||||
retVal = jsonResult1
|
||||
return common.PAErrorLevelSuccess, nil
|
||||
}
|
||||
j.sugarLogger.Debugf("jd code is:%s", code)
|
||||
baseapi.SugarLogger.Debugf("jd code is:%s", code)
|
||||
if _, ok := exceedLimitCodes[code]; ok {
|
||||
return common.PAErrorLevelExceedLimit, nil
|
||||
} else if _, ok := canRetryCodes[code]; ok {
|
||||
|
||||
@@ -3,6 +3,7 @@ package mtpsapi
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
@@ -42,12 +43,12 @@ func (m *MTPSAPI) CheckRequestValidation(request *http.Request) (callbackRespons
|
||||
request.ParseForm()
|
||||
sign := m.signParams(request.PostForm)
|
||||
if sign != request.FormValue(signKey) {
|
||||
m.sugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, request.FormValue(signKey))
|
||||
baseapi.SugarLogger.Infof("Signature is not ok, mine:%v, get:%v", sign, request.FormValue(signKey))
|
||||
return SignatureIsNotOk
|
||||
}
|
||||
|
||||
for _, valueKey := range []string{"delivery_id", "mt_peisong_id", "order_id"} {
|
||||
m.sugarLogger.Errorf("Missing mandatory param:%v", valueKey)
|
||||
baseapi.SugarLogger.Errorf("Missing mandatory param:%v", valueKey)
|
||||
if request.FormValue(valueKey) == "" {
|
||||
return &MtpsCallbackResponse{
|
||||
Code: -1,
|
||||
|
||||
@@ -10,9 +10,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platform/common"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -122,18 +122,16 @@ type MtpsCreateOrderByShopInfo struct {
|
||||
}
|
||||
|
||||
type MTPSAPI struct {
|
||||
appKey string
|
||||
secret string
|
||||
sugarLogger *zap.SugaredLogger
|
||||
client *http.Client
|
||||
appKey string
|
||||
secret string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func NewMTPSAPI(appKey, secret string, sugarLogger *zap.SugaredLogger) *MTPSAPI {
|
||||
func NewMTPSAPI(appKey, secret string) *MTPSAPI {
|
||||
api := &MTPSAPI{
|
||||
appKey: appKey,
|
||||
secret: secret,
|
||||
sugarLogger: sugarLogger,
|
||||
client: &http.Client{Timeout: clientTimeout},
|
||||
appKey: appKey,
|
||||
secret: secret,
|
||||
client: &http.Client{Timeout: clientTimeout},
|
||||
}
|
||||
|
||||
return api
|
||||
@@ -156,7 +154,7 @@ func (m *MTPSAPI) signParams(params url.Values) string {
|
||||
}
|
||||
}
|
||||
|
||||
// m.sugarLogger.Debug(finalStr)
|
||||
// baseapi.SugarLogger.Debug(finalStr)
|
||||
return fmt.Sprintf("%x", sha1.Sum([]byte(finalStr)))
|
||||
}
|
||||
|
||||
@@ -173,29 +171,28 @@ func (m *MTPSAPI) AccessMTPS(action string, params map[string]interface{}) (retV
|
||||
params2["timestamp"] = []string{utils.Int64ToStr(utils.GetCurTimestamp())}
|
||||
params2["version"] = []string{"1.0"}
|
||||
params2[signKey] = []string{m.signParams(params2)}
|
||||
// m.sugarLogger.Debug(params2.Encode())
|
||||
// baseapi.SugarLogger.Debug(params2.Encode())
|
||||
request, _ := http.NewRequest("POST", mtpsAPIURL+"/"+action, strings.NewReader(params2.Encode()))
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
apiAccess := &common.AccessPlatformAPIWithRetryParams{
|
||||
MaxExceedLimitRetryCount: maxRetryCountWhenReachLimited,
|
||||
MaxRecoverableRetryCount: maxRetryCountWhenNetworkException,
|
||||
SleepSecondWhenExceedLimit: sleepSecondWhenLimited,
|
||||
Client: m.client,
|
||||
Request: request,
|
||||
SugarLogger: m.sugarLogger,
|
||||
Client: m.client,
|
||||
Request: request,
|
||||
}
|
||||
|
||||
err = common.AccessPlatformAPIWithRetry(apiAccess, func(response *http.Response) (result string, err error) {
|
||||
jsonResult1, err := utils.HttpResponse2Json(response)
|
||||
if err != nil {
|
||||
m.sugarLogger.Warnf("HttpResponse2Json return:%v", err)
|
||||
baseapi.SugarLogger.Warnf("HttpResponse2Json return:%v", err)
|
||||
return common.PAErrorLevelGeneralFail, err
|
||||
}
|
||||
code := int(utils.MustInterface2Int64(jsonResult1["code"]))
|
||||
retVal = &MTPSResult{
|
||||
Code: code,
|
||||
}
|
||||
m.sugarLogger.Debug(jsonResult1)
|
||||
baseapi.SugarLogger.Debug(jsonResult1)
|
||||
if code == ResponseCodeSuccess {
|
||||
if innerData, ok := jsonResult1["data"]; ok {
|
||||
retVal.Data, _ = innerData.(map[string]interface{})
|
||||
@@ -242,7 +239,7 @@ func (m *MTPSAPI) CreateOrderByShop(basicParams *MtpsCreateOrderByShopInfo, addP
|
||||
delete(params, "expected_delivery_time")
|
||||
}
|
||||
if result, err := m.AccessMTPS("order/createByShop", params); err != nil {
|
||||
m.sugarLogger.Debugf("result:%v", result)
|
||||
baseapi.SugarLogger.Debugf("result:%v", result)
|
||||
return nil, utils.NewErrorIntCode(err.Error(), result.Code)
|
||||
} else {
|
||||
return m.result2OrderResponse(result), nil
|
||||
@@ -269,7 +266,7 @@ func (m *MTPSAPI) CancelOrder(deliveryId int64, mtPeiSongId string, cancelReason
|
||||
"cancel_reason": cancelReason,
|
||||
}
|
||||
if result, err := m.AccessMTPS("order/delete", params); err != nil {
|
||||
m.sugarLogger.Debugf("result:%v", result)
|
||||
baseapi.SugarLogger.Debugf("result:%v", result)
|
||||
return nil, err
|
||||
} else {
|
||||
return m.result2OrderResponse(result), nil
|
||||
|
||||
Reference in New Issue
Block a user