Skip to content

Commit

Permalink
feat: fix hard coded status code in response
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishant Sehgal committed Oct 3, 2023
1 parent 44c9259 commit a78352f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package com.intuit.springwebclient.client;

import com.intuit.springwebclient.entity.ClientHttpRequest;
import com.intuit.springwebclient.entity.ClientHttpResponse;
import com.intuit.springwebclient.retryHandler.RetryHandlerFactory;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.time.Duration;
import java.util.Objects;
import java.util.function.Consumer;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.UnknownContentTypeException;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
import org.springframework.web.reactive.function.client.WebClientResponseException;

import com.intuit.springwebclient.entity.ClientHttpRequest;
import com.intuit.springwebclient.entity.ClientHttpResponse;
import com.intuit.springwebclient.retryHandler.RetryHandlerFactory;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;
import java.util.Objects;
import java.util.function.Consumer;

/**
* Spring 5 Web Client Method executor.
*/
Expand All @@ -37,16 +40,21 @@ public class CommonSpringWebClient {
* @param <RESPONSE>
*/
public <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> syncHttpResponse(ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
try {
log.info("Executing http request for request={}, method={}", httpRequest.getUrl(), httpRequest.getHttpMethod());
return generateResponse(
generateResponseSpec(httpRequest)
.bodyToMono(httpRequest.getResponseType())
.retryWhen(generateRetrySpec(httpRequest))
.block());
} catch (final HttpStatusCodeException ex) {
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s",
ex.getResponseBodyAsString(), ex.getResponseHeaders());
try {
log.info("Executing http request for request={}, method={}", httpRequest.getUrl(),
httpRequest.getHttpMethod());
return generateResponseSpec(httpRequest).toEntity(httpRequest.getResponseType()).map(resp -> {
return generateResponse(resp);
}).retryWhen(generateRetrySpec(httpRequest)).block();
}
catch (final WebClientResponseException ex) {
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
ex.getResponseBodyAsString(), ex.getHeaders(), ex.getStatusCode());
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
}
catch (final HttpStatusCodeException ex) {
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
ex.getResponseBodyAsString(), ex.getResponseHeaders(),ex.getStatusCode());
return handleException(ex, errorMessage, HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
} catch (final UnknownContentTypeException ex) {
// It was observed that this exception was thrown whenever there was a HTTP 5XX error
Expand Down Expand Up @@ -90,25 +98,26 @@ private <REQUEST, RESPONSE> WebClient.ResponseSpec generateResponseSpec(
* @param httpRequest
* @return
*/
private Retry generateRetrySpec(ClientHttpRequest httpRequest) {
return Retry.fixedDelay(httpRequest.getClientRetryConfig().getMaxAttempts(), Duration.ofSeconds(httpRequest.getClientRetryConfig().getBackOff()))
.doBeforeRetry(signal -> log.info("Retrying for request={}, retryCount={}", httpRequest.getUrl(), signal.totalRetries()))
.filter(httpRequest.getClientRetryConfig().getRetryFilter());
}
private <REQUEST, RESPONSE> Retry generateRetrySpec(ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
return Retry
.fixedDelay(httpRequest.getClientRetryConfig().getMaxAttempts(),
Duration.ofSeconds(httpRequest.getClientRetryConfig().getBackOff()))
.doBeforeRetry(signal -> log.info("Retrying for request={}, retryCount={}", httpRequest.getUrl(),
signal.totalRetries()))
.filter(httpRequest.getClientRetryConfig().getRetryFilter());
}

/**
* Handle Success response.
* @param response
* @return
* @param <RESPONSE>
*/
private <RESPONSE> ClientHttpResponse<RESPONSE> generateResponse(RESPONSE response) {
return ClientHttpResponse.<RESPONSE>builder()
.response(response)
.status(HttpStatus.OK)
.isSuccess2xx(HttpStatus.OK.is2xxSuccessful())
.build();
}
/**
* Handle Success response.
*
* @param response
* @return
* @param <RESPONSE>
*/
private <RESPONSE> ClientHttpResponse<RESPONSE> generateResponse(ResponseEntity<RESPONSE> response) {
return ClientHttpResponse.<RESPONSE>builder().response(response.getBody()).status(response.getStatusCode())
.isSuccess2xx(response.getStatusCode().is2xxSuccessful()).build();
}

/**
* Handle Exception and send back response.
Expand All @@ -119,11 +128,11 @@ private <RESPONSE> ClientHttpResponse<RESPONSE> generateResponse(RESPONSE respon
* @return
* @param <RESPONSE>
*/
private <RESPONSE> ClientHttpResponse<RESPONSE> handleException(
private <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> handleException(
final Exception exception,
final String errorMessage,
final HttpStatus httpStatus,
final ClientHttpRequest httpRequest) {
final ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
log.error("Exception while executing http request for request={}, status={}, errorMessage={}", httpRequest.getUrl(), httpStatus, errorMessage);
httpRequest.getRetryHandlers()
.forEach(handlerId -> RetryHandlerFactory.getHandler(handlerId.toString()).checkAndThrowRetriableException(exception));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.intuit.springwebclient.client;

import com.intuit.springwebclient.entity.ClientHttpRequest;
import com.intuit.springwebclient.entity.ClientHttpRequest.ClientHttpRequestBuilder;
import java.util.function.Consumer;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -16,8 +15,11 @@
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.UnknownContentTypeException;
import org.springframework.web.reactive.function.client.WebClient;

import com.intuit.springwebclient.entity.ClientHttpRequest;
import com.intuit.springwebclient.entity.ClientHttpRequest.ClientHttpRequestBuilder;

import reactor.core.publisher.Mono;
import java.util.function.Consumer;


@ExtendWith(MockitoExtension.class)
Expand Down

0 comments on commit a78352f

Please sign in to comment.