Skip to content

Commit 0b0f9a2

Browse files
committed
added asynchronous methods and context propagation
1 parent 88fae17 commit 0b0f9a2

File tree

2 files changed

+51
-51
lines changed

2 files changed

+51
-51
lines changed

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@
2929
@Slf4j
3030
@Component
3131
public class CommonSpringWebClient {
32-
private final WebClient webClient;
32+
private final WebClient webClient;
3333

3434
public CommonSpringWebClient(@Qualifier("RWebPulseClient") WebClient webClient) {
3535
this.webClient = webClient;
3636
}
3737

3838
/**
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) {
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) {
4646
try {
4747
log.info("Executing http request for request={}, method={}", httpRequest.getRequest(),
4848
httpRequest.getHttpMethod());
@@ -73,9 +73,9 @@ public <REQUEST, RESPONSE> ClientHttpResponse<RESPONSE> syncHttpResponse(ClientH
7373
}
7474
}
7575

76-
/**
76+
/**
7777
* Generate Web Client Response spec from http request.
78-
*
78+
*
7979
* @param httpRequest
8080
* @return
8181
*/
@@ -96,11 +96,11 @@ private <REQUEST, RESPONSE> WebClient.ResponseSpec generateResponseSpec(
9696

9797
}
9898

99-
/**
100-
* Generates retry spec for the request based on config provided.
101-
* @param httpRequest
102-
* @return
103-
*/
99+
/**
100+
* Generates retry spec for the request based on config provided.
101+
* @param httpRequest
102+
* @return
103+
*/
104104
private <REQUEST, RESPONSE> Retry generateRetrySpec(ClientHttpRequest<REQUEST, RESPONSE> httpRequest) {
105105
return Retry
106106
.fixedDelay(httpRequest.getClientRetryConfig().getMaxAttempts(),
@@ -112,7 +112,7 @@ private <REQUEST, RESPONSE> Retry generateRetrySpec(ClientHttpRequest<REQUEST, R
112112

113113
/**
114114
* Handle Success response.
115-
*
115+
*
116116
* @param response
117117
* @return
118118
* @param <RESPONSE>
@@ -122,24 +122,24 @@ private <RESPONSE> ClientHttpResponse<RESPONSE> generateResponse(ResponseEntity<
122122
.isSuccess2xx(response.getStatusCode().is2xxSuccessful()).build();
123123
}
124124

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-
}
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+
}
145145
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ public void testSyncHttpResponseSuccess() {
5252

5353
commonSpringWebClient.syncHttpResponse(clientHttpRequest);
5454
}
55-
56-
@Test
57-
public void testSyncHttpResponseSuccessNoRequestBody() {
58-
ClientHttpRequest clientHttpRequest = createClientHttpRequest().request(null).requestType(null).build();
59-
Mockito.when(webClient.method(HttpMethod.GET)).thenReturn(requestBodyUriSpec);
60-
Mockito.doReturn(requestBodyUriSpec).when(requestBodyUriSpec).uri("test-url");
61-
Mockito.when(requestBodyUriSpec.headers(Mockito.any())).thenReturn(requestBodySpec);
6255

63-
commonSpringWebClient.syncHttpResponse(clientHttpRequest);
64-
}
56+
@Test
57+
public void testSyncHttpResponseSuccessNoRequestBody() {
58+
ClientHttpRequest clientHttpRequest = createClientHttpRequest().request(null).requestType(null).build();
59+
Mockito.when(webClient.method(HttpMethod.GET)).thenReturn(requestBodyUriSpec);
60+
Mockito.doReturn(requestBodyUriSpec).when(requestBodyUriSpec).uri("test-url");
61+
Mockito.when(requestBodyUriSpec.headers(Mockito.any())).thenReturn(requestBodySpec);
62+
63+
commonSpringWebClient.syncHttpResponse(clientHttpRequest);
64+
}
6565

6666
@Test
6767
public void testHttpStatusCodeException() {
@@ -115,12 +115,12 @@ public void accept(HttpHeaders httpHeaders) {
115115
httpHeadersConsumer.accept(httpHeadersMock);
116116

117117
return ClientHttpRequest.builder()
118-
.httpMethod(HttpMethod.GET)
119-
.url("test-url")
120-
.requestHeaders(httpHeadersMock)
121-
.requestType(ParameterizedTypeReference.forType(String.class))
122-
.request("hello")
123-
.responseType(ParameterizedTypeReference.forType(String.class));
118+
.httpMethod(HttpMethod.GET)
119+
.url("test-url")
120+
.requestHeaders(httpHeadersMock)
121+
.requestType(ParameterizedTypeReference.forType(String.class))
122+
.request("hello")
123+
.responseType(ParameterizedTypeReference.forType(String.class));
124124
}
125125

126126
private void mockRequestBody() {

0 commit comments

Comments
 (0)