-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHttpUtils.java
More file actions
36 lines (33 loc) · 1.61 KB
/
Copy pathHttpUtils.java
File metadata and controls
36 lines (33 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package fr.insee.genesis.infrastructure.utils.http;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.io.IOException;
@UtilityClass
public class HttpUtils {
public static <T, R> ResponseEntity<R> makeApiCall(String baseUrl,
String path,
HttpMethod method,
T requestBody,
Class<R> responseType,
OidcService oidcService
) throws IOException {
WebClient webClient = WebClient.builder()
.baseUrl(baseUrl)
.filter(PerretAuthWebClientFilter.perretAuthFilter(oidcService))
.build();
WebClient.ResponseSpec responseSpec = webClient
.method(method)
.uri(path)
.bodyValue(requestBody)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + oidcService.getServiceAccountToken())
.retrieve();
Mono<ResponseEntity<R>> response = responseSpec.toEntity(responseType);
return response.block();
}
}