* limitations under the License.
* ========================LICENSE_END===================================
*/
+
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 final String baseUrl;
+
+ 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);
+ this.baseUrl = baseUrl;
+ }
+
+ public Mono<String> post(String uri, String body) {
+ logger.debug("POST uri = '{}{}''", baseUrl, uri);
+ 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) {
+ logger.debug("POST (auth) uri = '{}{}''", baseUrl, uri);
+ 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 = '{}{}''", baseUrl, 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 = '{}{}''", baseUrl, uri);
return client.get() //
.uri(uri) //
.retrieve() //
.bodyToMono(String.class);
}
- public Mono<Void> delete(String uri) {
+ public Mono<String> delete(String uri) {
+ logger.debug("DELETE uri = '{}{}''", baseUrl, uri);
return client.delete() //
.uri(uri) //
.retrieve() //
.onStatus(HttpStatus::isError,
response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
- .bodyToMono(Void.class);
+ .bodyToMono(String.class);
}
}