Skip to content

Commit d8add58

Browse files
Nishant-sehgalNishant Sehgal
andauthored
feat: fix hard coded status code in response (#10)
Co-authored-by: Nishant Sehgal <nishant_sehgal@intuit.com>
1 parent 44c9259 commit d8add58

File tree

2 files changed

+53
-42
lines changed

2 files changed

+53
-42
lines changed

src/main/java/com/intuit/springwebclient/client/CommonSpringWebClient.java

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
package com.intuit.springwebclient.client;
22

3-
import com.intuit.springwebclient.entity.ClientHttpRequest;
4-
import com.intuit.springwebclient.entity.ClientHttpResponse;
5-
import com.intuit.springwebclient.retryHandler.RetryHandlerFactory;
6-
import lombok.AllArgsConstructor;
7-
import lombok.extern.slf4j.Slf4j;
3+
import java.time.Duration;
4+
import java.util.Objects;
5+
import java.util.function.Consumer;
6+
87
import org.springframework.http.HttpHeaders;
98
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
1010
import org.springframework.stereotype.Component;
1111
import org.springframework.web.client.HttpStatusCodeException;
1212
import org.springframework.web.client.UnknownContentTypeException;
1313
import org.springframework.web.reactive.function.client.WebClient;
1414
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
15+
import org.springframework.web.reactive.function.client.WebClientResponseException;
1516

17+
import com.intuit.springwebclient.entity.ClientHttpRequest;
18+
import com.intuit.springwebclient.entity.ClientHttpResponse;
19+
import com.intuit.springwebclient.retryHandler.RetryHandlerFactory;
20+
21+
import lombok.AllArgsConstructor;
22+
import lombok.extern.slf4j.Slf4j;
1623
import reactor.core.publisher.Mono;
1724
import reactor.util.retry.Retry;
1825

19-
import java.time.Duration;
20-
import java.util.Objects;
21-
import java.util.function.Consumer;
22-
2326
/**
2427
* Spring 5 Web Client Method executor.
2528
*/
@@ -37,16 +40,21 @@ public class CommonSpringWebClient {
3740
* @param <RESPONSE>
3841
*/
3942
public <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> syncHttpResponse(ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
40-
try {
41-
log.info("Executing http request for request={}, method={}", httpRequest.getUrl(), httpRequest.getHttpMethod());
42-
return generateResponse(
43-
generateResponseSpec(httpRequest)
44-
.bodyToMono(httpRequest.getResponseType())
45-
.retryWhen(generateRetrySpec(httpRequest))
46-
.block());
47-
} catch (final HttpStatusCodeException ex) {
48-
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s",
49-
ex.getResponseBodyAsString(), ex.getResponseHeaders());
43+
try {
44+
log.info("Executing http request for request={}, method={}", httpRequest.getUrl(),
45+
httpRequest.getHttpMethod());
46+
return generateResponseSpec(httpRequest).toEntity(httpRequest.getResponseType()).map(resp -> {
47+
return generateResponse(resp);
48+
}).retryWhen(generateRetrySpec(httpRequest)).block();
49+
}
50+
catch (final WebClientResponseException ex) {
51+
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
52+
ex.getResponseBodyAsString(), ex.getHeaders(), ex.getStatusCode());
53+
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
54+
}
55+
catch (final HttpStatusCodeException ex) {
56+
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
57+
ex.getResponseBodyAsString(), ex.getResponseHeaders(),ex.getStatusCode());
5058
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
5159
} catch (final UnknownContentTypeException ex) {
5260
// It was observed that this exception was thrown whenever there was a HTTP 5XX error
@@ -90,25 +98,26 @@ private <REQUEST, RESPONSE> WebClient.ResponseSpec generateResponseSpec(
9098
* @param httpRequest
9199
* @return
92100
*/
93-
private Retry generateRetrySpec(ClientHttpRequest httpRequest) {
94-
return Retry.fixedDelay(httpRequest.getClientRetryConfig().getMaxAttempts(), Duration.ofSeconds(httpRequest.getClientRetryConfig().getBackOff()))
95-
.doBeforeRetry(signal -> log.info("Retrying for request={}, retryCount={}", httpRequest.getUrl(), signal.totalRetries()))
96-
.filter(httpRequest.getClientRetryConfig().getRetryFilter());
97-
}
101+
private <REQUEST, RESPONSE> Retry generateRetrySpec(ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
102+
return Retry
103+
.fixedDelay(httpRequest.getClientRetryConfig().getMaxAttempts(),
104+
Duration.ofSeconds(httpRequest.getClientRetryConfig().getBackOff()))
105+
.doBeforeRetry(signal -> log.info("Retrying for request={}, retryCount={}", httpRequest.getUrl(),
106+
signal.totalRetries()))
107+
.filter(httpRequest.getClientRetryConfig().getRetryFilter());
108+
}
98109

99-
/**
100-
* Handle Success response.
101-
* @param response
102-
* @return
103-
* @param <RESPONSE>
104-
*/
105-
private <RESPONSE> ClientHttpResponse<RESPONSE> generateResponse(RESPONSE response) {
106-
return ClientHttpResponse.<RESPONSE>builder()
107-
.response(response)
108-
.status(HttpStatus.OK)
109-
.isSuccess2xx(HttpStatus.OK.is2xxSuccessful())
110-
.build();
111-
}
110+
/**
111+
* Handle Success response.
112+
*
113+
* @param response
114+
* @return
115+
* @param <RESPONSE>
116+
*/
117+
private <RESPONSE> ClientHttpResponse<RESPONSE> generateResponse(ResponseEntity<RESPONSE> response) {
118+
return ClientHttpResponse.<RESPONSE>builder().response(response.getBody()).status(response.getStatusCode())
119+
.isSuccess2xx(response.getStatusCode().is2xxSuccessful()).build();
120+
}
112121

113122
/**
114123
* Handle Exception and send back response.
@@ -119,11 +128,11 @@ private <RESPONSE> ClientHttpResponse<RESPONSE> generateResponse(RESPONSE respon
119128
* @return
120129
* @param <RESPONSE>
121130
*/
122-
private <RESPONSE> ClientHttpResponse<RESPONSE> handleException(
131+
private <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> handleException(
123132
final Exception exception,
124133
final String errorMessage,
125134
final HttpStatus httpStatus,
126-
final ClientHttpRequest httpRequest) {
135+
final ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
127136
log.error("Exception while executing http request for request={}, status={}, errorMessage={}", httpRequest.getUrl(), httpStatus, errorMessage);
128137
httpRequest.getRetryHandlers()
129138
.forEach(handlerId -> RetryHandlerFactory.getHandler(handlerId.toString()).checkAndThrowRetriableException(exception));

src/test/java/com/intuit/springwebclient/client/CommonSpringWebClientTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.intuit.springwebclient.client;
22

3-
import com.intuit.springwebclient.entity.ClientHttpRequest;
4-
import com.intuit.springwebclient.entity.ClientHttpRequest.ClientHttpRequestBuilder;
3+
import java.util.function.Consumer;
54

65
import org.junit.jupiter.api.Test;
76
import org.junit.jupiter.api.extension.ExtendWith;
@@ -16,8 +15,11 @@
1615
import org.springframework.web.client.HttpClientErrorException;
1716
import org.springframework.web.client.UnknownContentTypeException;
1817
import org.springframework.web.reactive.function.client.WebClient;
18+
19+
import com.intuit.springwebclient.entity.ClientHttpRequest;
20+
import com.intuit.springwebclient.entity.ClientHttpRequest.ClientHttpRequestBuilder;
21+
1922
import reactor.core.publisher.Mono;
20-
import java.util.function.Consumer;
2123

2224

2325
@ExtendWith(MockitoExtension.class)

0 commit comments

Comments
 (0)