* limitations under the License.
* ========================LICENSE_END===================================
*/
+
package org.oransc.policyagent.clients;
import java.lang.invoke.MethodHandles;
public class AsyncRestClient {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private final WebClient client;
+ private final String baseUrl;
- private static class AsyncRestClientException extends Exception {
+ public class AsyncRestClientException extends Exception {
private static final long serialVersionUID = 1L;
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) //
+ .defaultIfEmpty("");
+ }
+
+ 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);
+ .bodyToMono(String.class) //
+ .defaultIfEmpty("");
}
public Mono<String> put(String uri, String body) {
- logger.debug("PUT uri = '{}''", uri);
+ logger.debug("PUT uri = '{}{}''", baseUrl, uri);
return client.put() //
.uri(uri) //
.contentType(MediaType.APPLICATION_JSON) //
.retrieve() //
.onStatus(HttpStatus::isError,
response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
- .bodyToMono(String.class);
+ .bodyToMono(String.class) //
+ .defaultIfEmpty("");
}
public Mono<String> get(String uri) {
- logger.debug("GET uri = '{}''", uri);
+ logger.debug("GET uri = '{}{}''", baseUrl, uri);
return client.get() //
.uri(uri) //
.retrieve() //
.onStatus(HttpStatus::isError,
response -> Mono.error(new AsyncRestClientException(response.statusCode().toString()))) //
- .bodyToMono(String.class);
+ .bodyToMono(String.class) //
+ .defaultIfEmpty("");
}
public Mono<String> delete(String uri) {
- logger.debug("DELETE uri = '{}''", 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(String.class);
+ .bodyToMono(String.class) //
+ .defaultIfEmpty("");
}
}