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=0289db98eed83ae9f322ec00c2532bbd3c06e3d7;hb=643ba188e7c1d0c8ce097adfd94ced47b2da615e;hp=cea706088ac671afa38acdf32d2ff8f20fd2a604;hpb=7619c78eac9c8a1ad1e17e0a74d93acd04238ce8;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 cea70608..0289db98 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,86 +17,106 @@ * 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.http.ResponseEntity; +import org.springframework.lang.Nullable; import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec; import reactor.core.publisher.Mono; +/** + * Generic reactive REST client. + */ 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); - } - } + private final String baseUrl; public AsyncRestClient(String baseUrl) { this.client = WebClient.create(baseUrl); + this.baseUrl = baseUrl; } - public Mono post(String uri, String body) { - return client.post() // + public Mono> postForEntity(String uri, @Nullable String body) { + logger.debug("POST uri = '{}{}''", baseUrl, uri); + Mono bodyProducer = body != null ? Mono.just(body) : Mono.empty(); + RequestHeadersSpec request = 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); + .body(bodyProducer, String.class); + return retrieve(request); + } + + public Mono post(String uri, @Nullable String body) { + return postForEntity(uri, body) // + .flatMap(this::toBody); } public Mono postWithAuthHeader(String uri, String body, String username, String password) { - return client.post() // + logger.debug("POST (auth) uri = '{}{}''", baseUrl, uri); + RequestHeadersSpec request = 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); + .bodyValue(body); + return retrieve(request) // + .flatMap(this::toBody); } - public Mono put(String uri, String body) { - logger.debug("PUT uri = '{}''", uri); - return client.put() // + public Mono> putForEntity(String uri, String body) { + logger.debug("PUT uri = '{}{}''", baseUrl, uri); + RequestHeadersSpec request = client.put() // .uri(uri) // .contentType(MediaType.APPLICATION_JSON) // - .bodyValue(body) // - .retrieve() // - .onStatus(HttpStatus::isError, - response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) // - .bodyToMono(String.class); + .bodyValue(body); + return retrieve(request); + } + + public Mono put(String uri, String body) { + return putForEntity(uri, body) // + .flatMap(this::toBody); + } + + public Mono> getForEntity(String uri) { + logger.debug("GET uri = '{}{}''", baseUrl, uri); + RequestHeadersSpec request = client.get().uri(uri); + return retrieve(request); } 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); + return getForEntity(uri) // + .flatMap(this::toBody); + } + + public Mono> deleteForEntity(String uri) { + logger.debug("DELETE uri = '{}{}''", baseUrl, uri); + RequestHeadersSpec request = client.delete().uri(uri); + return retrieve(request); } public Mono delete(String uri) { - logger.debug("DELETE uri = '{}''", uri); - return client.delete() // - .uri(uri) // - .retrieve() // - .onStatus(HttpStatus::isError, - response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) // - .bodyToMono(String.class); + return deleteForEntity(uri) // + .flatMap(this::toBody); } + + private Mono> retrieve(RequestHeadersSpec request) { + return request.retrieve() // + .toEntity(String.class); + } + + Mono toBody(ResponseEntity entity) { + if (entity.getBody() == null) { + return Mono.just(""); + } else { + return Mono.just(entity.getBody()); + } + } + }