Merge "Recovery handling"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / clients / AsyncRestClient.java
index c69afb2..45c7afa 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.oransc.policyagent.clients;
 
+import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.web.reactive.function.client.WebClient;
 import reactor.core.publisher.Mono;
@@ -26,6 +27,15 @@ import reactor.core.publisher.Mono;
 public class AsyncRestClient {
     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);
     }
@@ -36,6 +46,8 @@ public class AsyncRestClient {
             .contentType(MediaType.APPLICATION_JSON) //
             .syncBody(body) //
             .retrieve() //
+            .onStatus(HttpStatus::isError,
+                response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
             .bodyToMono(String.class);
     }
 
@@ -43,13 +55,17 @@ public class AsyncRestClient {
         return client.get() //
             .uri(uri) //
             .retrieve() //
+            .onStatus(HttpStatus::isError,
+                response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
             .bodyToMono(String.class);
     }
 
-    public Mono<Void> delete(String uri) {
+    public Mono<String> delete(String uri) {
         return client.delete() //
             .uri(uri) //
             .retrieve() //
-            .bodyToMono(Void.class);
+            .onStatus(HttpStatus::isError,
+                response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
+            .bodyToMono(String.class);
     }
 }