|
1 | 1 | package it.gov.pagopa.idpay.transactions.connector.rest; |
2 | 2 |
|
3 | 3 | import it.gov.pagopa.common.reactive.utils.PerformanceLogger; |
| 4 | +import it.gov.pagopa.common.web.exception.ClientExceptionWithBody; |
4 | 5 | import it.gov.pagopa.idpay.transactions.connector.rest.dto.MerchantDetailDTO; |
5 | 6 | import it.gov.pagopa.idpay.transactions.connector.rest.dto.PointOfSaleDTO; |
6 | 7 | import java.time.Duration; |
|
13 | 14 | import org.springframework.cache.annotation.Cacheable; |
14 | 15 | import org.springframework.http.HttpEntity; |
15 | 16 | import org.springframework.http.HttpMethod; |
| 17 | +import org.springframework.http.HttpStatus; |
16 | 18 | import org.springframework.stereotype.Service; |
17 | 19 | import org.springframework.web.reactive.function.client.WebClient; |
18 | 20 | import org.springframework.web.reactive.function.client.WebClientResponseException; |
19 | 21 | import reactor.core.publisher.Mono; |
20 | 22 | import reactor.util.retry.Retry; |
21 | 23 |
|
| 24 | +import static it.gov.pagopa.idpay.transactions.utils.ExceptionConstants.ExceptionCode.MERCHANT_NOT_FOUND; |
| 25 | +import static it.gov.pagopa.idpay.transactions.utils.ExceptionConstants.ExceptionMessage.ERROR_MESSAGE_MERCHANT_NOT_FOUND; |
| 26 | + |
22 | 27 | @Service |
23 | 28 | @Slf4j |
24 | 29 | @CacheConfig |
@@ -90,39 +95,36 @@ public Mono<PointOfSaleDTO> getPointOfSale(String merchantId, String pointOfSale |
90 | 95 | public Mono<MerchantDetailDTO> getMerchantDetail(String merchantId, String initiativeId) { |
91 | 96 | log.info("Sending request to merchant {} to get merchant details", Utilities.sanitizeString(merchantId)); |
92 | 97 |
|
93 | | - return PerformanceLogger.logTimingOnNext( |
94 | | - "MERCHANT_INTEGRATION", |
95 | | - webClient |
96 | | - .get() |
97 | | - .uri(URI_MERCHANT_DETAIL, Map.of("initiativeId", initiativeId)) |
98 | | - .header("x-merchant-id", merchantId) |
99 | | - .retrieve() |
100 | | - .bodyToMono(MerchantDetailDTO.class), |
101 | | - x -> "received merchant detail" |
| 98 | + return webClient |
| 99 | + .get() |
| 100 | + .uri(URI_MERCHANT_DETAIL, Map.of("initiativeId", initiativeId)) |
| 101 | + .header("x-merchant-id", merchantId) |
| 102 | + .retrieve() |
| 103 | + .onStatus(HttpStatus.NOT_FOUND::equals, response -> |
| 104 | + response.bodyToMono(String.class) |
| 105 | + .flatMap(body -> { |
| 106 | + log.warn("Merchant not found for merchantId {}", merchantId); |
| 107 | + return Mono.error(new ClientExceptionWithBody( |
| 108 | + HttpStatus.NOT_FOUND, |
| 109 | + MERCHANT_NOT_FOUND, |
| 110 | + ERROR_MESSAGE_MERCHANT_NOT_FOUND.formatted(merchantId, initiativeId) |
| 111 | + )); |
| 112 | + }) |
102 | 113 | ) |
| 114 | + .bodyToMono(MerchantDetailDTO.class) |
103 | 115 | .retryWhen( |
104 | 116 | Retry.fixedDelay(maxAttempts, Duration.ofMillis(retryDelay)) |
105 | | - .filter(ex -> { |
106 | | - boolean retry = |
107 | | - ex instanceof WebClientResponseException.TooManyRequests || |
108 | | - ex.getMessage().startsWith("Connection refused"); |
109 | | - |
110 | | - if (retry) { |
111 | | - log.info("[MERCHANT_INTEGRATION] Retrying invocation due to exception: {}: {}", |
112 | | - ex.getClass().getSimpleName(), ex.getMessage()); |
113 | | - } |
114 | | - return retry; |
115 | | - }) |
| 117 | + .filter(ex -> |
| 118 | + ex instanceof WebClientResponseException.TooManyRequests || |
| 119 | + ex.getMessage().startsWith("Connection refused") |
| 120 | + ) |
116 | 121 | ) |
117 | | - .onErrorResume(WebClientResponseException.NotFound.class, ex -> { |
118 | | - log.warn("Merchant detail not found for merchant {}", Utilities.sanitizeString(merchantId)); |
119 | | - return Mono.empty(); |
120 | | - }) |
121 | 122 | .onErrorResume(WebClientResponseException.BadRequest.class, ex -> { |
122 | 123 | log.warn("Invalid request for merchant {}", Utilities.sanitizeString(merchantId)); |
123 | 124 | return Mono.empty(); |
124 | 125 | }); |
125 | 126 | } |
126 | 127 |
|
127 | 128 |
|
| 129 | + |
128 | 130 | } |
0 commit comments