fadd
This commit is contained in:
@@ -109,8 +109,6 @@ func (s *RongxingService) CallAPI(ctx context.Context, apiPath string, reqData m
|
|||||||
bodyStr := string(bodyBytes)
|
bodyStr := string(bodyBytes)
|
||||||
|
|
||||||
for attempt := 0; attempt < 2; attempt++ {
|
for attempt := 0; attempt < 2; attempt++ {
|
||||||
startTime := time.Now()
|
|
||||||
|
|
||||||
token, err := s.getToken(ctx, transactionID)
|
token, err := s.getToken(ctx, transactionID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -121,11 +119,6 @@ func (s *RongxingService) CallAPI(ctx context.Context, apiPath string, reqData m
|
|||||||
headerDmsToken: token,
|
headerDmsToken: token,
|
||||||
}
|
}
|
||||||
curlCmd := generateCurlCommand(http.MethodPost, requestURL, headers, bodyStr)
|
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))
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(bodyBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -143,7 +136,6 @@ func (s *RongxingService) CallAPI(ctx context.Context, apiPath string, reqData m
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
duration := time.Since(startTime)
|
|
||||||
respStr := string(respBody)
|
respStr := string(respBody)
|
||||||
|
|
||||||
if statusCode == http.StatusUnauthorized || statusCode == http.StatusForbidden {
|
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()
|
code := resp.code()
|
||||||
payload := extractBusinessPayload(resp.Data)
|
payload := extractBusinessPayload(resp.Data)
|
||||||
s.logResponse(transactionID, apiKey, statusCode, duration, respStr)
|
|
||||||
|
|
||||||
// 扣费只看 consumeFlag:1 扣费(按成功返回),0 不扣费
|
// 扣费只看 consumeFlag:1 扣费(按成功返回),0 不扣费
|
||||||
if IsBillable(resp.ConsumeFlag) {
|
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"}
|
headers := map[string]string{"Content-Type": "application/json"}
|
||||||
curlCmd := generateCurlCommand(http.MethodPost, requestURL, headers, bodyStr)
|
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))
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestURL, bytes.NewBuffer(bodyBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -277,14 +263,12 @@ func (s *RongxingService) login(ctx context.Context, transactionID string) (stri
|
|||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
startTime := time.Now()
|
|
||||||
respBody, statusCode, err := s.doHTTP(req)
|
respBody, statusCode, err := s.doHTTP(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = errors.Join(ErrDatasource, err)
|
err = errors.Join(ErrDatasource, err)
|
||||||
s.logErrorWithCurl(transactionID, apiKeyLogin, err, nil, curlCmd, "")
|
s.logErrorWithCurl(transactionID, apiKeyLogin, err, nil, curlCmd, "")
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
duration := time.Since(startTime)
|
|
||||||
respStr := string(respBody)
|
respStr := string(respBody)
|
||||||
|
|
||||||
if statusCode != http.StatusOK {
|
if statusCode != http.StatusOK {
|
||||||
@@ -321,7 +305,6 @@ func (s *RongxingService) login(ctx context.Context, transactionID string) (stri
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.logResponse(transactionID, apiKeyLogin, statusCode, duration, respStr)
|
|
||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,25 +374,6 @@ func extractBusinessPayload(data json.RawMessage) []byte {
|
|||||||
return data
|
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{}) {
|
func (s *RongxingService) logError(transactionID, apiKey, requestID string, err error, payload interface{}) {
|
||||||
if s.logger == nil {
|
if s.logger == nil {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user