*/
package org.oransc.policyagent.clients;
+import java.lang.invoke.MethodHandles;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
public class AsyncRestClient {
+ private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final WebClient client;
private static class AsyncRestClientException extends Exception {
this.client = WebClient.create(baseUrl);
}
+ public Mono<String> post(String uri, String body) {
+ return client.post() //
+ .uri(uri) //
+ .contentType(MediaType.APPLICATION_JSON) //
+ .bodyValue(body) //
+ .retrieve() //
+ .onStatus(HttpStatus::isError,
+ response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
+ .bodyToMono(String.class);
+ }
+
+ public Mono<String> postWithAuthHeader(String uri, String body, String username, String password) {
+ return client.post() //
+ .uri(uri) //
+ .headers(headers -> headers.setBasicAuth(username, password)) //
+ .contentType(MediaType.APPLICATION_JSON) //
+ .bodyValue(body) //
+ .retrieve() //
+ .onStatus(HttpStatus::isError,
+ response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
+ .bodyToMono(String.class);
+ }
+
public Mono<String> put(String uri, String body) {
+ logger.debug("PUT uri = '{}''", uri);
return client.put() //
.uri(uri) //
.contentType(MediaType.APPLICATION_JSON) //
- .syncBody(body) //
+ .bodyValue(body) //
.retrieve() //
.onStatus(HttpStatus::isError,
response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
}
public Mono<String> get(String uri) {
+ logger.debug("GET uri = '{}''", uri);
return client.get() //
.uri(uri) //
.retrieve() //
}
public Mono<String> delete(String uri) {
+ logger.debug("DELETE uri = '{}''", uri);
return client.delete() //
.uri(uri) //
.retrieve() //