X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fclients%2FAsyncRestClient.java;h=0a7c3a180a91a5b8bb227226aa9629189726983b;hb=b66dcce5210e25b2571036becb6f0e7b0c23e1b2;hp=c69afb2f50f4c401d047a0d145b7c8c1c6dc4141;hpb=6d8231a41263b49cb227b8eff2d39b1f3aa778bd;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java b/policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java index c69afb2f..0a7c3a18 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/clients/AsyncRestClient.java @@ -17,39 +17,87 @@ * limitations under the License. * ========================LICENSE_END=================================== */ + 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; + public class AsyncRestClientException extends Exception { + + private static final long serialVersionUID = 1L; + + public AsyncRestClientException(String message) { + super(message); + } + } + public AsyncRestClient(String baseUrl) { this.client = WebClient.create(baseUrl); } + public Mono 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 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 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()))) // .bodyToMono(String.class); } public Mono get(String uri) { + logger.debug("GET uri = '{}''", uri); return client.get() // .uri(uri) // .retrieve() // + .onStatus(HttpStatus::isError, + response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) // .bodyToMono(String.class); } - public Mono delete(String uri) { + public Mono delete(String uri) { + logger.debug("DELETE uri = '{}''", uri); return client.delete() // .uri(uri) // .retrieve() // - .bodyToMono(Void.class); + .onStatus(HttpStatus::isError, + response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) // + .bodyToMono(String.class); } }