Add A1 controller client in policy-agent
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / AsyncRestClient.java
index 2e6df94..2574217 100644 (file)
  */
 package org.oransc.policyagent.clients;
 
-import org.oransc.policyagent.exceptions.AsyncRestClientException;
+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 {
+
+        private static final long serialVersionUID = 1L;
+
+        public AsyncRestClientException(String message) {
+            super(message);
+        }
+    }
+
     public AsyncRestClient(String baseUrl) {
         this.client = WebClient.create(baseUrl);
     }
 
+    public Mono<String> post(String uri, String body) {
+        return client.post() //
+            .uri(uri) //
+            .contentType(MediaType.APPLICATION_JSON) //
+            .syncBody(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) //
@@ -44,6 +69,7 @@ public class AsyncRestClient {
     }
 
     public Mono<String> get(String uri) {
+        logger.debug("GET uri = '{}''", uri);
         return client.get() //
             .uri(uri) //
             .retrieve() //
@@ -52,12 +78,13 @@ public class AsyncRestClient {
             .bodyToMono(String.class);
     }
 
-    public Mono<Void> delete(String uri) {
+    public Mono<String> 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(Void.class);
+            .bodyToMono(String.class);
     }
 }