perbaikan

This commit is contained in:
2025-08-24 16:18:15 +07:00
parent 9838c48eab
commit 7681c796e8
24 changed files with 2443 additions and 2057 deletions

View File

@@ -12,6 +12,8 @@ import (
"api-service/internal/config"
"github.com/mashingan/smapping"
"github.com/rs/zerolog/log"
"github.com/tidwall/gjson"
)
// VClaimService interface for VClaim operations
@@ -49,6 +51,11 @@ type ResponDTO struct {
// NewService creates a new VClaim service instance
func NewService(cfg config.BpjsConfig) VClaimService {
log.Info().
Str("base_url", cfg.BaseURL).
Dur("timeout", cfg.Timeout).
Msg("Creating new VClaim service instance")
service := &Service{
config: cfg,
httpClient: &http.Client{
@@ -88,8 +95,20 @@ func (s *Service) SetHTTPClient(client *http.Client) {
// prepareRequest prepares HTTP request with required headers
func (s *Service) prepareRequest(ctx context.Context, method, endpoint string, body io.Reader) (*http.Request, error) {
fullURL := s.config.BaseURL + endpoint
log.Info().
Str("method", method).
Str("endpoint", endpoint).
Str("full_url", fullURL).
Msg("Preparing HTTP request")
req, err := http.NewRequestWithContext(ctx, method, fullURL, body)
if err != nil {
log.Error().
Err(err).
Str("method", method).
Str("endpoint", endpoint).
Msg("Failed to create HTTP request")
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -102,6 +121,14 @@ func (s *Service) prepareRequest(ctx context.Context, method, endpoint string, b
req.Header.Set("X-signature", xSignature)
req.Header.Set("user_key", userKey)
log.Debug().
Str("method", method).
Str("endpoint", endpoint).
Str("x_cons_id", consID).
Str("x_timestamp", tstamp).
Str("user_key", userKey).
Msg("Request headers set")
return req, nil
}
@@ -109,22 +136,55 @@ func (s *Service) prepareRequest(ctx context.Context, method, endpoint string, b
func (s *Service) processResponse(res *http.Response) (*ResponDTO, error) {
defer res.Body.Close()
log.Info().
Int("status_code", res.StatusCode).
Str("status", res.Status).
Msg("Processing HTTP response")
body, err := io.ReadAll(res.Body)
if err != nil {
log.Error().
Err(err).
Int("status_code", res.StatusCode).
Msg("Failed to read response body")
return nil, fmt.Errorf("failed to read response body: %w", err)
}
// Log response body for debugging (truncate if too long)
bodyStr := string(body)
if len(bodyStr) > 1000 {
bodyStr = bodyStr[:1000] + "...(truncated)"
}
log.Debug().
Int("status_code", res.StatusCode).
Str("response_body", bodyStr).
Msg("Raw response received")
// Check HTTP status
if res.StatusCode >= 400 {
log.Error().
Int("status_code", res.StatusCode).
Str("response_body", bodyStr).
Msg("HTTP error response")
return nil, fmt.Errorf("HTTP error: %d - %s", res.StatusCode, string(body))
}
// Parse raw response
var respMentah ResponMentahDTO
if err := json.Unmarshal(body, &respMentah); err != nil {
log.Error().
Err(err).
Int("status_code", res.StatusCode).
Msg("Failed to unmarshal raw response")
return nil, fmt.Errorf("failed to unmarshal raw response: %w", err)
}
// Log metadata
log.Info().
Str("meta_code", respMentah.MetaData.Code).
Str("meta_message", respMentah.MetaData.Message).
Msg("Response metadata")
// Create final response
finalResp := &ResponDTO{
MetaData: respMentah.MetaData,
@@ -132,6 +192,7 @@ func (s *Service) processResponse(res *http.Response) (*ResponDTO, error) {
// If response is empty, return as is
if respMentah.Response == "" {
log.Debug().Msg("Empty response received, returning metadata only")
return finalResp, nil
}
@@ -139,17 +200,47 @@ func (s *Service) processResponse(res *http.Response) (*ResponDTO, error) {
consID, secretKey, _, tstamp, _ := s.config.SetHeader()
respDecrypt, err := ResponseVclaim(respMentah.Response, consID+secretKey+tstamp)
if err != nil {
log.Error().
Err(err).
Str("meta_code", respMentah.MetaData.Code).
Msg("Failed to decrypt response")
return nil, fmt.Errorf("failed to decrypt response: %w", err)
}
log.Debug().
Str("encrypted_length", fmt.Sprintf("%d bytes", len(respMentah.Response))).
Str("decrypted_length", fmt.Sprintf("%d bytes", len(respDecrypt))).
Msg("Response decrypted successfully")
// Unmarshal decrypted response
if respDecrypt != "" {
if err := json.Unmarshal([]byte(respDecrypt), &finalResp.Response); err != nil {
// If JSON unmarshal fails, store as string
log.Warn().
Err(err).
Msg("Failed to unmarshal decrypted response, storing as string")
finalResp.Response = respDecrypt
} else {
log.Debug().Msg("Decrypted response unmarshaled successfully")
// Use gjson to extract and log some metadata from the response if it's JSON
if jsonBytes, err := json.Marshal(finalResp.Response); err == nil {
jsonStr := string(jsonBytes)
// Extract some common fields using gjson
if metaCode := gjson.Get(jsonStr, "metaData.code"); metaCode.Exists() {
log.Info().
Str("response_meta_code", metaCode.String()).
Msg("Final response metadata")
}
}
}
}
log.Info().
Str("meta_code", finalResp.MetaData.Code).
Str("meta_message", finalResp.MetaData.Message).
Msg("Response processing completed")
return finalResp, nil
}