fadd
This commit is contained in:
@@ -45,12 +45,12 @@ type RongxingService struct {
|
||||
|
||||
// apiResponse 戎行统一响应。code 可能是数字或字符串;扣费以 consumeFlag 为准。
|
||||
type apiResponse struct {
|
||||
Flag bool `json:"flag"`
|
||||
Code json.RawMessage `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Message string `json:"message"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
ConsumeFlag int `json:"consumeFlag"`
|
||||
Flag bool `json:"flag"`
|
||||
Code json.RawMessage `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Message string `json:"message"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
ConsumeFlag int `json:"consumeFlag"`
|
||||
}
|
||||
|
||||
func (r apiResponse) text() string {
|
||||
@@ -109,8 +109,6 @@ func (s *RongxingService) CallAPI(ctx context.Context, apiPath string, reqData m
|
||||
bodyStr := string(bodyBytes)
|
||||
|
||||
for attempt := 0; attempt < 2; attempt++ {
|
||||
startTime := time.Now()
|
||||
|
||||
token, err := s.getToken(ctx, transactionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -121,11 +119,6 @@ func (s *RongxingService) CallAPI(ctx context.Context, apiPath string, reqData m
|
||||
headerDmsToken: token,
|
||||
}
|
||||
curlCmd := generateCurlCommand(http.MethodPost, requestURL, headers, bodyStr)
|
||||
s.logCurl(transactionID, apiKey, requestURL, curlCmd)
|
||||
|
||||
if s.logger != nil {
|
||||
s.logger.LogRequest("", transactionID, apiKey, requestURL)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(bodyBytes))
|
||||
if err != nil {
|
||||
@@ -143,7 +136,6 @@ func (s *RongxingService) CallAPI(ctx context.Context, apiPath string, reqData m
|
||||
return nil, err
|
||||
}
|
||||
|
||||
duration := time.Since(startTime)
|
||||
respStr := string(respBody)
|
||||
|
||||
if statusCode == http.StatusUnauthorized || statusCode == http.StatusForbidden {
|
||||
@@ -171,7 +163,6 @@ func (s *RongxingService) CallAPI(ctx context.Context, apiPath string, reqData m
|
||||
|
||||
code := resp.code()
|
||||
payload := extractBusinessPayload(resp.Data)
|
||||
s.logResponse(transactionID, apiKey, statusCode, duration, respStr)
|
||||
|
||||
// 扣费只看 consumeFlag:1 扣费(按成功返回),0 不扣费
|
||||
if IsBillable(resp.ConsumeFlag) {
|
||||
@@ -263,11 +254,6 @@ func (s *RongxingService) login(ctx context.Context, transactionID string) (stri
|
||||
|
||||
headers := map[string]string{"Content-Type": "application/json"}
|
||||
curlCmd := generateCurlCommand(http.MethodPost, requestURL, headers, bodyStr)
|
||||
s.logCurl(transactionID, apiKeyLogin, requestURL, curlCmd)
|
||||
|
||||
if s.logger != nil {
|
||||
s.logger.LogRequest("", transactionID, apiKeyLogin, requestURL)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(bodyBytes))
|
||||
if err != nil {
|
||||
@@ -277,14 +263,12 @@ func (s *RongxingService) login(ctx context.Context, transactionID string) (stri
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
startTime := time.Now()
|
||||
respBody, statusCode, err := s.doHTTP(req)
|
||||
if err != nil {
|
||||
err = errors.Join(ErrDatasource, err)
|
||||
s.logErrorWithCurl(transactionID, apiKeyLogin, err, nil, curlCmd, "")
|
||||
return "", err
|
||||
}
|
||||
duration := time.Since(startTime)
|
||||
respStr := string(respBody)
|
||||
|
||||
if statusCode != http.StatusOK {
|
||||
@@ -321,7 +305,6 @@ func (s *RongxingService) login(ctx context.Context, transactionID string) (stri
|
||||
return "", err
|
||||
}
|
||||
|
||||
s.logResponse(transactionID, apiKeyLogin, statusCode, duration, respStr)
|
||||
return token, nil
|
||||
}
|
||||
|
||||
@@ -391,25 +374,6 @@ func extractBusinessPayload(data json.RawMessage) []byte {
|
||||
return data
|
||||
}
|
||||
|
||||
func (s *RongxingService) logCurl(transactionID, apiKey, url, curlCmd string) {
|
||||
if s.logger == nil {
|
||||
return
|
||||
}
|
||||
s.logger.LogInfo("rongxing curl",
|
||||
zap.String("transaction_id", transactionID),
|
||||
zap.String("api_code", apiKey),
|
||||
zap.String("url", url),
|
||||
zap.String("curl", curlCmd),
|
||||
)
|
||||
}
|
||||
|
||||
func (s *RongxingService) logResponse(transactionID, apiKey string, statusCode int, duration time.Duration, responseBody string) {
|
||||
if s.logger == nil {
|
||||
return
|
||||
}
|
||||
s.logger.LogResponseWithBody("", transactionID, apiKey, statusCode, duration, responseBody)
|
||||
}
|
||||
|
||||
func (s *RongxingService) logError(transactionID, apiKey, requestID string, err error, payload interface{}) {
|
||||
if s.logger == nil {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user