Skip to content

Commit 147f32e

Browse files
committed
updated linting
1 parent 0b0f9a2 commit 147f32e

File tree

2 files changed

+233
-219
lines changed

2 files changed

+233
-219
lines changed
Lines changed: 123 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
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;
36
import java.time.Duration;
47
import java.util.Objects;
58
import java.util.function.Consumer;
6-
9+
import lombok.extern.slf4j.Slf4j;
710
import org.springframework.beans.factory.annotation.Qualifier;
811
import org.springframework.http.HttpHeaders;
912
import org.springframework.http.HttpStatus;
@@ -14,12 +17,6 @@
1417
import org.springframework.web.reactive.function.client.WebClient;
1518
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
1619
import org.springframework.web.reactive.function.client.WebClientResponseException;
17-
18-
import com.intuit.springwebclient.entity.ClientHttpRequest;
19-
import com.intuit.springwebclient.entity.ClientHttpResponse;
20-
import com.intuit.springwebclient.retryHandler.RetryHandlerFactory;
21-
22-
import lombok.extern.slf4j.Slf4j;
2320
import reactor.core.publisher.Mono;
2421
import reactor.util.retry.Retry;
2522

@@ -29,117 +26,133 @@
2926
@Slf4j
3027
@Component
3128
public class CommonSpringWebClient {
32-
private final WebClient webClient;
3329

34-
public CommonSpringWebClient(@Qualifier("RWebPulseClient") WebClient webClient) {
35-
this.webClient = webClient;
36-
}
30+
private final WebClient webClient;
31+
32+
public CommonSpringWebClient(@Qualifier("RWebPulseClient") WebClient webClient) {
33+
this.webClient = webClient;
34+
}
3735

38-
/**
39-
* Execute Blocking http request.
40-
* @param httpRequest
41-
* @return
42-
* @param <REQUEST>
43-
* @param <RESPONSE>
44-
*/
45-
public <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> syncHttpResponse(ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
46-
try {
47-
log.info("Executing http request for request={}, method={}", httpRequest.getRequest(),
48-
httpRequest.getHttpMethod());
49-
return generateResponseSpec(httpRequest).toEntity(httpRequest.getResponseType())
50-
.map(this::generateResponse).retryWhen(generateRetrySpec(httpRequest)).block();
51-
} catch (final WebClientResponseException ex) {
52-
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
53-
ex.getResponseBodyAsString(), ex.getHeaders(), ex.getStatusCode());
54-
return handleException(ex, errorMessage, ex.getResponseBodyAsString(),
55-
HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
56-
} catch (final HttpStatusCodeException ex) {
57-
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s statusCode=%s",
58-
ex.getResponseBodyAsString(), ex.getResponseHeaders(), ex.getStatusCode());
59-
return handleException(ex, errorMessage, ex.getResponseBodyAsString(),
60-
HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
61-
} catch (final UnknownContentTypeException ex) {
62-
// It was observed that this exception was thrown whenever there was a HTTP 5XX error
63-
// returned in the REST call. The handle went into `RestClientException` which is the parent
64-
// class of `UnknownContentTypeException` and hence some contextual information was lost
65-
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s",
66-
ex.getResponseBodyAsString(), ex.getResponseHeaders());
67-
return handleException(ex, errorMessage, ex.getResponseBodyAsString(),
68-
HttpStatus.valueOf(ex.getRawStatusCode()), httpRequest);
69-
} catch (final Exception ex) {
70-
final String errorMessage = String
71-
.format("Error in making rest call. Error=%s", ex.getMessage());
72-
return handleException(ex, errorMessage, null, HttpStatus.INTERNAL_SERVER_ERROR, httpRequest);
73-
}
74-
}
36+
/**
37+
* Execute Blocking http request.
38+
*
39+
* @param httpRequest
40+
* @param <REQUEST>
41+
* @param <RESPONSE>
42+
* @return
43+
*/
44+
public <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> syncHttpResponse(
45+
ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
46+
try {
47+
log.info("Executing http request for request={}, method={}", httpRequest.getRequest(),
48+
httpRequest.getHttpMethod());
49+
return generateResponseSpec(httpRequest).toEntity(httpRequest.getResponseType())
50+
.map(this::generateResponse).retryWhen(generateRetrySpec(httpRequest)).block();
51+
} catch (final WebClientResponseException ex) {
52+
final String errorMessage = String.format(
53+
"Error in making rest call. Error=%s Headers=%s statusCode=%s",
54+
ex.getResponseBodyAsString(), ex.getHeaders(), ex.getStatusCode());
55+
return handleException(ex, errorMessage, ex.getResponseBodyAsString(),
56+
HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
57+
} catch (final HttpStatusCodeException ex) {
58+
final String errorMessage = String.format(
59+
"Error in making rest call. Error=%s Headers=%s statusCode=%s",
60+
ex.getResponseBodyAsString(), ex.getResponseHeaders(), ex.getStatusCode());
61+
return handleException(ex, errorMessage, ex.getResponseBodyAsString(),
62+
HttpStatus.valueOf(ex.getStatusCode().value()), httpRequest);
63+
} catch (final UnknownContentTypeException ex) {
64+
// It was observed that this exception was thrown whenever there was a HTTP 5XX error
65+
// returned in the REST call. The handle went into `RestClientException` which is the parent
66+
// class of `UnknownContentTypeException` and hence some contextual information was lost
67+
final String errorMessage = String.format("Error in making rest call. Error=%s Headers=%s",
68+
ex.getResponseBodyAsString(), ex.getResponseHeaders());
69+
return handleException(ex, errorMessage, ex.getResponseBodyAsString(),
70+
HttpStatus.valueOf(ex.getRawStatusCode()), httpRequest);
71+
} catch (final Exception ex) {
72+
final String errorMessage = String
73+
.format("Error in making rest call. Error=%s", ex.getMessage());
74+
return handleException(ex, errorMessage, null, HttpStatus.INTERNAL_SERVER_ERROR, httpRequest);
75+
}
76+
}
7577

76-
/**
77-
* Generate Web Client Response spec from http request.
78-
*
79-
* @param httpRequest
80-
* @return
81-
*/
82-
private <REQUEST, RESPONSE> WebClient.ResponseSpec generateResponseSpec(
83-
ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
78+
/**
79+
* Generate Web Client Response spec from http request.
80+
*
81+
* @param httpRequest
82+
* @return
83+
*/
84+
private <REQUEST, RESPONSE> WebClient.ResponseSpec generateResponseSpec(
85+
ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
8486

85-
Consumer<HttpHeaders> httpHeadersConsumer = (httpHeaders -> httpHeaders
86-
.putAll(httpRequest.getRequestHeaders()));
87-
RequestBodySpec webClientBuilder = webClient.method(httpRequest.getHttpMethod()).uri(httpRequest.getUrl())
88-
.headers(httpHeadersConsumer);
87+
Consumer<HttpHeaders> httpHeadersConsumer = (httpHeaders -> httpHeaders
88+
.putAll(httpRequest.getRequestHeaders()));
89+
RequestBodySpec webClientBuilder = webClient.method(httpRequest.getHttpMethod())
90+
.uri(httpRequest.getUrl())
91+
.headers(httpHeadersConsumer);
8992

90-
// set only when provided
91-
if (Objects.nonNull(httpRequest.getRequest()) && Objects.nonNull(httpRequest.getRequestType())) {
92-
webClientBuilder.body(Mono.just(httpRequest.getRequest()), httpRequest.getRequestType());
93-
}
93+
// set only when provided
94+
if (Objects.nonNull(httpRequest.getRequest()) && Objects.nonNull(
95+
httpRequest.getRequestType())) {
96+
webClientBuilder.body(Mono.just(httpRequest.getRequest()), httpRequest.getRequestType());
97+
}
9498

95-
return webClientBuilder.retrieve();
99+
return webClientBuilder.retrieve();
96100

97-
}
101+
}
98102

99-
/**
100-
* Generates retry spec for the request based on config provided.
101-
* @param httpRequest
102-
* @return
103-
*/
104-
private <REQUEST, RESPONSE> Retry generateRetrySpec(ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
105-
return Retry
106-
.fixedDelay(httpRequest.getClientRetryConfig().getMaxAttempts(),
107-
Duration.ofSeconds(httpRequest.getClientRetryConfig().getBackOff()))
108-
.doBeforeRetry(signal -> log.info("Retrying for requestUrl={}, retryCount={}", httpRequest.getUrl(),
109-
signal.totalRetries()))
110-
.filter(httpRequest.getClientRetryConfig().getRetryFilter());
111-
}
103+
/**
104+
* Generates retry spec for the request based on config provided.
105+
*
106+
* @param httpRequest
107+
* @return
108+
*/
109+
private <REQUEST, RESPONSE> Retry generateRetrySpec(
110+
ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
111+
return Retry
112+
.fixedDelay(httpRequest.getClientRetryConfig().getMaxAttempts(),
113+
Duration.ofSeconds(httpRequest.getClientRetryConfig().getBackOff()))
114+
.doBeforeRetry(
115+
signal -> log.info("Retrying for requestUrl={}, retryCount={}", httpRequest.getUrl(),
116+
signal.totalRetries()))
117+
.filter(httpRequest.getClientRetryConfig().getRetryFilter());
118+
}
112119

113-
/**
114-
* Handle Success response.
115-
*
116-
* @param response
117-
* @return
118-
* @param <RESPONSE>
119-
*/
120-
private <RESPONSE> ClientHttpResponse<RESPONSE> generateResponse(ResponseEntity<RESPONSE> response) {
121-
return ClientHttpResponse.<RESPONSE>builder().response(response.getBody()).status(response.getStatusCode())
122-
.isSuccess2xx(response.getStatusCode().is2xxSuccessful()).build();
123-
}
120+
/**
121+
* Handle Success response.
122+
*
123+
* @param response
124+
* @param <RESPONSE>
125+
* @return
126+
*/
127+
private <RESPONSE> ClientHttpResponse<RESPONSE> generateResponse(
128+
ResponseEntity<RESPONSE> response) {
129+
return ClientHttpResponse.<RESPONSE>builder().response(response.getBody())
130+
.status(response.getStatusCode())
131+
.isSuccess2xx(response.getStatusCode().is2xxSuccessful()).build();
132+
}
124133

125-
/**
126-
* Handle Exception and send back response.
127-
* @param exception
128-
* @param errorMessage
129-
* @param httpStatus
130-
* @param httpRequest
131-
* @return
132-
* @param <RESPONSE>
133-
*/
134-
private <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> handleException(
135-
final Exception exception,
136-
final String errorMessage,
137-
final String responseBody,
138-
final HttpStatus httpStatus,
139-
final ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
140-
log.error("Exception while executing http request for requestUrl={}, status={}, errorMessage={}", httpRequest.getUrl(), httpStatus, errorMessage);
141-
httpRequest.getRetryHandlers()
142-
.forEach(handlerId -> RetryHandlerFactory.getHandler(handlerId.toString()).checkAndThrowRetriableException(exception));
143-
return ClientHttpResponse.<RESPONSE>builder().error(responseBody).status(httpStatus).build();
144-
}
134+
/**
135+
* Handle Exception and send back response.
136+
*
137+
* @param exception
138+
* @param errorMessage
139+
* @param httpStatus
140+
* @param httpRequest
141+
* @param <RESPONSE>
142+
* @return
143+
*/
144+
private <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> handleException(
145+
final Exception exception,
146+
final String errorMessage,
147+
final String responseBody,
148+
final HttpStatus httpStatus,
149+
final ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
150+
log.error(
151+
"Exception while executing http request for requestUrl={}, status={}, errorMessage={}",
152+
httpRequest.getUrl(), httpStatus, errorMessage);
153+
httpRequest.getRetryHandlers()
154+
.forEach(handlerId -> RetryHandlerFactory.getHandler(handlerId.toString())
155+
.checkAndThrowRetriableException(exception));
156+
return ClientHttpResponse.<RESPONSE>builder().error(responseBody).status(httpStatus).build();
157+
}
145158
}

0 commit comments

Comments
 (0)