Skip to content

Commit d5a7f35

Browse files
committed
make RestClient calls null-saf
1 parent ed1f4dd commit d5a7f35

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

src/main/java/fr/insee/genesis/infrastructure/adapter/SurveyQualityToolPerretAdapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public SurveyQualityToolPerretAdapter( Config config,OidcService oidcService) {
3232

3333
@Override
3434
public ResponseEntity<Object> sendProcessedIds(Map<String, Set<String>> processedIdsMap) throws IOException {
35-
return HttpUtils.makeApiCall(config.getSurveyQualityToolUrl(), "/interrogations",HttpMethod.POST, processedIdsMap,
35+
Map<String, Set<String>> safeBody =
36+
processedIdsMap != null ? processedIdsMap : Map.of();
37+
return HttpUtils.makeApiCall(config.getSurveyQualityToolUrl(), "/interrogations",HttpMethod.POST, safeBody,
3638
Object.class, oidcService);
3739
}
3840
}

src/main/java/fr/insee/genesis/infrastructure/utils/http/HttpUtils.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,41 @@
1212

1313
@UtilityClass
1414
public class HttpUtils {
15-
public static <T, R> ResponseEntity<R> makeApiCall(String baseUrl,
16-
String path,
17-
HttpMethod method,
18-
T requestBody,
19-
Class<R> responseType,
20-
OidcService oidcService
21-
) throws IOException {
15+
public static <T, R> ResponseEntity<R> makeApiCall(
16+
String baseUrl,
17+
String path,
18+
HttpMethod method,
19+
T requestBody,
20+
Class<R> responseType,
21+
OidcService oidcService
22+
) throws IOException {
23+
2224
WebClient webClient = WebClient.builder()
2325
.baseUrl(baseUrl)
2426
.filter(PerretAuthWebClientFilter.perretAuthFilter(oidcService))
2527
.build();
26-
WebClient.ResponseSpec responseSpec = webClient
28+
29+
WebClient.RequestBodySpec request = webClient
2730
.method(method)
2831
.uri(path)
29-
.bodyValue(requestBody)
3032
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
31-
.header(HttpHeaders.AUTHORIZATION, "Bearer " + oidcService.getServiceAccountToken())
32-
.retrieve();
33-
Mono<ResponseEntity<R>> response = responseSpec.toEntity(responseType);
34-
return response.block();
33+
.header(HttpHeaders.AUTHORIZATION, "Bearer " + oidcService.getServiceAccountToken());
34+
35+
WebClient.ResponseSpec responseSpec;
36+
37+
if (requestBody != null) {
38+
responseSpec = request
39+
.bodyValue(requestBody)
40+
.retrieve();
41+
} else {
42+
responseSpec = request
43+
.retrieve();
44+
}
45+
46+
if (responseType == null || responseType == Void.class) {
47+
return (ResponseEntity<R>) responseSpec.toBodilessEntity().block();
48+
}
49+
50+
return responseSpec.toEntity(responseType).block();
3551
}
3652
}

0 commit comments

Comments
 (0)